Automatically merge pdf based on file existence

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Automatically merge pdf based on file existence

#1 Post by rjobaan » 14 Jul 2012 11:24

I have the follow challenge:

I have three pdf files which are stored on a file location for example C:\Report

Report 1.pdf
Report 2.pdf
Report 3.pdf

Now i need to merge all three pdf's into one with name Main Report.pdf.
But the main report.pdf needs to be created as soon as all files exist in C:\Report.

Is this possible with a batch file?
Hopefully someone can help me out.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Automatically merge pdf based on file existence

#2 Post by Fawers » 14 Jul 2012 11:54

I don't know if copy can do the trick, but try it.

Code: Select all

copy /b report*.pdf "Main Report.pdf"

Edit:
Nope, won't work.
Last edited by Fawers on 14 Jul 2012 12:02, edited 1 time in total.

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

Re: Automatically merge pdf based on file existence

#3 Post by rjobaan » 14 Jul 2012 12:00

I first need to check if all three files exists before creating the main report.pdf.

How can i do that?

Im new in this ( i worked with batch file in my past, so i have to start remember things again)

Where do i need to copy the code into?

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Automatically merge pdf based on file existence

#4 Post by Fawers » 14 Jul 2012 12:06

To see if they are there, you just have to use IF EXIST statements.

Code: Select all

@echo off
pushd "%pdfpath%"
::%pdfpath% should be the path containing your PDFs
set pdf=0
for %%n in (1 2 3) do if exist "report %%n.pdf" set /a pdf+=1
if %pdf% GTR 0 (echo There are %pdf% Report files in the folder.) else echo There aren't any report PDF files in the folder.

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

Re: Automatically merge pdf based on file existence

#5 Post by rjobaan » 14 Jul 2012 12:38

Fawers wrote:I don't know if copy can do the trick, but try it.

Code: Select all

copy /b report*.pdf "Main Report.pdf"

Edit:
Nope, won't work.


Yes i also just found out.
Any other ideas?

I also tried copy "Report 1.pdf" + "Report 2.pdf" "Main Report.pdf" but that didnt do the trick i think the copy command is not working with pdf files.
I can not find any where that is should not work, but alle examples are talking about txt files.

I found the solution for merging pdf file. Its even open source.
I downloaded: http://www.pdflabs.com
and added the following command
pdftk "Report 1.pdf" "Report 2.pdf" "Report 3.pdf" cat output "Main Report".pdf

but the now the file only needs to be created if all three files exists, how can i do that?

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

Re: Automatically merge pdf based on file existence

#6 Post by abc0502 » 14 Jul 2012 18:08

You can use Fawers's Code "IF the folder report will always have 3 pdf files ONLY" and then add your line to the end of it like that

@echo off
set "pdfpath=C:\Report"
pushd "%pdfpath%"
::%pdfpath% should be the path containing your PDFs
set pdf=0
for %%n in (1 2 3) do if exist "report %%n.pdf" set /a pdf+=1
if %pdf% EQU 3 (pdftk "Report 1.pdf" "Report 2.pdf" "Report 3.pdf" cat output "Main Report".pdf) else echo There aren't any report PDF files in the folder. &pause >nul
I made few changes it's in green and added your command and change the location in blue to suite your folder location

Edit
Fawer made the code to search for pdf files called report 1.pdf, report 2.pdf and report 3.pdf so if the names changed you will need a new code.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Automatically merge pdf based on file existence

#7 Post by Fawers » 14 Jul 2012 18:31

It looks better if you use "EQU 3" instead of "GTR 0", just like abc said.

And, yes, you'll have to change the code if the names are not "report x.pdf", where x can be 1, 2, or 3.

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

Re: Automatically merge pdf based on file existence

#8 Post by abc0502 » 14 Jul 2012 18:43

Hi, try this one this should work on any file names or location after small change you make in the variable section.
All changes you might want to do are written in RED color

@echo off
cls
Color 0E
Title PDF Merging Batch

:: Any Changes Happen Here >
set "pdftk=location_of_the_application_that_merge_files"
set "folder=D:\Report"
set "file_1=%folder%\Report 1.pdf"
set "file_2=%folder%\Report 2.pdf"
set "file_3=%folder%\Report 3.pdf"
set "main=%folder%\Main Report.pdf"

