Jump to content

Merge PDF Files


abenitez77

Recommended Posts

I have this code that I got from "igor krupitsky which he posted on another site. It merges (appends) 2 pdf filesicon1.png. I need to edit the code so that it does this:I have 8,000 files in a folder and I want to merge the files that have the same prefix name in the filename. i.e.122TX4939.pdf122TX4939 Support.pdf122TX4939 Additional.pdf333RS111.pdf333RS111 Support.pdf555DA77.pdfResults:The first 3 would get merged into 1 fileicon1.png:122TX4939.pdfThe next 2 would get merged into 1 file:333RS111.pdfThe last file would get copied or merged byitself555DA77.pdfand they would be in a destination folder other than the folder with the 8,000 pdf files.Code:Set fso = CreateObject("Scripting.FileSystemObject")sFolder = fso.GetParentFolderName(WScript.ScriptFullName)Set oFolder = fso.GetFolder(sFolder)Set oArgs = WScript.ArgumentsIf oArgs.Count = 0 Then'Double ClickMergeFilesElse'Drag & DropFor I = 0 to oArgs.Count - 1If LCase(Right(oArgs(I), 4)) = ".pdf" ThenMergeTwoFiles oArgs(I)End IfNextEnd If'=======================================================Sub MergeFiles()bFirstDoc = TrueIf oFolder.Files.Count < 2 ThenMsgBox "You need to have at least two PDF files in the same folder to merge."'fso.CopyFile(oFolder.Files.Name, oFolder & "Results")Exit SubEnd IfFor Each oFile In oFolder.FilesIf LCase(Right(oFile.Name, 4)) = ".pdf" ThenIf bFirstDoc ThenbFirstDoc = FalseSet oMainDoc = CreateObject("AcroExch.PDDoc")oMainDoc.Open sFolder & "" & oFile.NameElseSet oTempDoc = CreateObject("AcroExch.PDDoc")oTempDoc.Open sFolder & "" & oFile.NameoMainDoc.InsertPages oMainDoc.GetNumPages - 1, oTempDoc, 0, oTempDoc.GetNumPages, FalseoTempDoc.CloseEnd IfEnd IfNextoMainDoc.Saveicon1.png 1, sFolder & "Output.pdf"oMainDoc.CloseMsgBox "Done! See Output.pdf file."End Sub'=======================================================Sub MergeTwoFiles(sFileName)If Not fso.FileExists(sFolder & "Output.pdf") Thenfso.CopyFile sFileName, sFolder & "Output.pdf"Exit SubEnd IfSet oMainDoc = CreateObject("AcroExch.PDDoc")oMainDoc.Open sFolder & "Output.pdf"Set oTempDoc = CreateObject("AcroExch.PDDoc")oTempDoc.Open sFileNameoMainDoc.InsertPages oMainDoc.GetNumPages - 1, oTempDoc, 0, oTempDoc.GetNumPages, FalseoMainDoc.Save 1, sFolder & "Output.pdf"oTempDoc.CloseoMainDoc.CloseMsgBox "Done! See Output.pdf file."End Sub

Link to comment
Share on other sites

Take it in steps. First, decide if you want to hard-code the source and destination folder names, or if you want those as arguments to the script. One step is to open the folder and get a list of all of the files. For each file you'll need to strip the extension to get the base name and then go loop through the list of files again to check if any other filename starts with the base name of the other file. One option is to use the instr function to check if the filename contains the other name, but that's not necessarily going to tell you if the filename starts with the other filename. If you want to check if it starts with the other name then you need to get the length of the name and then the left X characters from the other filename, and see if they're the same. You can use the len and left functions for that. Once that is done, then for each file you should end up with a list of other files that match it (make sure to not include the original file in that list), and then you can use the code above to loop through that list of files and add the pages from each one to the main one, and then save it out as a new file in the destination folder. If there aren't any matches then you can skip the step where you combine them and just save the file to the destination folder.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...