The for command that resolves dos in Windows Windows uses

Source: Internet
Author: User
Tags eol uppercase letter

Directory structure:

contents Structure [+]
    1. Brief introduction
    2. For/d ...
      1. Case
        1. Case: Print all folder names under the c://root directory
        2. Case: Print a folder name with only 1-3 letters under the current path
        3. Case: Show all folder names starting with window
    3. For/r ...
      1. Case
        1. Case: Displays all filenames that end with an. exe in the current directory and sub-directories
        2. Case: Displays all directory names for the current directory and subdirectories
    4. For/l ...
      1. Case
        1. Case: 5 + cmd windows added
    5. Listening Menu keys
      1. Delims keywords
      2. Tokens keywords
      3. Skip and EOL Keywords
I. INTRODUCTION

In the DOS window, enter for/? To view the official instructions for the use of the for command.

For%variable in (set) does command [command-parameters]%variable specifies a single-letter replaceable parameter. (set)      Specifies one or a set of files. You can use wildcard characters (* and?).    command specifies the commands to execute on each file. Command-parameters  


We know the basic format of a for command is: forparameter% variable name in (related file or command) do execute command . It is important to note that in the DOS window and in the batch file, the format of the for command is somewhat different, in the DOS window, the variable is represented by a single percent sign plus a letter, but in a batch file, the variable is represented by a double percent sign and a letter, and the basic format of the for command in the batch file is: forparameter The percent variable name in the (related file or command) do command .
There are four parameters, namely/d,/R,/L,/F, and the specific format of the for command corresponding to each parameter is different. The next step is to explain the use of each of these parameters:
All subsequent cases, unless specifically stated, are in the form of batch files.

Two. For/d ...

/d indicates a directory search, which is used primarily for searching directories and does not search for files.
The format for the for command is: for/d%%variable in (set) do command
If the set contains wildcards, the execution of each directory that the set wants to match command,%%variable represents each directory.

2.1 Case Study: Print all folder names under the c://root directory
@echo Offrem into the C packing directory C:rem print all the folder names under the current directory for/d%%i in (*) do @echo%%ipause
Case: Print a folder name with only 1-3 letters under the current path
@echo offfor/d%%i in (???) Do @echo%%ipause
Case: Show all folder names starting with window
@echo Offrem into the C drive C:rem Show all folder names starting with Window for/d%%i in (window?) do @echo%%ipause
Three. For/r ...

/R represents recursion, recursively performing operations on each directory in the current directory.
The format for the for command is: FOR/R [[Drive:]path]%%variable in (set) do command
The brackets [] are optional, and if you do not specify the root of the start recursion, the current directory is the root of the recursion.
It is important to note that if set is a point (.), only the directory tree is enumerated.

3.1 Case Study: Displays all filenames ending with. exe in drive C
@echo offfor/r C:%%i in (*.exe) do @echo%%ipause
Case: Displays all filenames that end with an. exe in the current directory and sub-directories
@echo offfor/r%%i in (*.exe) do @echo%%ipause
Case: Displays all directory names for the current directory and subdirectories
@echo offfor/r  %%i in (.) Do @echo%%ipause


It is now clear that the/D and/R parameters are identical, they are all related to the directory, but/d will only retrieve all directories in the current directory, but/R can retrieve all directories recursively.

Four. For/l ...

/L can be written as a lowercase letter/L, in order to distinguish it from the number 1, the following is the uppercase letter L.
/L represents an iterative variable that iterates over the data at a given start, step, and end.
For command format: for/l%%variable in (start#,step#,end#) do command
Use the iteration variable to set the starting value (start#), and then step through a range of values until the value exceeds the set termination value (end#). /l will perform iteration variables by comparing start# to end#. If start# is less than end#, the command is executed.

4.1 Case Study: printing 1 to 5
@echo offfor/l%%i in (1,1,5) do @echo%%ipause
Case: 5 + cmd windows added
@echo offfor/l%%i in (1,1,5) do start cmdpause


Will find that the new addition of 5 cmd window, if (1,1,65535), will open more than 65,535 cmd window. If you change the above start CMD to MD%%i, you can create more than 5 folders.

Five. For/f ...

The for command with/F has the most use,
The format of the for command:

for/f ["Options"]%%i in (file) does commandfor/f ["Options"]%%i in ("string") does commandfor/f ["Options"]%%i in (comma nd) do commandfile represents one or more file strings representing the string command on behalf of commands ["Options"] optional


The three formats for the for command are all Chase small, in (file), in ("string"), and in (command), respectively, to read the data from the file, string, commands.

File is the file name, according to the official, for will be opened in file files, and before the next file to read each file into memory, according to each line into a single element, ignoring blank lines, see an example.

If the file a.txt has the following content:
1th row 1th column 1th row 2nd column 1th row 3rd column
2nd row 1th column 2nd row 2nd column 2nd row 3rd column
3rd row 1th column 3rd row 2nd column 3rd row 3rd column


You can use the type command to display:

@echo Offtype A.txtpause

Shown below:

1th row 1th column 1th row 2nd column 1th row 3rd column 2nd row 1th column 2nd row 2nd column 2nd row 3rd column 3rd row 1th column 3rd row 2nd column 3rd row 3rd column

You can also do this with the for command:

@echo offfor/f "delims=\n"%%i in (a.txt) do echo%%i pause

Where delims=\n means splitting with newline characters,

@echo offfor/f%%i in ("Asas    daa") do @echo%%ipause

The above statement will show Asas.

Delims keywords

The Delims keyword denotes a delimiter, and it is important to note that if you do not specify Delims, the default is split with a space and TAB key. If you use spaces to display in the following format:

@echo offfor/f "delims="%%i in (a.txt) do echo%%i pause

Show:

1th row 1th column 2nd row 1th column 3rd row 1th column

You can see that the value shows the value of the first column

Tokens keywords

The tokens command can specify which column of data to display, and the command also supports wildcard characters (*)

@echo offfor/f "tokens=2,3 delims="%%i in (a.txt) do @echo%%i%%jpause

Effect:

1th row 2nd Column 1th row 3rd column 2nd row 2nd column 2nd row 3rd column 3rd row 2nd column 3rd row 3rd column

In the above command more%%j, this is the for command automatically added, through the "tokens=2,3 delims=" means a space as a split symbol, take the rows of the 2nd and 3rd columns of data. and assigns the data of the column to%%i, assigns the data of the third column to the%%j. If you want to display the second and third columns in the same column, you can change the tokens=2,3 to tokens=2-3.

For wildcards, this is the row or all the remaining elements as a column of data.

@echo offfor/f "tokens=1,*"%%i in  (a.txt) do @echo%%i%%jpause

Show:

1th row 1th column 1th row 2nd column 1th row 3rd column 2nd row 1th column 2nd row 2nd column 2nd row 3rd column 3rd row 1th column 3rd row 2nd column 3rd row 3rd column
Skip and EOL Keywords

Skip is the number of rows to ignore before the file, and EOL is used to specify what symbol to start with, ignoring it.

@echo offfor/f "skip=2 tokens=*"%%i in (a.txt) do @echo%%ipause



The for command that resolves dos in Windows Windows uses

Related Article

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: info-contact@alibabacloud.com 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.