Combining 2 Batch Files with a Loop
Posted: 01 Sep 2009 15:40
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?
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?