# Executing .bat file in multiple subdirectories



## illyume (Jul 1, 2009)

I have a pair of .bat files set up to modify and convert multiple .tif images in a single directory.

It's not working right. I also need to set it up to run in multiple subdirectories. (It's a large-scale image conversion I'm working on)

The first .bat file simply executes a for command:

```
for %%f in (*.tif) do call echo.bat %%f
```
The second .bat file takes that parameter, and does this:


```
set file=&~n1
```
Which should set the variable 'file' to the file's name without file extension. It then runs through a series of commands to convert the file type, set the background to transparent, and cleans up the image.

It's working well, except for a couple of things:

on Angel.tif, and Bee.tif it works fine... but then it hits Bird House.tif and the space messes up the string parser, and it ends up returning just 'Bird' instead of 'Bird House'

How do I get the spaces to stay in there?

The "set file=%file:~0,-4%" doesn't work either, because that gets messed up by the space as well.

Also, I need this .bat to function not just in the directory it's put in, but instead I need to modify it somehow to run in each of the subdirectories 2-4 folders deeper than the current directory, and do the same thing to the files in those subdirectories. And... some of the folders have white space in their names too. Does anyone know how to make this all work?


----------



## Stu_computer (Jul 7, 2005)

*for /F "usebackq delims=" %k IN (`dir *.tif /s /b`) DO *@echo* %~dpnk*

here i just @echoed it to the display, try it at the command prompt and it looks like doing a fancy dir *.tif and will also show all the subdirectory contents too.

so in example if i was using irfanview (which, by the way, does batch conversion etc) to process a directory tree of tif files, like your doing, then i would use it's command line version like this...

*for /F "usebackq delims=" %k IN (`dir *.tif /s /b`) DO* c:\program files\i_view32.exe *%~dpnk* /convert=*%~dpk*\*.jpg
(not exactly correct, would have to tell iview it's getting a tif extension, but you get the idea).

so, up to you whether you use it directly or call the next batch file.

%k (full path)
%~dpnk (path, no extention)
%~dpk (only shows directories part that have k files but not filenames)


----------



## illyume (Jul 1, 2009)

Thank you so much! I haven't quite figured out what that code does, but I've been able to put it in my .bat files, adjusted a few things, and it's working just as I wanted it to now!

This is great! 

Err, scratch that. It's _almost_ working perfectly. Then I get to "Bird House.tif" and it does the bad '*** do I do with whitespace!?' thing again. At least this time it's a little further in the function call, and there's only two spots I'm going to have to change things further, it looks like.

So, here's the first place where it errors:









And there's another line that I'm pretty sure will do the same thing:
convert img_inside.png img_edging.png -background none -flatten %file%.png

How do I fix this?


----------



## illyume (Jul 1, 2009)

*Nevermind, I'm a .bat noob. I just needed to use "%file%" instead of %file% in the calls to the file.* 

Anyway, thanks again for the help!


----------



## Squashman (Apr 14, 2005)

Any time you have spaces in your path or filenames you need to use quotes.


----------



## Patrick Mc (Feb 6, 2009)

The following biterscript may simplify the job. It will convert all .tif files in C:\Winter and its subfolders, sub-subfolders, etc.

As Squashman correctly suggested, I am enclosing fully qualified file names in double quotes, since file names or folder names may contain spaces and other special characters.


```
# script convert.txt
# Get a list of .tif files.
var str list ; lf -n "*.tif" "C:\Winter" > $list
# Convert one file at a time.
while ($list <> "")
do
    var str file ; lex "1" $list > $file
    system convert ("\""+$file+"\"") "OTHER OPTIONS AND ARGUMENTS"
done
```
I have not tested it. Test it first. Save the script as C:\Scripts\convert.txt, start biterscripting ( http://www.biterscripting.com ), enter the following command.


```
script convert.txt
```

Patrick


----------

