WatchDirectory Forum
http://www.watchdirectory.net/cgi-bin/yabb25/YaBB.pl
watchDirectory forums >> Plugins >> Execute .bat on all files in a directory
http://www.watchdirectory.net/cgi-bin/yabb25/YaBB.pl?num=1304445003

Message started by alexhoward on May 3rd, 2011 at 7:50pm

Title: Execute .bat on all files in a directory
Post by alexhoward on May 3rd, 2011 at 7:50pm
I'm looking for a way to merge individual PDF files into one document on an automated basis. I've used WatchDirectory and thought I could use a batch file with Ghostscript for doing this but I keep getting device errors from GS when I try to run the command to merge the files

gswin32 -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=Merged.pdf -dBATCH 1.pdf 2.pdf 3.pdf

I found another tool to do this called PDFCombine which works great. I've been able to automate it with a simple batch file but my problem is that I'd like to execute the batch file on everything within a directory, not each file in the directory.

PDFCombine.exe "C:\Path to files\PDF_In" "C:\Path to files\PDF_Out\merged_<DATE>_<TIME:hhmm>.pdf" (These variables are for PDFCombine, not for WD.)

These are individual pages that will be produced sequentially, so I need WatchDirectory to wait until the directory is stable for 30 seconds or so before the script is triggered. In my test, I throw 4 files into the directory then, by watching the console window, Watch Directory then runs the command on each file in succession. I would like WatchDirectory to execute the command only once, as PDFCombine can process everything in the directory.

I'm using FILECHG as the event trigger with a 30 second delay. Is there another event I should use to put WD's focus on the directory rather than the individual files? Or does anyone have a suggestion for a better way to handle this?

Thanks.

Title: Re: Execute .bat on all files in a directory
Post by Gert on May 3rd, 2011 at 7:59pm
Hello Alex,

I think the easiest way is to enable FILENEW (for new files, but you may chose your FILECHNG as well, I guess) and, this is important, the QUIET event set to 30 seconds with also the option "Only one QUIET event".

Now the bat file you should use will look like
[code]
rem are we called for a new or changed file? Ignore the event
IF "%WD_REASON%" EQU "FILENEW" GOTO :EOF
IF "%WD_REASON%" EQU "FILECHNG" GOTO :EOF
rem so we are called for the QUIET event

"C:\Bin\PDFCombine.exe" "C:\Path to files\PDF_In" "C:\Path to files\PDF_Out\merged_<DATE>_<TIME:hhmm>.pdf"
[/code]

Something like above should work fine. I have added the path to PDFCombine (always use full paths in scripts!).

Title: Re: Execute .bat on all files in a directory
Post by jimaus on Jul 3rd, 2013 at 7:34am
Hi Gert,

We have not communicated for a while, WD is as always, operating superbly, I trust all is well.  :)

I am trying to something similar to Alex and given the age of the topic I thought maybe something may have changed that allows a solution that does not require the use of batch files.

I hunted around the web and found pdftk.exe from PDFLabs that will combine the files as I require but I would like to combine a number of files from a folder structure that has input and output folders as well as sub folders for various publications, e.g NWS and ECH under both in and out folders.

I would like to have a single WD setup to look at the root folder and subfolders and process the files, placing them in the correct output folders i.e. WD_RELDIR.

But as your help file rightly says, quite a number of the Environment variables do not work with a quiet event, which I believe is required to trigger the batch file so that all files can be processed together and not individually.

Below is the batch file I have used

---------------------------------------------------------------------------------------
rem are we called for a new or changed file? Ignore the event
IF "%WD_REASON%" EQU "FILENEW" GOTO :EOF
IF "%WD_REASON%" EQU "FILECHNG" GOTO :EOF
rem so we are called for the QUIET event


"C:\Program Files (x86)\PDFtk Server\bin\pdftk.exe" %WD_DIR%\*.pdf output %WD_DIR%\NWS_RealEstate_%WD_DATE%_%WD_TIME%.pdf