:: Main Code >>
IF not Exist "%file_1%" ( Echo File is Missing. &pause
) Else ( IF not Exist "%file_2%" ( Echo File is Missing. &pause
) Else ( IF not Exist "%file_3%" ( Echo File is Missing. &pause
) Else ( %pdftk% "%file_1%" "%file_2%" "%file_3%" cat output "%main%) ) )


It's a long code but simple in case you couldn't change Fawers's Code.

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

Re: Automatically merge pdf based on file existence

#9 Post by rjobaan » 15 Jul 2012 05:09

thanks for you help so far. Im really learning fast this way.

I noticed when using pdftk the bookmark of the original pdf files are lost? Any idea how come?

And is it possible to send email of certain files are missing?

What does &pause means icm with pause?

Squashman
Expert
Posts: 4011
Joined: 23 Dec 2011 13:59

Re: Automatically merge pdf based on file existence

#10 Post by Squashman » 15 Jul 2012 05:53

rjobaan wrote:thanks for you help so far. Im really learning fast this way.

I noticed when using pdftk the bookmark of the original pdf files are lost? Any idea how come?

And is it possible to send email of certain files are missing?

What does &pause means icm with pause?

The ampersand is used to execute another command after the previous command on the same line. So after the echo it the ampersand then lets the PAUSE command execute.

There is no native way in batch files to send email. You will need a 3rd party command line email client to do that.

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

Re: Automatically merge pdf based on file existence

#11 Post by rjobaan » 15 Jul 2012 06:12

Current code is like:
@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 files"

:: 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%")
))


IF not Exist "%folder%\%main_file%" (Echo "%main_file%" file is not created.) else (move "%folder%\%main_file%" "%file_loc%)

pause

Questions:

1 But the last line is not working.I want to move the file only when the file exists. What am i doing wrong?
2 And Before moving i need to create backup of file in %file_loc%.
So i need to check if %main_file% is already available in %file_loc%, if so move that file to %old_folder% and add time stamp to filename %main_file%

3 And does any one has an idea when using pdftk the bookmark of the original pdf files are lost?

thanks for your help guys!

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

Re: Automatically merge pdf based on file existence

#12 Post by foxidrive » 15 Jul 2012 07:25

Squashman wrote:There is no native way in batch files to send email. You will need a 3rd party command line email client to do that.


There is one. I've had the code since a 2007 Newsgroup posting but didn't ever look closely... it has been tested in XP and Win 7

Here's a modified version for a simple way to email just a short message/attachment. See alt.msdos.batch.nt and a recent "Send email via batch file" thread for more options, in a post by Timo Salmi who links to his FAQ on the issue.


Code: Select all

::email-bat.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal

:: defaults
set From=me@here.com.au
set To=you@lavabit.com
set Subj="email test   %date% %time%"
set Body="did it work? %date% %time%"
set Serv=mail.server.com.au
set Auth=user
set Pass=pass
set fileattach=

:: if command line arguments are supplied then use them
if "%~7" NEQ "" (
set From=%1
set To=%2
set Subj="%~3"
set Body="%~4"
set Serv=%5
set "Auth=%~6"
set "Pass=%~7"
set "fileattach=%~8"
)

call :createVBS "email-bat.vbs"

call :send %From% %To% %Subj% %Body% %Serv% %Auth% %Pass%
echo email sent (if parameters were correct)
pause
del "%vbsfile%" 2>nul
goto :EOF

:send
cscript.exe /nologo "%vbsfile%" %1 %2 %3 %4 %5 %6 %7
goto :EOF

:createVBS
set "vbsfile=%~1"
del "%vbsfile%" 2>nul
set cdoSchema=http://schemas.microsoft.com/cdo/configuration
echo >>"%vbsfile%" Set objArgs       = WScript.Arguments
echo >>"%vbsfile%" Set objEmail      = CreateObject("CDO.Message")
echo >>"%vbsfile%" objEmail.From     = objArgs(0)
echo >>"%vbsfile%" objEmail.To       = objArgs(1)
echo >>"%vbsfile%" objEmail.Subject  = objArgs(2)
echo >>"%vbsfile%" objEmail.Textbody = objArgs(3)
if defined fileattach echo >>"%vbsfile%" objEmail.AddAttachment "%fileattach%"
echo >>"%vbsfile%" with objEmail.Configuration.Fields
echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendusing")        = 2 ' not local, smtp
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpserver")       = objArgs(4)
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpserverport")   = 25
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpauthenticate") = 1 ' cdobasic
echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendusername")     = objArgs(5)
echo >>"%vbsfile%"  .Item ("%cdoSchema%/sendpassword")     = objArgs(6)
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpusessl")       = False
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpconnectiontimeout") = 25
echo >>"%vbsfile%"  .Update
echo >>"%vbsfile%" end with
echo >>"%vbsfile%" objEmail.Send

Squashman
Expert
Posts: 4011
Joined: 23 Dec 2011 13:59

Re: Automatically merge pdf based on file existence

#13 Post by Squashman » 15 Jul 2012 08:09

I got try that out one of these days.

Squashman
Expert
Posts: 4011
Joined: 23 Dec 2011 13:59

Re: Automatically merge pdf based on file existence

#14 Post by Squashman » 15 Jul 2012 08:09

Ed Dyreen wrote:'
banned billrich for reason double posting, user had been warned to respect the forum rules.

How about just for giving bad advice.

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

Re: Automatically merge pdf based on file existence

#15 Post by abc0502 » 15 Jul 2012 09:05

rjobaan wrote:Current code is like:
@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 files"

:: 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%")
))


IF not Exist "%folder%\%main_file%" (Echo "%main_file%" file is not created.) else (move "%folder%\%main_file%" "%file_loc%)

pause

Questions:

1 But the last line is not working.I want to move the file only when the file exists. What am i doing wrong?
2 And Before moving i need to create backup of file in %file_loc%.
So i need to check if %main_file% is already available in %file_loc%, if so move that file to %old_folder% and add time stamp to filename %main_file%

3 And does any one has an idea when using pdftk the bookmark of the original pdf files are lost?

thanks for your help guys!


First there is few mistakes:
1> you didn't set the file_1, 2 & 3 location in the variable section, so
insted of
set "file_2=Report 2.pdf"
it should be
set "file_2=%folder%\Report 2.pdf"

or fix that in the code it self by changeing
IF not Exist "%file_1%" to IF not Exist "%folder%\%file_1%"

and so is the rest of the code.

2> the last line just reverse the condition to be like that:
IF Exist "%folder%\%main_file%" ( move "%folder%\%main_file%" "%file_loc% ) else ( Echo "%main_file%" file is not created. )

and leave space between the "(" and the "next command" like the code above.

About the lost bookmark, you create a whole new pdf so the bookmark will be lost but if the tool have a command that can copy the bookmark to the new file it should work.

About The back up copy of the main_file, you make the main_file in the temp directory then move the file to the New folder but how you want to make a back up copy in the same folder ?, i don't understand

Edit
One more thing to make a back up copy and add date stamp this code should work
Copy /Y "%main_file%" "%old_folder%\Main_file - %date:/=%.pdf"

the variable in red will add the date to the file name

Post Reply