Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|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

2 Answers 2

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.

share|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! –  Kurt Pfeifle Aug 11 '10 at 11:20

A PowerShell approach:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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