Automatically merge pdf based on file existence

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Automatically merge pdf based on file existence

#16 Post by foxidrive » 15 Jul 2012 09:34

I don't want to annoy the guys who have already contributed but this is how I'd do it and hopefully it is simpler to follow.

Code: Select all

@echo off
cls
Color 0E
Title PDF Merging Batch
rem enter the folder where the files are stored
pushd "c:\temp"

:start
if exist "Report 1.pdf" if exist "Report 2.pdf" if exist "Report 3.pdf" (
rem wait for 30 seconds for the files to be fully transferred
ping -n 30 localhost >nul
goto :files_present
)
ping -n 30 localhost >nul
goto :start

:files_present

pdftk A="Report 1.pdf" B="Report 2.pdf" C="Report 3.pdf" cat A2 B C7 output "Main Report.pdf"

rem wait, just in case the file isn't complete.
ping -n 30 localhost >nul

md "new" 2>nul
md "old files" 2>nul

for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set timestamp=%dt:~0,8%_%dt:~8,6%

copy /b "Main Report.pdf" "old files\Main Report_%timestamp%.pdf"
move "Main Report.pdf" "new"

rem remove the old files and loop to the start again
del "Report ?.pdf"

goto :start
Last edited by foxidrive on 15 Jul 2012 09:52, edited 3 times in total.

rjobaan
Posts: 30
Joined: 14 Jul 2012 11:16

Re: Automatically merge pdf based on file existence

#17 Post by rjobaan » 15 Jul 2012 09:39

i will look into your changes abc0502. thank you very much.

foxidrive, looks good, i will use some of it.
I know have to make the change in my code first.


Is there maybe another open source tool which also merge the bookmarks with it?

Otherwise i need to add the bookmarks myself, but that will give other issues.
Last edited by rjobaan on 15 Jul 2012 09:44, edited 2 times in total.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Automatically merge pdf based on file existence

#18 Post by abc0502 » 15 Jul 2012 09:41

hi, foxidrive, very nice code but it keep repeat it self more and more, I think you shuld replace the last command

Code: Select all

goto :start

with pause or goto eof to stop the repeat
by the way thanks i never understood the pushd command till now :)


and rjobaan have alook at this http://www.coolutils.com/CommandLine/PDFCombine
Last edited by abc0502 on 15 Jul 2012 09:50, edited 1 time in total.

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Automatically merge pdf based on file existence

#19 Post by foxidrive » 15 Jul 2012 09:44

I changed it just now to give a better timestamp.


And yes, the three files need to be removed or it will keep looping immediately. I assumed that the process was to be repeated.

Does the PDFTK process delete the three files? I was confused when you said that the 'bookmarks' are deleted.
I'm not sure which bookmarks you refer to.
Last edited by foxidrive on 15 Jul 2012 09:47, edited 1 time in total.

rjobaan
Posts: 30
Joined: 14 Jul 2012 11:16

Re: Automatically merge pdf based on file existence

#20 Post by rjobaan » 15 Jul 2012 09:45

abc0502 wrote:by the way thanks i never understood the pushd command till now :)


How does the pushd works? why is it this why clear?
it just sets the location right?

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Automatically merge pdf based on file existence

#21 Post by foxidrive » 15 Jul 2012 09:48

Pushd "c:\temp"
changes the working directory to "C:\temp"

It works across drives and also on networks.


It really pushes the directory onto a stack and so the matching POPD needs to be executed at the end of the routine, if you are leaving the folder.

(Push and Pop are also assembler commands for pushing memory_locations (where to return to in the code) onto and off of a memory stack)

rjobaan
Posts: 30
Joined: 14 Jul 2012 11:16

Re: Automatically merge pdf based on file existence

#22 Post by rjobaan » 15 Jul 2012 11:07

foxidrive wrote:Does the PDFTK process delete the three files? I was confused when you said that the 'bookmarks' are deleted.
I'm not sure which bookmarks you refer to.


Each pdf has its own bookmark, but when merging to one pdf with pdftk the bookmarks are not merge with it.

rjobaan
Posts: 30
Joined: 14 Jul 2012 11:16

Re: Automatically merge pdf based on file existence

#23 Post by rjobaan » 15 Jul 2012 11:11

My new code (mix of abc en foxidrive)

@echo off

cls
Color 0E
Title PDF Merging Batch

:: Any Changes Happen Here >>
set "folder=C:\TEMP"
set "file_loc=C:\TEMP\NEW
set "file_1=Report 1.pdf"
set "file_2=Report 2.pdf"
set "file_3=Report 3.pdf"
set "main_file=Main Report.pdf"
set "old_folder=C:\TEMP\old"

:: Main Code >>
IF not Exist "%file_1%" ( Echo File %file_1% is not present.) else ( IF not Exist "%file_2%" ( Echo File %file_2%

is not present.) else ( IF not Exist "%file_3%" ( Echo File %file_3% is not present.) else (pdftk A="%folder%\

%file_1%" B="%folder%\%file_2%" C="%folder%\%file_3%" cat A2 B C7 output "%main_file%")
))

::Create timestamp >>

for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set timestamp=%dt:~0,8%_%dt:~8,6%

:: Adding timestamp to old file >>

Copy /Y "%file_loc%\%main_file%" "%old_folder%\Main_file - %timestamp%.pdf"

::Move new created file into new folder>>
move "%folder%\%main_file%" "%file_loc%
pause


Now i will look if PDFCombine will do the tric regarding the bookmarks.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Automatically merge pdf based on file existence

#24 Post by abc0502 » 15 Jul 2012 11:30

are you sure your code is working ?!

when you check to see if a %file_1% exist or not it won't work unless the batch n the same folder with the files, to make your code work from any location ether use the pushd command foxidrive used in his batch or put %folder%\ before each %file_1 ,2 or 3%

rjobaan
Posts: 30
Joined: 14 Jul 2012 11:16

Re: Automatically merge pdf based on file existence

#25 Post by rjobaan » 15 Jul 2012 11:41

sorry i copied old batch file.

i indeed have
:: Main Code >>
IF not Exist "%folder%\%file_1%" ( Echo File %file_1% is not present.) else ( IF not Exist "%folder%\%file_2%" (

Echo File %file_2% is not present.) else ( IF not Exist "%folder%\%file_3%" ( Echo File %file_3% is not present.)

else (pdftk A="%folder%\%file_1%" B="%folder%\%file_2%" C="%folder%\%file_3%" cat A2 B C7 output "%main_file%")
))

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Automatically merge pdf based on file existence

#26 Post by abc0502 » 16 Jul 2012 08:24

I found what you exactly searching for here: http://sourceforge.net/projects/pdfmerge/

Read the Discreption

Edit: Read the readme.doc file in the program folder for usage

rjobaan
Posts: 30
Joined: 14 Jul 2012 11:16

Re: Automatically merge pdf based on file existence

#27 Post by rjobaan » 16 Jul 2012 12:55

wow, this looks really great. This will save me a lot of code. But first check if its working.

I know trying to figure how to use in a batch file.....

I still think i need to create the <cmd_file_name> right?
how can i do that

can you help me out?
See also my last post.
im wondering if that is possible with pdfmerge.

Post Reply