Page 1 of 1

Combining 2 Batch Files with a Loop

Posted: 01 Sep 2009 15:40
by jhennig
I got some excellent help here yesterday from Dccd who very quickly solved a problem for me that I have been struggling with for a time.

I thought I was all set, but it turns out that I'm in need of help again.

I needed a batch file that would take exactly 8 pdfs from a directory, move them to another and rename them to a common name with a sequence number after each one. That's what DccD did for me and the code is prefect. Here's the file:

[echo off

set "sourcedir=D:\BC250_IN"
set "destdir=D:\BC250_OUT"
cd /d "%sourcedir%"

:: Count the number of PDF files
set numbfile=0
FOR /F "tokens=*" %%A IN ('dir /b *.pdf 2^>nul') do (set /A numbfile+=1)
if %numbfile% LSS 8 exit

:: Start the process
set counter=0
set numb=
for %%a in (*.pdf) do call :loop "%%~a"
goto :eof

:loop
set /A counter+=1
if %counter% == 9 exit
if exist "%destdir%\file_%numb%%counter%.pdf" set /A numb+=1
move %1 "%destdir%\file_%numb%%counter%.pdf"
goto :eof]

I wrote another batch file to further process them with a shareware app called PDFMerger Deluxe (http://www.sherrodcomputers.com/). It has a command interface to combine these individual PDFs into a single file. My batch file combines them and then adds a timestamp, sends the combined file to a hot folder and deletes the source files. It also works fine. Here's the code:

[@echo off

set "destdir=D:\BC250_OUT"
set "mergedir=D:\BC250_BATCH"
set "hotfolder=D:\HF"
set "filename=file_"

cd C:\"Program Files"\PDFMergerDeluxe

pdfmerge.exe /i %destdir%\%filename%1.pdf,%destdir%\%filename%2.pdf,%destdir%\%filename%3.pdf,%destdir%\%filename%4.pdf,%destdir%\%filename%5.pdf,%destdir%\%filename%6.pdf,%destdir%\%filename%7.pdf,%destdir%\%filename%8.pdf /o %mergedir%\batch250.pdf /silent

del %destdir%\%filename%1.pdf
del %destdir%\%filename%2.pdf
del %destdir%\%filename%3.pdf
del %destdir%\%filename%4.pdf
del %destdir%\%filename%5.pdf
del %destdir%\%filename%6.pdf
del %destdir%\%filename%7.pdf
del %destdir%\%filename%8.pdf

rem Parse the date
set cur_yyyy=%date:~10,4%
set cur_mm=%date:~4,2%
set cur_dd=%date:~7,2%

rem Parse the time
set cur_hh=%time:~0,2%
if %cur_hh% lss 10 (set cur_hh=0%time:~1,1%)
set cur_nn=%time:~3,2%
set cur_ss=%time:~6,2%
set cur_ms=%time:~9,2%

rem Set the timestamp format
set timestamp=%cur_yyyy%%cur_mm%%cur_dd%-%cur_hh%%cur_nn%%cur_ss%%cur_ms%

rem Do something with it
move %mergedir%\batch250.pdf %hotfolder%\batch250_%timestamp%.pdf

rem Clear the environment variables
set cur_yyyy=
set cur_mm=
set cur_dd=
set cur_hh=
set cur_nn=
set cur_ss=
set cur_ms=
set timestamp=]

Here's the issue: I have both batch files scheduled with scheduled task and all was OK. Until I was informed today that the time gap between the two executions need to be less than a minute and that I need to add an additional 4 components to this (executing both scripts each to 4 different sets of folders). (which I plan to do as separate combined batch files)

I tried to merge the two together, but I think the ":loop" call is in stomping on the merging. I tried setting up another process, but it gets to the end of the first script and stops. No matter what I do, I cannot get past the first script.

I need them to run in order, meaning select the 8 files first and then merge them.

Any thoughts anyone?

Posted: 01 Sep 2009 16:04
by DccD
I'm not sure to understand what you already did but I would create a batch file named LOADER.BAT with the following code:

Code: Select all

@echo off
call script1.bat
call script2.bat


Then make that LOADER.BAT start from a scheduled task every minute.
And repeat all this per folder needed to be processed.

Does it make sense?

Posted: 01 Sep 2009 16:22
by jhennig
Thanks DccD.

Yes it does make sense. I neglected to say in my post that it was one of the things I had tried. It executed the code that you did for me yesterday but the second script never starts. I even put a pause in it so I could watch it but it never launches. I also tried playing with combinations of start and call - no difference.

I did some experimenting with wait commands too - nothing.

Is it possible that the first batch isn't terminating?

Posted: 01 Sep 2009 18:33
by DccD
I see, this is because of the EXIT command. This one should work:

Code: Select all

echo off

set "sourcedir=D:\BC250_IN"
set "destdir=D:\BC250_OUT"
cd /d "%sourcedir%"

:: Count the number of PDF files
set numbfile=0
FOR /F "tokens=*" %%A IN ('dir /b *.pdf 2^>nul') do (set /A numbfile+=1)
if %numbfile% LSS 8 goto :eof

:: Start the process
set counter=0
set numb=
for %%a in (*.pdf) do call :loop "%%~a"
goto :eof

:loop
set /A counter+=1
if %counter% GEQ 9 goto end
if exist "%destdir%\file_%numb%%counter%.pdf" set /A numb+=1
move %1 "%destdir%\file_%numb%%counter%.pdf"
:end
goto :eof

Posted: 01 Sep 2009 20:38
by jhennig
PERFECTION!

Your modification did the trick and allowed my batch file to run. It enabled me to do some debugging on mine and found that I needed to split the merge routine from the move/rename. Odd considering that it ran combined as a stand alone without issue - but I'm not complaining.

I also had to insert a wait into my launcher batch file, so it ended up looking like this:

Code: Select all

echo off

call c:\scripts\BC250choose8.bat

echo wscript.sleep 3000 > c:\waiting.vbs
wscript c:\waiting.vbs
del c:\waiting.vbs

start c:\scripts\BC250merge8.bat

echo wscript.sleep 3000 > c:\waiting.vbs
wscript c:\waiting.vbs
del c:\waiting.vbs

call c:\scripts\BC250rename.bat


It runs perfectly now.

Thanks very much.[/quote]

Posted: 01 Sep 2009 21:36
by DccD
I'm glad everything is working now :)

Just the be "more" perfect, the "wait" can be achieved several ways...
using PING:

Code: Select all

ping -n 4 127.0.0.1 >nul

or using the SLEEP command if you run Windows Server
Usage: sleep time-to-sleep-in-seconds
sleep [-m] time-to-sleep-in-milliseconds
sleep [-c] commited-memory ratio (1%-100%)


instead of using external VBscript:

Code: Select all

echo wscript.sleep 3000 > c:\waiting.vbs
wscript c:\waiting.vbs
del c:\waiting.vbs

...and it saves you some extra lines ;)

Posted: 02 Sep 2009 08:00
by jhennig
Even better. I had not considered using a ping as a wait.

Thanks again!