View Full Version : Resolved Batch script - recursing through folders

09-08-2009, 03:33 PM
I'm trying to write a batch script to do a lot of work for me by converting TIF files into PDFs. I've got an executable to do the conversion, but need some help with the script.

FOR /F "tokens=*" %%G IN ('DIR /B /AD /S *.*') DO (
image2pdf -i "%%G\*.TIF" -o "%%G\output.pdf"

What that does is recurses through all sub-directories and calls image2pdf. That script, when run inside a folder, adds all images into one PDF. Right now the script works and creates output.pdf in all of the folders. What I'd like to do is be able to name the file by date according to the folder structure. Here's an example:

I run the script in the top level folder, which sees this tree:

-->123456 (this folder has all of the files)
-->123456 (this folder has all of the files)
-->123456 (this folder has all of the files)


Is there some way I can identify that top level folder and save it as a variable so I can use the date information?

09-08-2009, 06:14 PM
As far as I know you split things simply with the "for" loop, which you are already familiar with. "cd" with no arguments will give you a string of the current directory.

It may be better to loop through each of the dates and save the output pdf string then, while in each, loop through each of the subdirectories again to convert the images.

some pseudo code:

for each date_folder (
create the date\output.pdf string s
for each recursive subdirectory (
save to s

If you are still having problems with that it might be time to change to a more powerful language such as vbs, power shell or a small executable written in a high level language.

09-08-2009, 07:20 PM
I'd like to avoid PS and VBA if possible - simple is better in this case. You have given me an idea, but I'm stuck once more.

In the script below, I've managed to get the month, day, and year, which I can now use to create my filename.

One improvement I'd like to make now, is to not create unnecessary files. My image2pdf program creates a PDF file regardless of what's in the folder. I want to look at the folder, and if there are no *.TIF files, not run the script.

I understand the logic of the GOTO command, but need help with the condition. I tried counting the files in the directory, which works...but I don't know how to assign that to a variable (what I have doesn't work). My thought is to simply count the number of files in the current directory (non-recursive)...if that's 0, then don't create the PDF. That way I can output my PDF files all to one folder and not have to move them all manually.

@echo off

FOR /F "tokens=*" %%G IN ('DIR /B /AD /S *.*') DO (call :makePDF "%%G")
GOTO :done

set data=%1

:: Remove quotes
for /f "useback tokens=*" %%a in ('%data%') do set data=%%~a
echo %data%

::::::::::::::Trying to get the number of files in the current directory

SET num_files = dir "%data%" /b |find /v /c "::"
echo Number files: %num_files%

::::::::::::::Trying to get the number of files in the current directory

:: Get the last part of the string
set data=%data:~-18%

:: Get just the date info now
set str=%data:~0,8%

:: Get month, day, and year
set year=%data:~0,4%
set month=%data:~4,2%
set day=%data:~6,2%

echo year: %year%
echo month: %month%
echo day: %day%

GOTO :eof


09-08-2009, 08:00 PM
I didn't mention VBA at all. Visual Basic for Applications is something completely different to Visual Basic Script. VBS is a simple scripting language supported by most versions of Windows that can be run in windowed or console mode. You can also use JScript (.js files) but the result is about the same you just use a different syntax. VBA is the crappy language you get inside Microsoft Access and the like that is just a little bit more powerful than Microsoft Office macros.

I will assume you meant that you didn't want to use Power Shell or VBS.

For checking the existance you can use
if not exist *.tif

09-08-2009, 08:06 PM
I did mean VBS, sorry about that! (An acceptable typo, I hope, considering the QWERTY keyboard). :)

Much simpler solution than what I was going for...it worked just fine. Thanks for your help!

09-08-2009, 08:33 PM
I just thought I would make the distinction because VBS is commonly used for doing things just out of reach of batch (but all possible in power shell) while VBA is hated by absolutely everyone and you would be quite right never to use it. :)

Glad it worked for you.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum