【问题标题】:Search folders by name of files and print content按文件名搜索文件夹并打印内容
【发布时间】:2017-11-25 14:22:00
【问题描述】:

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

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

【问题讨论】:

  • 您首先需要像 SET "Var_Name=Variable String Value" 这样更改您的代码并像这样引用您的变量 "%Var_Name%""%Var_Name%\" 这都是关于双引号的
  • F3003 在你的例子中应该是FL103,对吧?

标签: windows batch-file pdf printing cmd


【解决方案1】:

以下批处理脚本完全符合您的要求。您的脚本中没有循环,但您应该这样做,请参阅 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"

【讨论】:

  • 除了将for /R移动到子例程中之外,您还可以将pushd "\root\path"放在它之前和popd之后,因为for /R默认为当前目录...
  • @aschipfl:是的,你完全正确!有时间我会改进我的答案。
  • @User552853:我更新了批处理脚本,现在它应该可以完美地适应您的情况。在我编辑之前,路径中的空格存在问题,我对其进行了更改并对其进行了测试。它有效。
【解决方案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

【讨论】:

    【解决方案3】:

    如果我理解正确的要求,一些堆叠的 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
    

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

    【讨论】:

      猜你喜欢
      • 2018-05-03
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多