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.)
In this tutorial:
- The CMD Command-Line
- CMD Versus COMMAND
- Running CMD
- Opening a Command Prompt Window with Administrator Privileges
- CMD Options
- Disabling Command Extensions
- Command-Line Processing
- Console Program Input and Output
- Using the Console Window
- I/O Redirection and Pipes
- Copy and Paste in Command Prompt Windows
- Command Editing and the History List
- Name Completion
- Enabling Directory Name Completion
- Multiple Commands on One Line
- Grouping Commands with Parentheses
- Arguments, Commas, and Quotes
- Escaping Special Characters
- Configuring the CMD Program
- The Search Path
- Changing the Path
- Predefined and Virtual Environment Variables
- Setting Default Environment Variables
- Built-in Commands
- Extended Commands
- Listing Files with the Dir Command
- Paginating Long Listings
- Printing Directory Listings
- Sorting Listings
- Locating Alternate File Streams
- Setting Variables with the Set Command
- Conditional Processing with the if Command
- Scanning for Files with the for Command
- Using the for Command's Variable
- Processing Directories
- Numerical for Loop
- Getting More Information