2017-06-22 210 views
2

文件夾A與多個PDF文件名爲:搜索按文件的名稱和打印內容文件夾

FL001.pdf 
FL002.pdf 
FL003.pdf 
etc. 

而一個文件夾B與包含的文件命名的其他文件夾中文件夾中的子目錄一個包含其他.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. 

而且我也有一個網絡打印機。


我想要做的事:

按照在文件夾中的.pdf文件的名稱的搜索在文件夾B對應的子目錄;如果不存在,然後發送到打印機從.pdf文件夾中,然後發送到打印機的所有.pdf文件中對應的子目錄從文件夾B,然後進入下一個文件,重複此過程爲所有的文件夾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 

是否有可能如上述來搜索?你能幫我解決問題嗎?

+1

首先,您需要更改代碼這樣的'SET「VAR_NAME =變量字符串值」'和引用您的變量,像這樣的'「%VAR_NAME%」'或'「%VAR_NAME%\ 「'**這是關於雙引號** – Compo

+1

'F3003'在你的例子應該讀'FL103',鑽機H T? – aschipfl

回答

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" 
+0

除了將'for/R'移動到一個子例程之外,您還可以在之前和之後使用'pushd'\ root \ path''作爲'for/R'默認當前目錄... – aschipfl

+0

@aschipfl:是的,你絕對正確!當我有空時,我會改進我的回答。 –

+0

@ User552853:我更新了批處理腳本,現在它應該能夠與您的情況完美協作。在編輯之前,它在路徑中存在空間問題,我改變了它並測試了它。有用。 –

2

如果我理解了正確的要求,某些堆疊的For,If和Call應該沒有所有變量。

@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

我不會做在目錄樹Folder BFolder 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