如何获取文件列表并使用文件名作为字符串操作的输入(重复检查)&条件使用批处理文件?

时间:2014-10-22 10:04:10

标签: windows batch-file

我正在尝试创建一个Windows批处理文件:

1st:列出当前目录中的文件并输出到文件名列表(Filelist_ [YYY] [MM] [DD] [hh] [mm] ss])

第二:从给定的文件名列表中,读取每一行作为第三步的输入

3rd:读取文件的第一行并提取第7,第9和第14个字符串。

Delimiter can be "~", "*" and "^"

第4步:将第7个分配为ISAsender变量,将第9个分配为ISAreceiver,将第14个字符串分配为ISActrlnum。

5th:将回显变量重定向到单个日志文件(EDI.log)。

编辑: 6:如果ISActrlnum已经存在或重复,请检查ISAsender的CNtable.txt(例如,ISA的APPLESND的AP_CNtable.txt,SAMSUNGSND的SS_CNtable)。最初CNtable.txt包含ISActrlnum(控制号)的转储值。

a. If there is duplicate control number found, exit with error message and output to Dupfile.txt as well as the ISActrlnum in issue. Move file to EXCEPT folder.
b. If not duplicate, insert ISActrlnum to CNtable.txt then proceed to 7th.

7th:对ISAsender和ISAreceiver使用IF条件将文件移动到相应的文件夹。 8日:重复第3至第7步,直至第2步达到EOF或完成每行读取。

示例:从给定的Filelist_20141022010101,FileName.txt包含以下“〜”作为分隔符:

ISA~00~          ~00~          ~ZZ~APPLESND       ~14~APPLERCV       ~130214~2300~U~00401~000000001~0~T~>
GS~FA~APPLESND~APPLERCV~20130214~2300~810~X~004010
ST~997~131250001
AK1~SC~1809
AK9~A~1~1~1
SE~4~131250001
GE~1~810
IEA~1~000000001

虽然CNtable.txt包含:

000000004
000000002
000000003
000000005

由于CNtable.txt中不存在ISActrlnum = 000000001,它将通过附加ISActrlnum来更新CNtable.txt。这里只使用ISAsender和ISAreceiver的条件语句。 因此,如果ISAsender = APPLESND和ISAreceiver = APPLERCV将curent文件(FileName.txt)移动到特定目录,例如C:\ VP \ APPLE。 Filelist_20141022010101内的下一个文件名的逻辑相同。

我能够编写下面的代码但不知道如何从FORFILES获取文件名并再次将其用作FOR LOOP中的变量输入。

REM FileReceiver.bat
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%

set TIMESTAMP=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%

C:
cd "C:\VP"

FORFILES /P "C:\VP" >"FLIST.%TIMESTAMP%"


set first=1
for /f "tokens=7 delims=~" %%a in (FileName.txt) do (
      if %first%==1 (
           set first=0
       set ISAsender=%%a
       echo %ISAsender%

      )
)

set first=1
for /f "tokens=9 delims=~" %%b in (FileName.txt) do (
      if %first%==1 (
           set first=0
       set ISAreceiver=%%b
       echo %ISAreceiver%

      )
)
set first=1
for /f "tokens=14 delims=~" %%c in (FileName.txt) do (
      if %first%==1 (
           set first=0
       set ISActrlnum=%%c
       echo %ISActrlnum%
      )
)


if %ISAsender%=="APPLESND" if %ISAreceiver=="APPLERCV"    (
    move FileName.txt "C:\VP\APPLE"
    )

if %ISAsender%=="SAMSUNGSND" if %ISAreceiver=="SAMSUNGRCV"    (
    move FileName.txt "C:\VP\SAMSUNG"
    )

任何人都可以帮助我满足要求而无需调用另一个检查重复控制号(ISActrlnum)的批处理文件吗? 我希望尽可能将整个代码放在一个bat文件中。请加上我冗长的描述。

1 个答案:

答案 0 :(得分:0)

我需要设置您计算机上的整个环境来重新编码批处理文件,但我想我已经得到了它。

阅读评论 - 以rem开头的行 - 了解批处理文件并在必要时将其调整到您的环境。

