搜索按文件的名称和打印内容文件夹
我文件夹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
是否有可能如上述来搜索?你能帮我解决问题吗?
以下批处理脚本完全符合您的要求。脚本中没有循环,但您必须参阅here how to loop through files和here 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"
除了将'for/R'移动到一个子例程之外,您还可以在之前和之后使用'pushd'\ root \ path''作为'for/R'默认当前目录... – aschipfl
@aschipfl:是的,你绝对正确!当我有空时,我会改进我的回答。 –
@ User552853:我更新了批处理脚本,现在它应该能够与您的情况完美协作。在编辑之前,它在路径中存在空间问题,我改变了它并测试了它。有用。 –
如果我理解了正确的要求,某些堆叠的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
如果输出看起来不错,请删除最后一行中的回显。
我不会做在目录树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
首先,您需要更改代码这样的'SET“VAR_NAME =变量字符串值”'和引用您的变量,像这样的'“%VAR_NAME%”'或'“%VAR_NAME%\ “'**这是关于双引号** – Compo
'F3003'在你的例子应该读'FL103',钻机H T? – aschipfl