2

I have Folder A with multiple pdf files named:

FL001.pdf
FL002.pdf
FL003.pdf
etc.

And a Folder B with subdirectories that contain other folders named after the files in Folder A that contains other .pdf files, like this:

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.

And I also have a network printer.


What I'm trying to do:

By following the names of the .pdf files in Folder A search the corresponding subdirectories in Folder B; IF exists then send to the printer the .pdf file from Folder A and then send to the printer all the .pdf files in the corresponding subdirectory from Folder B, then go to the next file and repeat the process for all the names/files in Folder A.

Printing the .pdf's in Folder A is ok, but I need help for the second part which is not working. If I change the current_directory to ...\Folder B\FL000-099\ it works, but I need to search in all subdirectories from the original path. (see code below)


What I have done

@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

Is it possible to search as described above? Can you help me with a solution?

  • 1
    You first need to change your code like this `SET "Var_Name=Variable String Value"` and reference your variables like this `"%Var_Name%"` or `"%Var_Name%\"` **It's all about the doublequotes** – Compo Jun 22 '17 at 12:38
  • 1
    `F3003` in your example should read `FL103`, right? – aschipfl Jun 22 '17 at 14:11

3 Answers3

2

The following batch script does exactly what you have asked for. You have no loops in your script, but you should have to, see here how to loop through files and here how to loop through directories.

Three loops are needed, the first is needed to loop through your files in Folder A. The second loops through all subdirectories in Folder B. The last loop gets all files in that sub directory to compare them with the file of 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"
Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
  • Instead of moving `for /R` into a sub-routine, you could also put `pushd "\root\path"` before and `popd` after it as `for /R` defaults to the current directory... – aschipfl Jun 22 '17 at 13:56
  • @aschipfl: Yeah your absolutely right! I will improve my answer when I have time. – Andre Kampling Jun 22 '17 at 13:57
  • @User552853: I updated the batch script, now it should work perfectly with your situation. Before my edit it has problems with spaces in paths, I changed that and tested it. It works. – Andre Kampling Jun 22 '17 at 17:50
2

If I understand the requirements right, some stacked For, an If and a call should do without all that variables.

@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

If the output looks right remove the echo in the last line.

2

I would not do a recursive search in the directory tree Folder B for each file in Folder A, because the target sub-directories are located on a fixed hierarchy depth anyway. This is a possible solution:

@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

After having successfully tested the script, remove the upper-case ECHO commands in order to actually print any files!


If you want the files in Folder B to be printed in ascending (alphabetical) order by their names, the inner-most for loop must be replaced by a for /F loop together with dir /O:N, because else the sort order depends on the file system:

@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
aschipfl
  • 33,626
  • 12
  • 54
  • 99