---------------------------------------------------------------------------------------

I am monitoring c:\test\pdfin\nws, this produces a pdf file that has then contents of the watched folder merged. I would also like to  be able to make the file name dynamic based on the folder that the files were found in and move the original files to an archive folder.

Your thoughts and suggestions would be appreciated as always

Cheers, Jim

Title: Re: Execute .bat on all files in a directory
Post by Gert on Jul 3rd, 2013 at 10:25am
Hello Jim,

I have a hard time understanding the exact problem you have with your script.

This is what I am guessing.... am I correct?
- you want a separate QUIET event for each sub-folder (not for the complete directory tree)
- when QUIET fires, you need to know for which sub-folder it fires (environment variables set for that sub-folder)

The above may be hard to implement, but I can see its usefulness. Before I spend a lot of time on it.... am I correct the above would solve the issue?

Title: Re: Execute .bat on all files in a directory
Post by jimaus on Jul 3rd, 2013 at 9:41pm
Gert,

Yes, your guess is correct, currently I need to have separate batch files for each directory monitored, which will become difficult to manage if this is done for a lot of sub folders.

e.g. Folder structure.

PDFIn    
           NWS
           ECH
           BEN

PDFout
           NWS
           ECH
           BEN

WD monitors  PDFIN and sub folders, if a Quiet event is triggered in NWS, batch runs that combines pdf's and places them in PDFout\NWS.

I notice that the "Run any Program" plugin does not trigger on quiet events, could this be used to replace the use of batch files if it was able to do so?

I find that not a lot of people understand batch files and scripting generally, so I try to avoid using them. I am not very good at them myself, a lot of research and hit and miss is involved each time I have to do something with batch files.

Cheers, Jim


Title: Re: Execute .bat on all files in a directory
Post by Gert on Jul 4th, 2013 at 12:33pm
I will look at the QUIET event for "start any program".

What I think I will implement is this:
On the "Events" window - http://www.watchdirectory.net/wdhelp/help/wdnewconfigpage4.html - there will be a sub-option for QUIET, something like
[ ] Each sub-directory of the monitored directory has its own QUIET event

So if you are monitoring C:\Monitor, a directory with several subdirs (C:\Monitor\Dir1 and C:\Monitor\Dir2), it can fire 3 different QUIET events (also one for C:\Monitor itself).
There will be an extra variable just for QUIET events, something like
WD_QUIETDIR
That contains C:\Monitor, C:\Monitor\Dir1 or C:\Monitor\Dir2
and perhaps also
WD_QUIETSUB
That contains "" (empty), Dir1 or Dir2

Note that this will only work for subdirectories directly under the monitored directory (not for C:\Monitor\Dir1\anothersub).

I have looked at the sources a bit, I think this should be relatively easy to implement (but no promises).

Title: Re: Execute .bat on all files in a directory
Post by jimaus on Jul 5th, 2013 at 2:07am
Thanks Gert,

That sounds like an ideal implementation, hopefully it will be possible to do. (I note the no promises)

Look forward to seeing it.

Cheers,  Jim

Title: Re: Execute .bat on all files in a directory
Post by Gert on Jul 5th, 2013 at 10:16am
Hello Jim,

I just checked a bit better and it will not be possible to implement this soon, it is just to tricky. I will leave it on the todo list for a future version.

I have created a bat script for you that should work for QUIET events on the root-directory (C:\PDFIN). It checks each sub-directory to see if there are PDF files present, if not it skips the subdir otherwise it runs your pdftk command on them. If pdftk runs ok it moves the processed pdf files to another directory.

It should be easy to follow what it does, if not shout.

[code]@echo off
rem are we called for a new or changed file? Ignore the event
IF "%WD_REASON%" EQU "FILENEW" GOTO :EOF
IF "%WD_REASON%" EQU "FILECHNG" GOTO :EOF
rem so we are called for the QUIET event

SET SOURCEROOT=C:\PDFIN
SET PDFTARGET=C:\PDFOUT
SET MOVETARGET=C:\PDFDONE


