Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Batch File Looping Through Directories to Process Files?

I need to write/use a batch file that processes some imagery for me.

I have one folder full of nested folders, inside each of these nested folders is one more folder that contains a number of TIF images, the number of images vary in each folder. I also have a batch file, lets call it ProcessImages.bat for Windows that you can "drop" these TIF files on (or obviously specify them in a command line list when invoking the bat); upon which it creates a new folder with all my images process based on an EXE that I have.

The good thing is that because the bat file uses the path from the folders you "drop" onto it, I can select all the TIFs of one folder and drop it to do the processing... but as I continue to manually do this for the 300 or so folders of TIFs I have I find it bogs my system down so unbelievably and if I could only process these one at a time (without manually doing it) it would be wonderful.

All that said... could someone point me in the right direction (for a Windows bat file AMATEUR) in a way I can write a Windows bat script that I can call from inside a directory and have it traverse through ALL the directories contained inside that directory... and run my processing batch file on each set of images one at a time?

like image 347
Tyler Avatar asked Dec 06 '11 09:12

Tyler


People also ask

How do I stop a batch file from looping?

The only way to stop an infinitely loop in Windows Batch Script is by either pressing Ctrl + C or by closing the program.

What is %% f in batch file?

For simple batch files, a single character such as %%f will work. You can use multiple values for variable in complex batch files to distinguish different replaceable variables.

What does %1 mean in a batch file?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.


4 Answers

You may write a recursive algorithm in Batch that gives you exact control of what you do in every nested subdirectory:

@echo off
call :treeProcess
goto :eof

:treeProcess
rem Do whatever you want here over the files of this subdir, for example:
for %%f in (*.tif) do echo %%f
for /D %%d in (*) do (
    cd %%d
    call :treeProcess
    cd ..
)
exit /b
like image 111
Aacini Avatar answered Oct 19 '22 12:10

Aacini


Aacini's solution works but you can do it in one line:

for /R %%f in (*.tif) do echo "%%f"
like image 79
Jack Allan Avatar answered Oct 19 '22 14:10

Jack Allan


Jack's solution work best for me but I need to do it for network UNC path (cifs/smb share) so a slight modification is needed:

for /R "\\mysrv\imgshr\somedir" %%f in (*.tif) do echo "%%f"

The original tip for this method is here

like image 12
Tomsim Avatar answered Oct 19 '22 13:10

Tomsim


Posting here as it seems to be the most popular question about this case.

Here is an old gem I have finally managed to find back on the internet: sweep.exe.
It executes the provided command in current directory and all subdirectories, simple as that.


Let's assume you have some program that process all files in a directory (but the use cases are really much broader than this):

:: For example, a file C:\Commands\processimages.cmd which contains:
FOR %%f IN (*.png) DO whatever

So, you want to run this program in current directory and all subdirectories:

:: Put sweep.exe in your PATH, you'll love it!
C:\ImagesDir> sweep C:\Commands\processimages.cmd

:: And if processimages.cmd is in your PATH too, the command becomes:
C:\ImagesDir> sweep processimages


Pros: You don't have to alter your original program to make it process subdirectories. You have the choice to process the subdirectories only if you want so. And this command is so straightforward and pleasant to use.

Con: Might fail with some commands (containing spaces, quotes, I don't know). See this thread for example.

like image 3
Gras Double Avatar answered Oct 19 '22 12:10

Gras Double