按文件名和打印内容搜索文件夹

时间:2017-06-22 10:44:00

标签: windows batch-file pdf printing cmd

我的文件夹A 包含多个名为

的pdf文件
FL001.pdf
FL002.pdf
FL003.pdf
etc.

文件夹B ,其子目录包含以文件夹A 中包含其他.pdf文件的文件命名的其他文件夹,如下所示:

FL000-099
        FL001
           - 001100.pdf
           - 001101.pdf
        FL002
           - 002100.pdf
           - 002101.pdf
        FL003
           - 003100.pdf
           - 003101.pdf
FL100-199
        FL101
           - 101100.pdf
           - 101101.pdf
        FL102
           - 102100.pdf
           - 102101.pdf
        F3003
           - 103100.pdf
           - 103101.pdf
 etc.

我还有一台网络打印机。

我正在尝试做什么:

按照文件夹A 中的.pdf文件名称搜索文件夹B 中的相应子目录;如果存在,则从文件夹A 向打印机发送.pdf文件,然后从文件夹B 将相应子目录中的所有.pdf文件发送给打印机,然后转到下一个文件,并对文件夹A中的所有名称/文件重复该过程。

打印文件夹A中的.pdf是可以的,但我需要帮助才能解决第二部分无效的问题。如果我将current_directory更改为...\Folder B\FL000-099\它可以正常工作,但我需要从原始路径搜索所有子目录。 (见下面的代码)

我做了什么

@echo off 
set current_directory=C:\Users\user\Desktop\Folder B\
set art_directory=C:\Users\user\Desktop\Folder A\

set filename=FL001
set extension=.pdf

set tofind=%current_directory%%filename%
set tofind2=%art_directory%%filename%

set tofindextension=%tofind2%%extension%


IF EXIST %tofindextension% ( "C:\Program Files\SumatraPDF\SumatraPDF.exe" %tofindextension% -print-to "\\server\printer"
    ) ELSE (
        echo "No file!"
    )

IF EXIST %tofind%\ (
    FOR /R %tofind% %%F in (*.pdf*) do "C:\Program Files\SumatraPDF\SumatraPDF.exe" %tofind%\%%~nxF -print-to "\\server\printer"

    )  ELSE (

        echo "No file!"
    )


pause

是否可以按上述方式进行搜索?你能帮我解决一下吗?

3 个答案:

答案 0 :(得分:2)

以下批处理脚本完全符合您的要求。您的脚本中没有循环,但您必须这样做,请参阅here how to loop through fileshere how to loop through directories

需要三个循环,第一个循环需要循环遍历Folder A中的文件。第二个循环遍历Folder B中的所有子目录。最后一个循环获取该子目录中的所有文件,以将它们与Folder A

文件进行比较
@echo off
setlocal enabledelayedexpansion

rem set directories
set "current_directory=C:\Users\andre_kampling\Desktop\batTest\Folder B"
set "art_directory=C:\Users\andre_kampling\Desktop\batTest\Folder A"
rem set extension
set "extension=txt"


rem loop through all files of "Folder A"
pushd "%art_directory%"
for /r %%f in ("*.%extension%") do (
   set "curFileA=%%~nf"
   set "curFileAFull=%%f"
   rem loop through all directories of "Folder B"
   for /d %%d in ("%current_directory%\*") do (
      set "curDirB=%%d"
      rem is in "Folder B\*" a folder with the name of file in "Folder A"?
      set "curSubDirB=!curDirB!\!curFileA!"

      if exist "!curSubDirB!\" (
         rem file exists print file of "Folder A" and all files found
         rem in sub directory
         echo.
         echo Subfolder is existent "!curSubDirB!"
         call :print curFileAFull

         rem loop through all files in that subdirectory    
         pushd "!curSubDirB!"
         for /r %%g in ("*.%extension%") do (
            set "file=%%g"
            call :print file
         )
         popd
      )
   )
)
popd

pause
exit /B