rem loop over all subfolders inside %SOURCEROOT%
FOR /D %%a IN (%SOURCEROOT%\*) DO Call :DoOneSubfolder "%%a"
rem quit
GOTO :EOF


:DoOneSubfolder
SET FOLDERPATH=%~1
SET JUSTSUBNAME=%~n1
rem check if there are any pdf files present in this directory
Call :CountPDFS
echo %FOLDERPATH% - %JUSTSUBNAME% - %COUNT% PDFS
IF %COUNT% GEQ 1 Call :CombinePDFS
GOTO :EOF


:CombinePDFS
"C:\Program Files (x86)\PDFtk Server\bin\pdftk.exe" "%FOLDERPATH%\*.pdf" output "%PDFTARGET%\%JUSTSUBNAME%\%WD_DATE%_%WD_TIME%.pdf"
if %errorcode% EQU 0 (
  mkdir "%MOVETARGET%\%JUSTSUBNAME%"
  MOVE "%FOLDERPATH%\*.pdf" "%MOVETARGET%\%JUSTSUBNAME%"
)
GOTO :EOF


:CountPDFS
SET COUNT=0
For %%c IN (%FOLDERPATH%\*.pdf) DO Call :Count_AddOne
GOTO :EOF

:Count_AddOne
SET /a COUNT=%COUNT% + 1
GOTO :EOF
[/code]

Title: Re: Execute .bat on all files in a directory
Post by jimaus on Jul 8th, 2013 at 12:44am
Gert,

Thanks for the batch file, after a couple of modifications it works.

Initially I got this error

C:\Batchfiles>echo C:\Test\CombineRoot\PDFIN\NWS - NWS - 12 PDFS
C:\Test\CombineRoot\PDFIN\NWS - NWS - 12 PDFS
C:\BatchC:\Windows\system32\cmd.exefiles>IF 12 GEQ 1 Call :CombinePDFS
C:\Batchfiles>"C:\Program Files (x86)\PDFtk Server\bin\pdftk.exe" "C:\Test\CombineRoot\PDFIN\NWS\*.pdf" output "C:\Test\CombineRoot\PDFOUT\NWS\NWS_RealEstate_20130706_082823.pdf"
0 was unexpected at this time.
C:\Batchfiles>if  EQU 0 (

It appears that " if EQU 0 " was the issue, I believe this is to determine if the "done" folder for the files to be processed exists, if not create it.
Given the subfolders are not required to be created on the fly, I removed the line "if %errorcode% EQU 0 " , the mkdir and the parentheses , I kept the command to move the files and it works a treat.

I now have a WD setup that will combine files from a folder structure and move the files to a done folder.

Thanks for your help once again.

Cheers, Jim

Title: Re: Execute .bat on all files in a directory
Post by Gert on Jul 8th, 2013 at 6:21am
Hello Jim,

As I didn't have pdftk installed, I couldn't test the complete script. I now see what the error was:
you should replace

Code (]if %errorcode% EQU 0 (
  mkdir "%MOVETARGET%\%JUSTSUBNAME%"
  MOVE "%FOLDERPATH%\*.pdf" "%MOVETARGET%\%JUSTSUBNAME%"
)
[/code):

with this
[code]if %errorlevel% EQU 0 (
  mkdir "%MOVETARGET%\%JUSTSUBNAME%"
  MOVE "%FOLDERPATH%\*.pdf" "%MOVETARGET%\%JUSTSUBNAME%"
)


(errorcode -> errorlevel)

The idea is that the pdf files are only moved if the pdftk command runs without error. If there is some problem, the files are not moved and will be retried the next run.

Title: Re: Execute .bat on all files in a directory
Post by jimaus on Jul 9th, 2013 at 7:46am
Gert,

Thanks, that fixed the issue.

Appreciate the fast response.

Cheers, Jim

WatchDirectory Forum » Powered by YaBB 2.5.2!
YaBB Forum Software © 2000-2017. All Rights Reserved.