up vote 2 down vote favorite
1
share [g+] share [fb]

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.

link|improve this question

2  
is powershell an option? – tony roth Aug 10 '10 at 2:36
yes, i guess it can .. thanks – codex73 Aug 10 '10 at 2:37
feedback

2 Answers

up vote 3 down vote accepted

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.

link|improve this answer
Thanks so much! This is exactly what I was looking for. – codex73 Aug 10 '10 at 14:04
'+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! – pipitas Aug 11 '10 at 11:20
feedback

A PowerShell approach:

Get-ChildItem -Recurse . FILENAME.TXT | foreach {
    $_
    & myexe $_
}
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.