rem function definitions
:print
set "arg=!%1!"
echo File "%arg%" will be printed...
"C:\Program Files\SumatraPDF\SumatraPDF.exe" %arg% -print-to "\\server\printer"

答案 1 :(得分:2)

如果我理解了正确的要求,有些堆叠For,if和a调用应该没有所有变量。

@Echo off&SetLocal

Set "DirA=A:\FolderA"
Set "DirB=A:\FolderB"
PushD "%DirA%"
For %%A in ("FL*.pdf"
) Do For /F "delims=" %%B in (
  'Dir /B/AD/S "%DirB%\%%~nA"'
) Do If Exist "%%~fB\*.pdf" (
  Call :Print "%%~fA"
  For %%C in ("%%~fB\*.pdf") Do Call :Print "%%~fC"
)
PopD
Pause
Goto :Eof
:Print
Echo "C:\Program Files\SumatraPDF\SumatraPDF.exe" "%~1" -print-to "\\server\printer"
Goto :Eof

如果输出看起来正确,则删除最后一行中的回声。

答案 2 :(得分:2)

我不会在目录树Folder B中对Folder A中的每个文件进行递归搜索,因为目标子目录无论如何都位于固定的层次结构深度上。这是一个可能的解决方案:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_CUR_DIR=C:\Users\user\Desktop\Folder B"
set "_ART_DIR=C:\Users\user\Desktop\Folder A"
set "_PATTERN=*.pdf"
set "_PRN_EXE=%ProgramFiles%\SumatraPDF\SumatraPDF.exe"
set "_PRN_SWI=-print-to"
set "_PRINTER=\\server\printer"

rem // Loop through first directory and enumerate the matching files:
for %%A in ("%_ART_DIR%\%_PATTERN%") do (
    rem /* Loop through the second directory and only enumerate the
    rem    immediate sub-directories: */
    for /D %%B in ("%_CUR_DIR%\*") do (
        rem /* Simply check whether a sub-directory exists having the
        rem    same name as the current file in the first directory: */
        if exist "%%~B\%%~nA\" (
            rem /* Print the file from the first directory and all files
            rem    located in the found sub-directory: */
            for %%P in ("%%~A" "%%~B\%%~nA\%_PATTERN%") do (
                ECHO "%_PRN_EXE%" "%%~P" %_PRN_SWI% "%_PRINTER%"
            )
        )
    )
)

endlocal
exit /B

成功测试完脚本后,删除大写的ECHO命令,以便实际打印任何文件!

如果您希望Folder B中的文件按名称按升序(字母)顺序打印,则最内层for循环必须由for /F循环替换为dir /O:N,因为排序顺序取决于文件系统:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_CUR_DIR=C:\Users\user\Desktop\Folder B"
set "_ART_DIR=C:\Users\user\Desktop\Folder A"
set "_PATTERN=*.pdf"
set "_PRN_EXE=%ProgramFiles%\SumatraPDF\SumatraPDF.exe"
set "_PRN_SWI=-print-to"
set "_PRINTER=\\server\printer"

rem // Loop through first directory and enumerate the matching files:
for %%A in ("%_ART_DIR%\%_PATTERN%") do (
    rem /* Loop through the second directory and only enumerate the
    rem    immediate sub-directories: */
    for /D %%B in ("%_CUR_DIR%\*") do (
        rem /* Simply check whether a sub-directory exists having the
        rem    same name as the current file in the first directory: */
        if exist "%%~B\%%~nA\" (
            rem // Print the file from the first directory:
            ECHO "%_PRN_EXE%" "%%~A" %_PRN_SWI% "%_PRINTER%"
            rem /* Print all files located in the found sub-directory,
            rem    sorted alphabetically in ascending order: */
            for /F "delims= eol=|" %%P in ('
                dir /B /A:-D /O:N "%%~B\%%~nA\%_PATTERN%"
            ') do (
                ECHO "%_PRN_EXE%" "%%~B\%%~nA\%%~P" %_PRN_SWI% "%_PRINTER%"
            )
        )
    )
)

endlocal
exit /B