按文件名称和打印内容search文件夹

我有文件夹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. 

而且我也有一台networking打印机。


我在做什么:

通过跟踪文件夹A中 .pdf文件的名称,search文件夹B中相应的子目录; 如果存在,则从文件夹A向打印机发送.pdf文件,然后将文件夹B中相应子目录中的所有.pdf文件发送到打印机,然后转到下一个文件并重复所有文件中的名称/文件文件夹A.

在文件夹A中打印.pdf是可以的,但是我不需要第二部分的帮助。 如果我将current_directory更改为...\Folder B\FL000-099\它可以工作,但是我需要从原始path中search所有子目录。 (见下面的代码)


我做了什么

 @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 

是否可以像上面描述的那样进行search? 你能帮我解决吗?

以下批处理脚本完全符合您的要求。 你的脚本中没有循环,但你必须在这里看到如何遍历文件 , 在这里如何遍历目录 。

需要三个循环,第一个循环需要遍历文件Folder A中的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" 

我不会在目录树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中的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 

如果我理解了这个需求,那么一些堆积的For,一个If和一个调用应该没有所有的变量。

 @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 

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