Windows – Recursive CMD or BAT Windows Script

command-line-interfacepowershellwindowswindows-command-promptwindows-server-2003

How can I create a BAT or CMD which will iterate through a folder tree and run same command agaists each folder file?

E.g

myexe.exe C:\Documents and Settings\folder1\filename.txt
myexe.exe C:\Documents and Settings\folder2\filename.txt

Notes:

  • The filename is the same on each folder found.
  • The folder names will be all different.

Your help is very appreciated or any hints of where to start.

Best Answer

Adapt this batch file to your needs:

@echo off
cd "\program files"
for /f "usebackq delims=|" %%a in (`dir filename.txt /s/b`) do (
        echo %%a
        myexe "%%a"
)

make sure to wrap the variable in double quotes in case the file name has spaces in it.