Windows 7 / Getting Started

Processing Directories

The for command has several variations that change the way it interprets the contents of the listed set of names.

The variation for /d %variable in (set) do command works much like the standard for command, except that wildcards in set match only directory names.You can use this variation to perform a command or run a batch file in any or all of the subfolders of particular folders. For example, on Windows 7,

for /d %d in ("%homepath%\Documents\My Music\*.*") do echo %d

lists the names of all the subfolders under the user's My Music folder.

Processing Files in Directories and Subdirectories

The variation for /r path %variable in (set) do command runs the complete for command in the directory named by path and then in each of its subdirectories and their subdirectories, and so on.The wildcard matching operation on set is performed in each of these directories. For example, the command

for /r c:\data %x in (*.txt) do notepad %x

visits the folder c:\data and each of its subfolders, and in each one, it runs a copy of Notepad to display and edit every .txt file it finds. (This can open a lot of copies of Notepad.)

[Previous] [Contents] [Next]