Super User is a question and answer site for computer enthusiasts and power users. 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

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
  dosomething
  cd ..
)

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

share|improve this question
up vote 4 down vote accepted

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

share|improve this answer

You must log in to answer this question.

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