Help write a simple script please!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
vladtess
Posts: 3
Joined: 23 Aug 2010 12:55

Help write a simple script please!

#1 Post by vladtess » 23 Aug 2010 13:00

Hi there!

I have a simple script to write, but I have limitations at programming. Here's what I need my script to do:

1. Scan a directory (C:\temp\spool) for changes every second.
2. If a *.pdf file is detected, execute command "AcroRd32.exe /p/t C:\temp\spool\*.pdf" (basically, print the file using acrobat)
3. Delete that file (*.pdf)
4. Resume the loop scanning.

Can anyone help me??!

Thanks much!!!

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Help write a simple script please!

#2 Post by orange_batch » 23 Aug 2010 17:37

This is similar enough to dally165's thread. This is my solution to his problem adapted to yours:

DOS needs to know where AcroRd32.exe is, so if you use an absolute path like C:\Program Files\...\AcroRd32.exe make sure you enclose it in quotes, as in "C:\Program Files\...\AcroRd32.exe".

This will wait for AcroRd32.exe to terminate (i.e. send the file to your printer) before deleting the PDF and resuming the loop.

Options are:

set "monitor=C:\Your Folder Path" <- Self-explanatory.

ping -n 3 -i 1 127.0.0.1>nul <- As in the comment, -n 2 is check every second, -n 3 is check every 2 seconds, etc...

Code: Select all

@echo off&echo:&color B&title PDF Print-Delete Monitor by orange_batch&setlocal enabledelayedexpansion

set "monitor=C:\Your Folder Path"
set prevlength=1
set "prev1=nul"
set "new=true"

echo: ^> Monitoring: ... %monitor:~-60%
echo:

:loop

:: -n 2 is wait 1 second, -n 3 is wait 2 seconds, -n 4 is ... etc.
ping -n 3 -i 1 127.0.0.1>nul

:: Gather current files' paths.
for /r "%monitor%" %%x in (*.pdf) do (
set /a currlength+=1
set "curr!currlength!=%%x"
)

:: Compare each current file's path to each previous file's path. If a match is not found, then process the file.
for /l %%x in (1,1,%currlength%) do (
for /l %%y in (1,1,%prevlength%) do (
if "!curr%%x!"=="!prev%%y!" set "new=false"
)
if "!new!"=="true" (
AcroRd32.exe /p/t "!curr%%x!"
echo:   Printed: ... !curr%%x:~-63!
echo:
del /s "!curr%%x!"
echo:   Deleted: ... !curr%%x:~-63!
echo:
) else set "new=true"
)

:: Copy current file paths over previous file paths and loop.
for /l %%x in (1,1,%currlength%) do (
set "prev%%x=!curr%%x!"
)
set prevlength=%currlength%
set currlength=
goto loop

vladtess
Posts: 3
Joined: 23 Aug 2010 12:55

Re: Help write a simple script please!

#3 Post by vladtess » 23 Aug 2010 23:53

orange_batch, thanks so much for your reply!! It does its job = prints, however some debugging must be done. One problem is that after the print, the adobe does not close (adobe reader window is open) and that causes the script to halt (I think). Is there a way about it? Thanks so much!!!

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Help write a simple script please!

#4 Post by orange_batch » 24 Aug 2010 01:37

(Edited) AcroRd32.exe needs a command line switch to terminate after printing, otherwise the loop will delete the file before it even has a chance to open. If AcroRd32.exe doesn't require a file once opened to exist on the harddrive, a sufficient delay before deletion would solve the problem. If so, place this on a new line before the "del /s ..." in the script:

ping -n # -i 1 127.0.0.1>nul

...where # as aforementioned, is the number of seconds to wait plus 1. (2 = 1 sec, 3 = 2 secs, 4 = 3 secs, ...)

Make sure to give it enough time.

Otherwise, you can have it continually monitor the folder and print any PDFs that appear without deleting them by:
-First, add "start "" " (without the surrounding quotes) to the beginning of the same line as AcroRd32.exe.

Code: Select all

start "" "C:\...\AcroRd32.exe" /p/t "!curr%%x!"
-Second, removing the del /s ..... line of the script (and for good measure, the following two echo: status lines).

This will make DOS not wait for AcroRd32 before looping. Just be aware that moving any PDF(s) around folders within the one being monitored will have the PDF(s) recognized as a new file.

If you're printing a batch of PDFs and can manually delete them after the whole process, this change would be fine.

vladtess
Posts: 3
Joined: 23 Aug 2010 12:55

Re: Help write a simple script please!

#5 Post by vladtess » 24 Aug 2010 10:19

Organge_batch, thanks much for your help. I solved the problem by using another reader, Foxit reader. It loads faster and has many options in command line execution. So I simply replaced the line with

Code: Select all

"C:\Program Files\Foxit Software\Foxit Reader\Foxit Reader.exe" /t "!curr%%x!" "Canon MF3200 Series"

But the rest of the code works very well. Thanks so much!!!

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Help write a simple script please!

#6 Post by orange_batch » 24 Aug 2010 11:22

Indeed, termination switch ftw.

Post Reply