Windows Batch File Looping Through Directories to Process Files?

WindowsRecursionBatch File

Windows Problem Overview


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?

Windows Solutions


Solution 1 - Windows

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

Solution 2 - Windows

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

for /R %%f in (*.tif) do echo "%%f"

Solution 3 - Windows

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

Solution 4 - Windows

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.

Solution 5 - Windows

I know this is not recursion (iteration through enumerated subdirectories?), but it may work better for some applications:

for /F "delims=" %%i in ('dir /ad /on /b /s') do (
	pushd %%i
	dir | find /i "Directory of"
	popd
)

Replace the 3rd line with whatever command you may need.

dir /ad - list only directories

The cool thing is pushd does not need quotes if spaces in path.

Solution 6 - Windows

rem Replace "baseline" with your directory name
for /R "baseline" %%a in (*) do (
	echo %%a
)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTylerView Question on Stackoverflow
Solution 1 - WindowsAaciniView Answer on Stackoverflow
Solution 2 - WindowsJack AllanView Answer on Stackoverflow
Solution 3 - WindowsTomsimView Answer on Stackoverflow
Solution 4 - WindowsGras DoubleView Answer on Stackoverflow
Solution 5 - WindowsManicalicView Answer on Stackoverflow
Solution 6 - WindowsVivek MandalView Answer on Stackoverflow