@echo off
REM FileReceiver.bat
setlocal

rem Get current date and time as local time independent on Windows region settings.
for /F "tokens=2 delims==." %%T in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "dt=%%T"

rem Reformat the date and time strong to wanted format.
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"
set "TimeStamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"

rem Define name of the list file containing current date and time in name.
set "ListFile=FLIST_%TimeStamp%.lst"

rem Change directory (and drive).
cd /D "C:\VP"

rem Create the list file which is good here as the list of files changes
rem while running this batch file and therefore it is better to work with
rem a list file instead of running a FOR directly for each file in the
rem directory. The list file is not included in this list as it either does
rem not exist at all or it has wrong file extension as only *.txt files are
rem listed by command DIR. The log file EDI.log has also wrong file extension.
dir *.txt /A-D /B /ON >%ListFile%

rem It might be useful to delete the log file from a previous run.
if exist EDI.log del EDI.log

rem Process each file in the list file.
for /F "delims=" %%F in ( %ListFile% ) do call :ProcessFile "%%F"

rem Delete the list file as not needed anymore. It could be also kept.
del %ListFile%

rem Exit batch file.
endlocal
goto :EOF


:ProcessFile
rem The parameter passed from first FOR is the file name in double quotes.
set "FileName=%~1"

rem Ignore the files *_CNtable.txt and Dupfile.txt in same directory.
rem Command goto :EOF just exits here from subroutine ProcessFile.
if "%FileName%"=="AP_CNtable.txt" goto :EOF
if "%FileName%"=="SS_CNtable.txt" goto :EOF
if "%FileName%"=="Dupfile.txt" goto :EOF

rem Get 7th, 9th and 14th element from first line of current file.
for /f "usebackq tokens=7,9,14 delims=~*^" %%a in ( "%FileName%" ) do (
   set "ISAsender=%%a"
   set "ISAreceiver=%%b"
   set "ISActrlnum=%%c"
   goto WriteToLogFile
)

:WriteToLogFile
rem Remove all spaces as ISAsender and ISAreceiver have
rem usually spaces appended at end according to example
rem text. Then write file name and the 3 values to log file.
set "ISAsender=%ISAsender: =%"
set "ISAreceiver=%ISAreceiver: =%"
set "ISActrlnum=%ISActrlnum: =%"
echo %FileName%,%ISAsender%,%ISAreceiver%,%ISActrlnum%>>EDI.log

if "%ISAsender%"=="APPLESND" (
    rem Check for ISA control number in file AP_CNtable.txt.
    %SystemRoot%\System32\Findstr.exe /X /M /C:%ISActrlnum% AP_CNtable.txt >nul
    if not errorlevel 1 goto Duplicate

    echo %ISActrlnum%>>AP_CNtable.txt

    if "%ISAreceiver%"=="APPLERCV" (
        move /Y "%FileName%" "APPLE"
        echo Moved %FileName% to directory APPLE.
    )
)

if "%ISAsender%"=="SAMSUNGSND" (
    rem Check for ISA control number in file SS_CNtable.txt.
    %SystemRoot%\System32\Findstr.exe /X /M /C:%ISActrlnum% SS_CNtable.txt >nul
    if not errorlevel 1 goto Duplicate

    echo %ISActrlnum%>>SS_CNtable.txt

    if "%ISAreceiver%"=="SAMSUNGRCV" (
        move /Y "%FileName%" "SAMSUNG"
        echo Moved %FileName% to directory SAMSUNG.
    )
)
rem Exit the subroutine ProcessFile.
goto :EOF

:Duplicate
rem This ISA control number is already present in file *_CNtable.txt.
echo Duplicate control %ISActrlnum% found in file %FileName%.
echo %ISActrlnum%,%FileName%>>Dupfile.txt

rem Exit the subroutine ProcessFile.
goto :EOF

我不确定为什么列表文件必须具有名称中的当前日期和时间,但是您可能希望保留它的原因不明。在这种情况下,有必要删除上面代码中的2行 - 带有命令del %ListFile%的行和上面的注释行。

根据相关第6点的变化,在2014-10-07更新了批次代码。