Link to home
Start Free TrialLog in
Avatar of creativefusion
creativefusionFlag for Australia

asked on

Cmd Batch Print PDF Files Then Rename Them After Finishing

Hi All,

I've got a folder in MyDocuments called "DAILY". It contains anything from 1 - 30 PDF files of which each file has a different name. i.e 45660JJH.pdf

I am wanting to achieve the following:

1. Run a cmd script from my desktop
2. Automatically print each new file found
3. After printing each file, save a copy with the original file name and "_PRINTED_&_DateTimeStamp" at the end of each file. The above example would look like 45660JJH_PRINTED_19012011_15:45

I am using acrobat 8 and windows 2003 office XP pro

Is this possile and if so, can anyone help me?

Thanks
CF

Avatar of Bill Prew
Bill Prew

What program do you use for viewing PDF files? (Acrobat Reader, Foxit Reader, ...)

~bp
Sorry, I see that you said Acrobat Reader 8, my bad.  Can you do the following two commands at a command prompt and tell me that they display?

ECHO %DATE%

ECHO %TIME%

~bp
Avatar of creativefusion

ASKER

Hi BP,

Thanks for the reply. We might be out of sync with time its here 8:00am here in Sydney.

Okay, I did as you asked and I got the date and time.

CF
Good morning!

Yes, I was looking for the exact format of the displayed date and time, I suspect your format may differ slightly from mine, so I need to adjust the script for that.

Also, can you try the following, can you open a windows command window and then try the following:

"C:\Program Files\Adobe\Acrobat 9.0\Acrobat\AcroRd32.exe"  /N /T "yourfile.pdf" "YourPrinterName"

Adjusting the path to your installation of adobe reader, as well as the name of a small PDF file, and the name of a printer.  I want to confirm that this syntax prints the PDF from the command line on your system.

~bp
Hi BP,

Great!!

Date format is Thu 20/01/2011; Time format is 8:58:49.06 note the "." in the seconds..

"Also, can you try the following, can you open a windows command window and then try the following:"

C:\Program Files\Adobe\Acrobat 9.0\Acrobat\AcroRd32.exe

Does not work. It errors with a program not recognized. Now, I went into my program files and found the folder that Acrobat is sitting in is named "Reader 9". In my installed programs, it shows as Adobe Reader 9.4.0

" /N /T "yourfile.pdf"
\\nnorcl2usr2\usr2mount3$\u2m3\nsjso\My Documents\DAILY\test123.pdf

"YourPrinterName"
\\NPENSOGFIS001\NPSO01B4MFDCOL

Let me know if there is anything else you need. Thanks again.

CF




Also, here is the correct mapping to my PDF reader:

C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe

When I type it in the command prompt, it provides the same error as before...

CF
Try this, you need to quote the adobe exe path since it has spaces.

"C:\Program Files\Adobe\Acrobat 9.0\Acrobat\AcroRd32.exe" /N /T "\\nnorcl2usr2\usr2mount3$\u2m3\nsjso\My Documents\DAILY\test123.pdf" "\\NPENSOGFIS001\NPSO01B4MFDCOL"

~bp
Hi BP,

Thanks.

So I entered this first part in my cmd window - C:\Program Files\Adobe\Acrobat 9.0\Acrobat\AcroRd32.exe and got the same error. Program is not recognized as an internal or external command, operable program of bacth file.

Do I need to add quotes? Or anything else?

Sorry mate, I'm only new at this!!

CF
No problem.

Copy this single line from below and paste it into the command line all at once.

"C:\Program Files\Adobe\Acrobat 9.0\Acrobat\AcroRd32.exe" /N /T "\\nnorcl2usr2\usr2mount3$\u2m3\nsjso\My Documents\DAILY\test123.pdf" "\\NPENSOGFIS001\NPSO01B4MFDCOL"

Open in new window

~bp
Okay I did exactly that but got the following error..

"The system cannot find the path specified."

Off to a meeting now and will be absent for about an hour. Whats your time zone?

CF

8:11 pm here.

~bp
Hi Bill,

Have you got any idea whats going wrong?

CF
I used the @Echo off statement prior as well...
@Echo off
"C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe"

