Page 1 of 1

Merge multiple pdf files in sub folder

Posted: 31 Dec 2013 14:43
by zagix
Hello,

This is my situation and i want to merge multiple pdf files in the subfolder and named it on todays date.
After creation/merger from single file and named on todays date delete all other pdf's.
Will use PDFtk.exe

Folder structure:
c:database\001\todays date(format 01-01-2014)\download\PDF\*.pdf
c:database\002\todays date(format 01-01-2014)\download\PDF\*.pdf
c:database\003\todays date(format 01-01-2014)\download\PDF\*.pdf
.....
....
I want automate it.

The below codes are working but i have to manually enter each sub folder and select the pdf files and create a merged pdf and delete the single ones.

    Using the below codes.
    Open ..\pdftk\bin* and copy pdftk.exe and libiconv2.dll to a folder of your choice
    create a new text file and paste the following code
    create a shortcut of this .cmd and place it in your sendto folder
    Go to a folder with some PDF files and select as many PDFs as you want
    Right-click them and choose the shortcut you just created
    a new binder PDF in the same folder will be created where all selected PDFs are merged into one

Code: Select all

@echo off
setlocal enabledelayedexpansion
FOR %%A IN (%*) DO (set command=!command! %%A)
pdftk.exe %command% cat output "%~dp1binder.pdf"

Re: Merge multiple pdf files in sub folder

