2

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.

1
  • 2
    is powershell an option?
    – tony roth
    Aug 10, 2010 at 2:36

2 Answers 2

3

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.

1
  • '+1'. Would have been my own answer, but without the nice formatting of the loop. Now I've learned how+where to make line breaks without these ugly '^'-chars from your answer. Thanks so much! Aug 11, 2010 at 11:20
2

A PowerShell approach:

Get-ChildItem -Recurse . FILENAME.TXT | foreach {
    $_
    & myexe $_
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .