2

I have multiple folders, each one containing multiple pdf image files like this:

  Folder 1 
     file1.pdf
     file2.pdf
     file3.pdf
     ...
     file100.pdf
  Folder 2 
     file1.pdf
     file2.pdf
     file3.pdf
     ...
     file100.pdf
  Folder 3 
     file1.pdf
     file2.pdf
     file3.pdf
     ...
     file100.pdf
  ...
  Folder 94
     file1.pdf
     file2.pdf
     file3.pdf
     ...
     file100.pdf 

Is there any way to create a sort of script that outputs at one time each folder as one pdf file like this :

Folder1.pdf
Folder2.pdf
Folder3.pdf
...
Folder94.pdf
1

Merge PDF Files in a Folder to One PDF File Matching the Folder Name

Since you have a solution to convert all the JPG files into PDF files, you need a solution that will merge all PDF files in a folder to a single PDF file merged chronologically based on file names.

You can use PDFtk Free and its CLI PDFtk with the cat parameter in a batch script to automate the operations to convert all PDF files within a folder to a single PDF with the folder name as the file name.

"PDFtk Free is our friendly graphical tool for quickly merging and splitting PDF documents and pages. It is free to use for as long as you like."

"Power Users: PDFtk Free comes with our command-line tool, PDFtk Server. So you get both the GUI and the command-line interface to PDFtk!"


Batch Script

Note: The SourceParentDir= value will be the full path to the location where the subfolders with the PDF files reside which you need to merge.

@ECHO OFF

SET "SourceParentDir=C:\Root\Parent\Folder"
FOR /R "%SourceParentDir%" %%A IN (.) DO (
    IF /I NOT [%%~A]==[%SourceParentDir%\.] pdftk "%SourceParentDir%\%%~NA\*.pdf" cat output "%SourceParentDir%\%%~NA.pdf"
    )
PAUSE
EXIT

Batch Script (Inverse Order)

@ECHO OFF

SET "SourceParentDir=C:\Root\Parent\Folder"
FOR /R "%SourceParentDir%" %%A IN (.) DO (
    IF /I NOT [%%~A]==[%SourceParentDir%\.] pdftk "%SourceParentDir%\%%~NA\*.pdf" cat output "%SourceParentDir%\temp.pdf"
    IF EXIST "%SourceParentDir%\temp.pdf" pdftk "%SourceParentDir%\temp.pdf" cat end-1 output "%SourceParentDir%\%%~NA.pdf"
    IF EXIST "%SourceParentDir%\%%~NA.pdf" IF EXIST "%SourceParentDir%\temp.pdf" DEL /Q /F "%SourceParentDir%\temp.pdf"
    )
PAUSE
EXIT

Further Resources

  • FOR /R

    FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]
    
        Walks the directory tree rooted at [drive:]path, executing the FOR
        statement in each directory of the tree.  If no directory
        specification is specified after /R then the current directory is
        assumed.  If set is just a single period (.) character then it
        will just enumerate the directory tree.
    
  • IF

  • Batch Substitutions (FOR /?)

    In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:

    %~nI        - expands %I to a file name only
    
  • pdftk.exe --Help

          cat [<page ranges>]
                 Assembles (catenates) pages from input PDFs to create a new
                 PDF. Use cat to merge PDF pages or to split PDF pages from
                 documents. You can also use it to rotate PDF pages. Page
                 order in the new PDF is specified by the order of the given
                 page ranges. Page ranges are described like this:
    
                 <input PDF handle>[<begin page number>[-<end page num-
                 ber>[<qualifier>]]][<page rotation>]
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.