Posted: 31 Dec 2013 18:58
by penpen
I have some problems in understanding what you want to do (or why), so i give no complete solution (as i can't serve it).
zagix wrote:The below codes are working but i have to manually enter each sub folder and select the pdf files and create a merged pdf and delete the single ones.
You should not delete the single files automatically, as there may occure errors, so you should better check the result:
i don't know a way to do this automatically, and i fear there is none.

zagix wrote:Open ..\pdftk\bin* and copy pdftk.exe and libiconv2.dll to a folder of your choice
Why do you want to copy the executables every time you want to merge the files?
And what do you mean with the notation for the source folder "..\pdftk\bin*"
(do you want to erxecute it on multiple system and it may be found within different locations, or
do you have multiple versions and want to use the most up to date on)?

zagix wrote:create a new text file and paste the following code
create a shortcut of this .cmd and place it in your sendto folder
I assume you meant the codeblock at the end of your post,
why don't you just place/copy the .cmd file just once to your sendto folder
(again multiple systems, or why else)?
Assumed the Sendto folder is located in your userprofile, thats name does not contain any special character:

Code: Select all

(
   echo @echo off
   echo setlocal enabledelayedexpansion
::   the next line should be added if !command! should not be contained in the variable command
::   set "command= "
   echo FOR %%%%A IN ^(%%*^) DO ^(set command=!command! %%%%A^)
   echo pdftk.exe %%command%% cat output "%%~dp1binder.pdf"
) > "%USERPROFILE%\SendTo\whatNameYouWant.cmd"

zagix wrote:Go to a folder with some PDF files and select as many PDFs as you want
Right-click them and choose the shortcut you just created
This cannot be automated, as no algorithm can predict your choice reliably,
so i assume, you want to do this still manually:
If you have created the cmd within the SendTo folder then just right click and choose SendTo-> .cmd file.

zagix wrote:The below codes are working but i have to manually enter each sub folder and select the pdf files and create a merged pdf and delete the single ones.

(...)

a new binder PDF in the same folder will be created where all selected PDFs are merged into one
I assume this is something that you expect to happen, and nothing you want to do actively: may i misunderstand you?

Additional:
Please note that the maximum command line length is limited, for example XP uses a limit of 8191 characters:
I am not firm with your tool "pdftk.exe",
maybe it supports the input of a text file with a list of pdf files, or
maybe it supports passing the name of the files per piping to STDIN.
These are the default ways to get around such a limitation.
If it does not support such things, you have to control the line length and use tempFiles with in-between results,
or you even may abort the operation with an error message.

penpen

Re: Merge multiple pdf files in sub folder

Posted: 31 Dec 2013 23:03
by zagix
Thanks Penpen for the reply.

I am sorry to say, but you misunderstood my question the top part describes what i want to achieve and the below part is what i am using right now (a sample). Let us live all that and i put forward this question. I apologize.

This is my situation and i want to merge multiple pdf files in the subfolder and named it on todays date.
After creation/merger from single file and named on todays date delete all other pdf's.
Will use PDFtk.exe

Folder structure:
c:database\001\todays date(format 01-01-2014)\download\PDF\*.pdf
c:database\002\todays date(format 01-01-2014)\download\PDF\*.pdf
c:database\003\todays date(format 01-01-2014)\download\PDF\*.pdf
.....
....
I want to automate it.

Re: Merge multiple pdf files in sub folder

Posted: 01 Jan 2014 04:01
by foxidrive
Do all folders have today's date in them?

What format is the date? MM-DD-YYYY or DD-MM-YYYY?

Re: Merge multiple pdf files in sub folder

Posted: 01 Jan 2014 06:12
by penpen
So as far as i understand, you may be searching for something like that:

Code: Select all

:: this batch only works from the location you have specified
@echo off
setlocal enableDelayedExpansion

:: you have to change this path: What do you mean with the notation for the source folder "..\pdftk\bin*" (see my last post)
:: (* is no allowed path character)
set "pdftkPath=..\pdftk\bin*"

:: you may have to change this, as the date format is unknown (see foxidrives post).
:: assumed !date! gives you the date in the format MM.DD.YYYY, and you want the result in this format MM-DD-YYYY
set "todaysDate=!date:.=-!"

:: maybe you have to change this too, if your initial path differs
set "databasePath=c:database"

for /F "tokens=* delims=" %%a in ('dir "!databasePath!" /B /A:D ^| findstr "^[0-9][0-9][0-9]$"') do (
::   this assumes that the maximum line limit will not be reached, maybe you have to check it (see my last post).
   set "command= "
   for "tokens=* delims=" %%B in ('dir /B /A:-D "!databasePath!\%%~a\download\PDF\*.pdf" ^| findstr /I "[.]pdf$"') do (
::      i'm not firm with pdftk.exe, so you may add the volume and path to each file, too
::      set "command=!command! "!databasePath!\%%~a\download\PDF\%%~B""
::      instead of doing just this
      set "command=!command! "%%~B""
   )
   "!pdftkPath!\pdftk.exe" !command! cat output "!databasePath!\%%~a\download\PDF\binder.pdf"
)

endlocal
goto :eof

penpen

Edit: Bug Removed: I have escaped the '|' char within the for-'command' part of both for loops.

Re: Merge multiple pdf files in sub folder

Posted: 01 Jan 2014 13:03
by zagix
foxidrive wrote:Do all folders have today's date in them?

What format is the date? MM-DD-YYYY or DD-MM-YYYY?


Thanks for response, Foxidrive.
Yes all the folders have today's date in them, and they are some what like this.

c:\database\001\todays date\download\PDF\*.pdf
c:\database\002\todays date\download\PDF\*.pdf
....
...
The format of date is DD-MM-YYYY

With reference to Penpen reply it shows an error "| was unexpected at this time." and he has skipped the folder after database\? ?

Foxidrive please have a look.

Re: Merge multiple pdf files in sub folder

Posted: 01 Jan 2014 14:08
by penpen
I've removed the bug in the above code with the unescaped '|' character.
Sorry for that, actually i write from mobile phone, and i am currently not able to test it.
Is the assumption, that you just want to replace each '.' within the date variable with a '-'?
And please post, if the assumptions, i have made are correct, so i can remove the comments starting with a paamayim nekudotayim.

penpen

Re: Merge multiple pdf files in sub folder

Posted: 01 Jan 2014 17:56
by foxidrive
"pathto\pdftk.exe" needs changing. It may work, but test it on some sample files first.


Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "today=%DD%-%MM%-%YYYY%"
for /d %%a in ("c:\database\*") do (
   pushd "%%~a\%today%\download\PDF\"
      "pathto\pdftk.exe" *.pdf cat output "%today%.pdf.bin"
      del *.pdf
      ren "%today%.pdf.bin" "%today%.pdf"
   popd
)

Re: Merge multiple pdf files in sub folder

Posted: 01 Jan 2014 23:12
by zagix
Hello,

Thanks foxidrive, SUPERB!

Re: Merge multiple pdf files in sub folder

Posted: 25 Mar 2014 00:40
by Nana111
HI there
For me ,i have googled a code for
merge pdf files :


public void CombineDocumentsToStream(Stream s, List<BaseDocument> docList)
{
PDFDocument.Combine(docList, s);
}

public void CombineDocumentAndSaveItToFile(List<BaseDocument> docList, String destFilePath)
{
PDFDocument.Combine(docList, destFilePath);
}
But it is hard for me to do that using a code.Is there any powerful
PDF program which supports to merge pdf files directly.Thanks a lot.