The Windows command prompt (cmd.exe) provides the FOR command. It can be used to do an operation for any file in a directory. Example:

\> dir
28.05.2012  18:25                69 buildall.bat
28.05.2012  07:48    <DIR>          include
28.05.2012  18:23    <DIR>          sub-607

\> for %d in (*.bat) echo %d

How can I do the same thing (non-recursively) with all subdirectories in a given directory? The idea is:

for %d in (sub-*) do (
  cd %d
  cd ..

The for statement above does not find sub-607 becauses it's not a file.


The for command can take the /D switch to iterate over subdirectories, rather than files.

for /D %d in (sub-*) do ...

See here for more information: http://ss64.com/nt/for_d.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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