DOS Loop for command

Source: Internet
Author: User

If you have any commands that you don't understand, you can enter them in cmd: name/?
First, the basic format
For %%variable in (set) do command [Command-parameters]
%%variable Specifies 1 single 1 letters to represent replaceable parameters.
(SET) specifies 1/1 sets of files. Wildcard characters are available.
command specifies the commands to execute on each file.
command-parameters specifying Parameters/command-line switches for specific commands
parameters: For has 4 parameters /d/l/r/f

Ii. parameter/d

for/d%%variable in (set) do command [Command-parameters]
If the set contains wildcard characters, each directory that matches the set (Instead of a filegroup in the specified directory) executes the specified command.
This parameter is primarily used for directory searches and does not search for files:
@echo off
for/d%%i in (c:\*) do echo%%i
The run will print the name of all directories under the C packing directory, and the file name does not show!
@echo off
for/d%%i in (???) do echo%%i
if the current directory has a directory name of only 1-3 letters, it will be displayed, no show
the/d parameter can only display the directory name under the current directory !

iii. parameter/R
for/r [[Drive:]path]%%variable in (set) do command [Command-parameters]
Check the directory tree with [Drive:]path Root], pointing to each directoryFor statement. If no directory is specified after/R, use the currentdirectory. If the set is only a single point (.) character, the directory tree is enumerated.
/ D only displays the directory name under the current path,/Ryou can read all the names of the files under the current/specified path!
Note 2 points:
1. If the file name in set contains a wildcard, the directory specified by the/R parameter and its subdirectories below are listed with the setall files that match, and directories without matching files are not listed.
2, set is a specific file name, does not contain wildcards, the directory tree enumeration (that is, listing the directory and all the sub-directory), regardless of whether the specified file exists in the set. This is the same as the single point (.) enumeration in the preceding list tree, a single point representsThe current directory can also be treated as a file.
@echo off
for/r c \%%i in (*.exe) do echo%%i
Let's save this bat to the D-disk, anywhere, and we'll see that he put the C-packing directory and the subdirectories under each directoryall the EXE files are listed here.
@echo off
for/r%%i in (*.exe) do @echo%%i
search path with current directory
@echo off
for/r c \%%i in (boot. ini) do echo%%i
Enumerate all directories on the C-disk, in order to enumerate only the directories that exist for the boot. INI, you can change:
@echo off
for/r c \%%i in (boot. ini) do if exist%%i echo%%i
It's nice to search for files with this command ...

DOS Loop for command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.