使用FOR / F,FIND / C等命令在类似命名的文件中在BATCH中出现问题

时间:2014-05-27 05:35:49

标签: batch-file pdf for-loop find

我已经在Batch中编写了一个小程序,它将一对PDF与一个控制文件一起打包并将其拉出来发送给我们公司内的另一个团队。我最近扩展了这个程序以处理其他类型的PDF,但是其他类型的类似命名为预先存在的导致我的程序出现问题。

以下是我的代码:

:ZIPPAIRS
::Zips pairs -- checks for pairs first using filelist.txt
ECHO [INFO] Checking for pairs, if a pair exists, zipping it up >> %LOG%
ECHO.
ECHO Zipping all pairs from your PDFCOMPARE folder...
ECHO.
CD %DRIVE%\PDFCompareApp\PDFCOMPARE
FOR /F "delims=|" %%i IN (filelist.txt) DO (
        SETLOCAL EnableDelayedExpansion
        CD %DRIVE%\PDFCompareApp\PDFCOMPARE
        SET COMPAREZIPS="dir /b "%%i*.pdf" | find /c "%%i" "
        FOR /F %%j IN (' !COMPAREZIPS! ') DO SET X=%%j
        IF !X! EQU 2 (
                ECHO [INFO] Zipping %%i PDF's and control_file.xml >> %LOG%
                ECHO Zipping %%i PDF's and control_file.xml...
                %DRIVE%\PDFCompareApp\7-ZIP\7z a %DRIVE%\PDFCompareApp\PDFCOMPARE\%%i.zip %DRIVE%\PDFCompareApp\PDFCOMPARE\%%i*.* %DRIVE%\PDFCompareApp\PDFCOMPAREDOWNLOADED\control_file.xml
                ECHO.
                ECHO [INFO] Moving %%i PDF's >> %LOG%
                ECHO Moving %%i PDF's
                ECHO.
                MOVE %DRIVE%\PDFCompareApp\PDFCOMPARE\%%i*.pdf %DRIVE%\PDFCompareApp\PDFCOMPARE\FINISHED
                MOVE %DRIVE%\PDFCompareApp\PDFCOMPARE\%%i*.zip %DRIVE%\PDFCompareApp\PDFCOMPARE\ZIPS
            )   ELSE (
                ECHO.
                ECHO.
                ECHO [WARN] This compare %%i does not have a matching pair >> %LOG%
                ECHO This compare %%i does not have a matching pair
                ECHO.
                ECHO.
            )
SET i=""
SET j=""
SET X=""
ENDLOCAL
)

以下是filelist.txt

的示例
2174_1_1_TEST_|
2174_1_1_TEST_SO_|
2174_1_1_ABC_|
2174_1_1_ABC_DEF_|
2174_1_1_XYZ_MNO_|
998014_YOYO_SP_|
998014_YOYO_|
998014_MAILMAN_|
998014_BATMAN_|
998014_SPIDERMAN_|
998014_CATWOMAN_|
311009_SUPERMAN_|

最后,这是PDFCOMPARE文件夹中PDF的一部分:

2174_1_1_TEST_dev.pdf
2174_1_1_TEST_qa.pdf
2174_1_1_TEST_SO_dev.pdf
2174_1_1_TEST_SO_qa.pdf

最后一个后缀可能会根据PDF来自哪个环境而改变,但此时它只能来自4:dev,qa,uat,prod(如果此信息有帮助)。

该计划在此问题上存在问题:

SET COMPAREZIPS="dir /b "%%i*.pdf" | find /c "%%i" "

该行总是应该是2,如果程序确实找到了2个类似命名的文件,但是基于上面的例子,当它在filelist.txt中命中2174_1_1_TEST_|时它找到4而在命中{{2174_1_1_TEST_SO_|时找到2 1}}在filelist.txt。

如果有帮助,该问题仅存在于TEST / TEST_SO,ABC / ABC_DEF,YOYO / YOYO_SP文件中。

我尝试了很多东西(改变FOR循环,调整重定向/管道等),但我一直在回头挠挠脑袋。我确信这很简单,但我被击败并要求SO的帮助。请帮忙!

提前谢谢!

1 个答案:

答案 0 :(得分:1)

您可以更改for命令以明确搜索所需内容

SET "COMPAREZIPS=dir /b "%%idev.pdf" "%%iqa.pdf" "%%iuat.pdf" "%%iprod.pdf" ^| find /c "%%i" "

或添加过滤器以确保只计算相关文件

SET "COMPAREZIPS=dir /b "%%i*.pdf" ^| findstr /i /r /c:"^%%i[^_]*\.pdf$" ^| find /c "%%i" "

这将解决文件计数的问题,但不会避免使用%%i*.pdf来处理文件的其他命令的问题。

因此,最好首先检索将在每次迭代中处理的文件列表,然后使用此列表执行其余命令

:ZIPPAIRS
::Zips pairs -- checks for pairs first using filelist.txt
ECHO [INFO] Checking for pairs, if a pair exists, zipping it up >> %LOG%
ECHO.
ECHO Zipping all pairs from your PDFCOMPARE folder...
ECHO.

setlocal enableextensions disabledelayedexpansion

set "sources=dev qa uat prod"
set "tempFile=%temp%\%~nx0.%random%.tmp"
pushd "%DRIVE%\PDFCompareApp\PDFCOMPARE"
FOR /F "delims=|" %%i IN (filelist.txt) DO (
    :: Search for required files 
    dir /b "%%idev.pdf" "%%iqa.pdf" "%%iuat.pdf" "%%iprod.pdf" > "%tempFile%"
    for /f %%j in (
        ' ^<"%tempFile%" find /i /c "%%i" '
    ) do if %%j equ 2 (
        ECHO [INFO] Zipping %%i PDF's and control_file.xml >> %LOG%
        ECHO Zipping %%i PDF's and control_file.xml...
        %DRIVE%\PDFCompareApp\7-ZIP\7z a %DRIVE%\PDFCompareApp\PDFCOMPARE\%%i.zip @"%tempFile%" %DRIVE%\PDFCompareApp\PDFCOMPAREDOWNLOADED\control_file.xml
        ECHO.
        ECHO [INFO] Moving %%i PDF's >> %LOG%
        ECHO Moving %%i PDF's
        ECHO.
        for /f "usebackq delims=" %%k in ("%tempFile%") do MOVE %DRIVE%\PDFCompareApp\PDFCOMPARE\%%k %DRIVE%\PDFCompareApp\PDFCOMPARE\FINISHED
        MOVE %DRIVE%\PDFCompareApp\PDFCOMPARE\%%i*.zip %DRIVE%\PDFCompareApp\PDFCOMPARE\ZIPS
    ) else (
        ECHO.
        ECHO.
        ECHO [WARN] This compare %%i does not have a matching pair >> %LOG%
        ECHO This compare %%i does not have a matching pair
        ECHO.
        ECHO.
    )
)
popd
del /q "%tempFile%" >nul 2>nul 
endlocal