Improving Windows Batch File: Merge Two PDFs from Separate Folders
In this article, we will focus on enhancing a Windows batch file that merges two PDFs from separate folders and dumps the result in a third folder. The original batch file may have some limitations, and we will address those by providing a more detailed explanation of the key concepts involved and offering solutions to common issues.
Key Concepts
- Merging PDFs
- Using Windows Batch Files
- Working with Multiple Folders
Original Windows Batch File
Here is the original Windows batch file that merges two PDFs from separate folders:
@echo off
setlocal
set "folder1=C:\Folder1"
set "folder2=C:\Folder2"
set "outputFolder=C:\OutputFolder"
pdftk.exe %folder1%\PDF1.pdf %folder2%\PDF2.pdf output %outputFolder%\merged.pdf
endlocal
Issues with the Original Batch File
The original batch file assumes that:
- The input PDFs are named PDF1.pdf and PDF2.pdf.
- The folders containing the input PDFs are named Folder1 and Folder2.
- The output folder is named OutputFolder.
These assumptions may not always hold, and the batch file should be modified to handle more general cases.
Enhanced Windows Batch File
Here is an enhanced version of the Windows batch file that can handle more general cases:
@echo off
setlocal
set /p "folder1=Enter the path to the first folder: "
set /p "folder2=Enter the path to the second folder: "
set /p "outputFolder=Enter the path to the output folder: "
set "pdftkExe=C:\Program Files\PDFtk Server\bin\pdftk.exe"
if not exist "%pdftkExe%" (
echo PDFTK is not installed. Please install PDFTK Server from the following link:
echo
pause
exit
)
set "pdf1=%folder1%\PDF1.pdf"
set "pdf2=%folder2%\PDF2.pdf"
if not exist "%pdf1%" (
echo The first PDF file was not found in the specified folder.
pause
exit
)
if not exist "%pdf2%" (
echo The second PDF file was not found in the specified folder.
pause
exit
)
"%pdftkExe%" "%pdf1%" "%pdf2%" output "%outputFolder%\merged.pdf"
echo The merged PDF file has been saved to %outputFolder%\merged.pdf
pause
endlocal
Explanation of the Enhanced Batch File
- The user is prompted to enter the paths to the first folder, the second folder, and the output folder.
- The batch file checks if PDFTK is installed and, if not, provides a link to download it.
- The batch file checks if the input PDF files exist in the specified folders.
- The batch file merges the two PDFs using PDFTK and saves the result in the specified output folder.
- We have provided an enhanced Windows batch file that can merge two PDFs from separate folders and handle more general cases.
- The batch file prompts the user to enter the paths to the input and output folders and checks if the input PDF files exist.
- The batch file uses PDFTK to merge the two PDFs and saves the result in the specified output folder.