It starts the reader for me...

CF
I tried a test here and the following line:

"C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe" /N /T "X:\temp\IMPORTANT CS3.3 Upgrade README.pdf"

Open in new window


and it did print the document to my default printer.

Try doing a command line like this and see if it opens your doc:

"C:\Program Files\Adobe\Acrobat 9.0\Acrobat\AcroRd32.exe" "\\nnorcl2usr2\usr2mount3$\u2m3\nsjso\My Documents\DAILY\test123.pdf"

Open in new window

~bp
Hi Bill

I am playing around with the path mapping here to see if I can get it to work but it does not pick up the file path mapping. I am on a network and using the correct file paths.

CF

Here is the mapping that I have for it:

"C:\Program Files\Adobe\Acrobat 9.0\Acrobat\AcroRd32.exe" "\\nyenad03fs002\share\Replenishment\test123.pdf"
Could be a "feature" of Adobe Reader :-(.

~bp
Hi Bill...

Phew.... What a mission....
This works...

"C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe" "\\nyenad03fs002\share\Replenishment\test123.pdf"

I am mindful of the time difference, where to from here?

CF
In your original post you mentioned a DAILY folder in My Documents.  Is the path you just mentioned a replacement for that, or just for testing?  Let me share the basic BAT script I have so far though, even though it might need a little adjusting.  See if the basic idea here makes sense.

@echo off

REM Define paths and settings for script
set BaseDir=%USERPROFILE%\My Documents\Daily
set Printer=Duplex
set Acrobat=C:\Program Files\Adobe\Acrobat 9.0\Acrobat\AcroRd32.exe

REM Build time stamp to add to end of files processed (YYYYMMDD_HHMMSS)
set Stamp=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%

REM Process all files in folder that have not already been processed
for /F "tokens=*" %%A in ('dir /a-d /b "%BaseDir%\*.pdf" ^| find /i /v "_PRINTED_"') do (

  REM Launch PDF program with print options
  "%Acrobat%"  /N /T "%BaseDir%\%%A" "%Printer%"

  REM Rename file to indicate processed
  ren "%BaseDir%\%%A" "%%~nA_PRINTED_%Stamp%.pdf"

  REM End the PDF print program (doesn't close on its own after printing)
  taskkill /IM AcroRd32.exe
)

Open in new window

~bp
Hi Bill,

Wow, so cool...Thanks

It makes sense and the rem statements help identify what each function is up to.

Now, to your question about the folder in the file path, yes this is correct. I have just adjusted this for testing only given the issues we have been having!! When the solution is finalized, I will switch to the correct folder path. Or would you prefer I give the correct path to you now?

CF
Feel free to adjust the path in that BAT file as needed and do some testing with it.  I'm not sure exactly what the printer name will need to be, you may have to experiment with that a bit on your system.

Got to call it a night here, will check for any updates in the morning.  It's 10:45 pm here currently, I should be online a little after 7:00 am my time tomorrow.

~bp
Any luck?

~bp
Hi Bill,

How are you today?

Okay, I did some mods to the file path and so forth but it crashes when Adobe Reader tries to open the document.

In the test code I sent you yesterday, it worked.

Enclosed is the current code I am using.

CF

@echo off

REM Define paths and settings for script
set BaseDir=%\\nyenad03fs002\share\Replenishment\DAILY\test123.pdf
set Printer=\\NPENSOGFIS001\NPSO01B4MFDCOL
set Acrobat=C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe

REM Build time stamp to add to end of files processed (YYYYMMDD_HHMMSS)
set Stamp=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%

REM Process all files in folder that have not already been processed
for /F "tokens=*" %%A in ('dir /a-d /b "%BaseDir%\*.pdf" ^| find /i /v "_PRINTED_"') do (

  REM Launch PDF program with print options
  "%Acrobat%"  /N /T "%BaseDir%\%%A" "%Printer%"

  REM Rename file to indicate processed
  ren "%BaseDir%\%%A" "%%~nA_PRINTED_%Stamp%.pdf"

  REM End the PDF print program (doesn't close on its own after printing)
  taskkill /IM AcroRd32.exe
)

Open in new window

The BaseDir line seems to have some issues.  First, it seems to have a stray % sign in it, don't want that.  And it also contains a PDF file name, but this setting should be just the folder name to search.  So I think you want to change:

set BaseDir=%\\nyenad03fs002\share\Replenishment\DAILY\test123.pdf

to:

set BaseDir=\\nyenad03fs002\share\Replenishment\DAILY

~bp
Hi Bill,

Thanks for pointing that out. Your right, we don't need to specify a file, just a folder.

I changed the dimensions and ran it using command prompt but it errors on the reader saying "There was an error opening this document. This file cannot be found"

I copied the updated code for you as well.

CF
@echo off

REM Define paths and settings for script
set BaseDir=\\nyenad03fs002\share\Replenishment\DAILY
set Printer=%\\NPENSOGFIS001\NPSO01B4MFDCOL
set Acrobat=C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe

REM Build time stamp to add to end of files processed (YYYYMMDD_HHMMSS)
set Stamp=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%

REM Process all files in folder that have not already been processed
for /F "tokens=*" %%A in ('dir /a-d /b "%BaseDir%\*.pdf" ^| find /i /v "_PRINTED_"') do (

  REM Launch PDF program with print options
  "%Acrobat%"  /N /T "%BaseDir%\%%A" "%Printer%"

  REM Rename file to indicate processed
  ren "%BaseDir%\%%A" "%%~nA_PRINTED_%Stamp%.pdf"

  REM End the PDF print program (doesn't close on its own after printing)
  taskkill /IM AcroRd32.exe
)

Open in new window

Sorry Bill,

My bad, I copied the wrong code!!!

Must be Friday!! Lolzzzz


@echo off

REM Define paths and settings for script
set BaseDir=%\\nyenad03fs002\share\Replenishment\DAILY
set Printer=\\NPENSOGFIS001\NPSO01B4MFDCOL
set Acrobat=C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe

REM Build time stamp to add to end of files processed (YYYYMMDD_HHMMSS)
set Stamp=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%

REM Process all files in folder that have not already been processed
for /F "tokens=*" %%A in ('dir /a-d /b "%BaseDir%\*.pdf" ^| find /i /v "_PRINTED_"') do (

  REM Launch PDF program with print options
  "%Acrobat%"  /N /T "%BaseDir%\%%A" "%Printer%"

  REM Rename file to indicate processed
  ren "%BaseDir%\%%A" "%%~nA_PRINTED_%Stamp%.pdf"

  REM End the PDF print program (doesn't close on its own after printing)
  taskkill /IM AcroRd32.exe

Open in new window

Hi Bill,

Okay, with the code I just supplied you, the file is being renamed as we expected. The only thing that is not happening is the printing.

CF
Hi Bill,

It seems the "set Printer=\\NPENSOGFIS001\NPSO01B4MFDCOL" does not work.

What is the correct command to set a printer in DOS?

CF
The set Printer... line isn't doing anything fancy at the DOS level, it's just setting a variable to the name of the printer we want to print to.  It's then passed to the acrobat program as a command line parm in:

  "%Acrobat%"  /N /T "%BaseDir%\%%A" "%Printer%"

Unfortunately documentation on the command line is weak from Adobe, but I think the printer name needs to be the name you gave to the printer when you configured it in Windows on the system running the BAT file.  I doubt that is the network path to the printer as you are using.

Try this command and if it works it should list the possible printers on that system

wmic printer get name

~bp
Hi Bill,

After numerous tests and research, I am still struggling with getting this what appears to be a simple thing to work!!

It works with printing 1 document without renaming the files. As soon as I remove the file name, it craps out.

I have copied the current version for you to review.
@echo off

REM Define paths and settings for script
set DOCFullPath=\\nyenad03fs002\share\Replenishment\DAILY
set Acrobat=C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe

REM Print document
"%Acrobat%" /n /s /o /h /t "%DOCFullPath%" "%Printer%" "RICOH Aficio MP 3500 PCL 6" "NPSO01B4PRMFDCOL"

"%Acrobat%" /n "%DOCFullPath%"

REM Build time stamp to add to end of the files processed (YYYYMMDD_HHMMSS)
set Stamp=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%

REM Process all files within the folder that have not already been processed
for /F "tokens=*" %%A in ('dir /a-d /b "%BaseDir%\*.pdf" ^| find /i /v "_PRINTED_"') do (

EXIT

Open in new window

Hi Bill,

I've managed to get the printing sorted out however, there are two things wrong.

1. Acrobat is not closing after printing each file. I have to do this manually.
2. The file renaming method is not working.

Enclosed is the revised procedure.

Cheers,
Shaun
@echo off

REM Define paths and settings for script
set DOCFullPath=\\nyenad03fs002\share\Replenishment\DAILY
set Acrobat=C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe

REM Build time stamp to add to end of the files processed (YYYYMMDD_HHMMSS)
set Stamp=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%

REM Process all files within the folder that have not already been processed
for /F "tokens=*" %%A in ('dir /a-d /b "%DOCFullPath%\*.pdf" ^| find /i /v "_PRINTED_"') do (

REM Print document
"%Acrobat%" /n /s /o /h /t "%DOCFullPath%\%%A" "%Printer%" "RICOH Aficio MP 3500 PCL 6" "NPSO01B4PRMFDCOL"

)

EXIT

Open in new window

==> 1. Acrobat is not closing after printing each file. I have to do this manually.

Yes, that's why I added the taskkill line earlier, you might try putting that back now.

  REM End the PDF print program (doesn't close on its own after printing)
  taskkill /IM AcroRd32.exe

==> 2. The file renaming method is not working.

It looks like you removed that as well, we'll need to add that back.

  REM Rename file to indicate processed
  ren "%BaseDir%\%%A" "%%~nA_PRINTED_%Stamp%.pdf"

~bp
Hi Bill,

I got the renaming part to work now!! It was outside of the loop.

All we need to do is to get Acrobat reader to close each time a file is printed and renamed. Can you take one last look at it for a well deserved 500 points?

Cheers,
Shaun
@echo off

REM Define paths and settings for script
set DOCFullPath=\\nyenad03fs002\share\Replenishment\DAILY
set Acrobat=C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe

REM Build time stamp to add to end of the files processed (YYYYMMDD_HHMMSS)
set Stamp=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%

REM Process all files within the folder that have not already been processed
for /F "tokens=*" %%A in ('dir /a-d /b "%DOCFullPath%\*.pdf" ^| find /i /v "_PRINTED_"') do (

REM Print document
"%Acrobat%" /n /s /o /h /t "%DOCFullPath%\%%A" "%Printer%" "RICOH Aficio MP 3500 PCL 6" "NPSO01B4PRMFDCOL"
ren "%DOCFullPath%\%%A" "%%~nA_PRINTED_%Stamp%.pdf"
)

EXIT

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Bill,

Same thing happens. Cannot kill Adobe Reader after the file has been printed. So this means I need to go to every session of Adobe reader and close them manually. Seems pointless given the whole idea is to automate the process!!

Is there anything else we can try?

Shaun
Do you get any errors from the taskkill coammand?

After the BAT runs, try doing the taskkill yourself manually at a command line to what happens also.

~bp
No nothing at all.

As suggested, I entered the taskkill /IM AcroRd32.exe in a seperate command window and it closed the Acrobat session successfully as expected. It then moved onto the printing and renaming next file. Method is working fine, its just the Acrobat session we need to deal with.

Are there any other work arounds?
Shaun
Hmm, that's odd, let me try some other options here.

~bp
Hi Bill,

How are you getting on with the Acrobat solution?

Shaun
I tried it a few different ways, and it always seems to kill the reader process.  Not sure why it's not there.

~bp
Is there any other work arounds like using two seperate command scripts built into one?
Rather than TASKKILL, you could try PSKILL from Microsoft SysInternals utils.

http://technet.microsoft.com/en-us/sysinternals/bb896683

~bp
Hi Bill,

Given the time it has taken for us to work out a solution, I believe it is best for me to close this case for now. If I require further assistance in the final objective, I will post it in a seperate session.

Once again, thanks very much for all your wonderful help.
Shaun
Welcome, hope you can salvage some of it, sorry Acrobat isn't a bit more batch friendly.

~bp