Use the shell for loop command in Windows

Source: Internet
Author: User

This article is great for people who have been using bash in Linux for a long time. The for command has powerful functions!

Self, http://hi.bccn.net/space-21499-do-blog-id-13524.html

For allows some commands that do not support wildcards to operate on a series of files. In Win9x, the type Command (display file content) does not support the *. txt format (the wildcard is supported for the start type of Win2k ). In a similar situation, you can use:

For % A in (*. txt) do type %

These are not the most powerful for functions. I think its most powerful features are manifested in the following advanced applications:

1. You can use the/R parameter to traverse the entire directory tree.
2. You can use the/F parameter to take the text file content as the loop range.
3. You can use the/F parameter to take the execution result of a command as the loop range.
4. You can use % ~ The operator separates file names into separate parts, such as file names, extensions, and drive letters.

Examples are as follows:
1. Use/R to traverse the directory tree
If you use wildcard characters such as *. * or *. txt as the loop range of for/R, you can operate on all files (including files in subdirectories) in the current directory. For example, you want to search for the "bluebear" in all TXT files (including subdirectories) in the current directory. However, because find itself cannot traverse subdirectories, we use:

For/R. % A in (*. txt) Do @ find "bluebear" %

The @ in front of find is to make the output result not include the find command itself. This is an early dos feature. It has nothing to do with.
When "." is used as the loop range, "for" only uses the structure (directory name) of sub-directories as the loop range, excluding the files in it. It is a bit like the Tree Command, but it has different focuses. Tree focuses on output in beautiful and easy-to-read format, while for output is suitable for some automatic tasks. For example, we all know that in projects managed by CVS, each subdirectory has a CVS directory. Sometimes we want to remove all these CVs directories during software release:

For/R. % A in (.) Do @ if exist % ACVs RD/S/Q % ACVs

Use if exist to check whether the for statement lists each directory mechanically. If CVS is not found in some directories, the statement is executed. Use if exist to determine whether it is safer.
This DELETE command is too powerful. Please use it with caution. It is best to replace RD/S/Q with @ echo before executing the preceding DELETE command to list the directories to be deleted, and then replace them with RD/S/Q:

For/R. % A in (.) Do @ if exist % ACVs @ echo % ACVs

There may be an extra "." in the directory, such as C: proj elease. CVs, but the command execution effect will not be affected.
2. Take the content of a file or command execution result as the loop range:
Suppose you have a file named todel.txt, which contains a list of all objects to be deleted. Now you want to delete all the objects listed in it. Assume that each file name occupies one row, as shown in the following figure:

C: empa1.txt
C: empa2.txt
C: empsubdir3.txt
C: empsubdir4.txt

You can use for:

For/F % A in (todel.txt) Do del %

This command can also be more powerful. For example, your todel.txt is not as clean as in the preceding example, but is directly generated by Dir, which has some useless information, such:

Volume in drive D is Data
Volume serial number is C47C-9908
Directory of D: MP
09/26/2001 12:50 pm 18,426 alg0925.txt
12/02/2001 am 795 bsample.txt
04/11/2002 am 2,043 invitation.txt
4 file (s) 25,651 bytes
0 Dir (s) 4,060,700,672 bytes free

For can still solve the file name and perform the operation:

For/F "Skip = 5 tokens = 5" % A in (todel.txt) Do @ if exist % A del %

Of course, the above command is being deleted. If you just want to see which files will be operated, replace del with ECHO:

For/F "Skip = 5 tokens = 5" % A in (todel.txt) Do @ if exist % A echo %

You will see:

Alg0925.txt
Bsample.txt
Invitation.txt

Skip = 5 indicates that the first five rows (that is, the information in the Dir output header) are skipped. tokens = 5 indicates that 5th columns of each row are placed as the loop value in % A, which is the name of the file. Here, I added a file for judgment, because the "free" in the last row is exactly 5th columns. Currently, I still cannot find a good way to filter out the last two rows, so check whether it is safe.
3. You can use the/F parameter to take the execution result of a command as the loop range.
Very useful features. For example, we want to know the names of the current environment variables (we only need the names, not the values ). However, the output of the SET command is in the format of "name = value". Now you can use for to retrieve only the name part:

For/F "delims =" % I in ('set') Do @ echo % I

You will see:

Allusersprofile
Appdata
Classpath
Commonprogramfiles
Computername
Comspec
Dircmd
Homedrive
......

Here we use the execution result of the SET command as the loop range. Delims = indicates that = is used as the separator. Because for/F uses the first token in each line by default, variable names can be separated. To list only values:

For/F "delims = tokens = 2" % I in ('set') Do @ echo % I

The tokens = 2 is the same as the separator, indicating that the second column (by = as the separator) is used as the loop value.
Here is a more useful example:
We know that the output of date/t (/T indicates not to ask the user for input) is like this:

Sat 07/13/2002

Now I want to separate the date part, that is, 13:

For/F "tokens = 3 delims =/" % A in ('date/t') Do @ echo %

In fact, after tokens is changed to 1, 2, 3, or 4, you will get sat, 07, 13, and 2002 respectively. Note that delims =/is followed by a space, indicating that the separator between the separator and the space. Because the space delims must be the last item of the/F option.
A little more flexible, as mentioned at the beginning of this article, output the date in the format:

For/F "tokens = 2, 3, 4 delims =/" % A in ('date/t') Do @ echo % C-% A-% B

When tokens is followed by multiple values, it is mapped to % A, % B, % C, and so on. It is actually related to the variables you specified. If you specify % I, they will use % I, % J, % K, etc.
Flexible application is almost impossible.
4. You can use % ~ The operator separates file names into separate parts, such as file names, extensions, and drive letters.
This is relatively simple, that is to say, to automatically separate the values of the cyclic variables as long as the file name, as long as the extension, or as long as the drive letter and so on.
For example, you need to list all MP3 song names under C: MP3. If you use the normal DIR/B/S or for/R, the following will be the case:

G: mp3Archived-18-01-A youhongming-Xiasha youhongming-01 Xiasha
G: mp3Archived-18-01-A youhongming-Xiasha youhongming-02 21
......
G: mp3archived-18-01-awangfei
G: maid
G: mp3archived-18-01-awangfei-Wang yiyan-I do not love me
......

If I only need the song name (do not use the path or ". MP3 "):

You hongming-01 Xiasha
You hongming-02 21 people
......
Faye Wong-Asura
Faye Wong-peganhua
Faye Wong-do not love me I do not love
......

You can use the for command:

For/r g: MP3 % A in (*. mp3) Do @ echo % ~ Na

All % ~ All operators that start with are separated by file names. For details, see /? Help.
Some examples in this article may not be of practical use or can be completed in other ways. It is only used to reflect for and can complete quite flexible tasks without using other tools. It can only be combined with doscommands.

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.