Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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


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


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.