Previously, the command line function of DOS was often too weak, and it could not be used as a very complex operation with the command line as unix. In fact, when MS starts the command line from Win2k, it has been drawn on quite many UNIX's advantages, although it is not so flexible, but it has completed the vast majority of tasks, such as && and | Two (or more) commands, determine if the next return value is executed, and so on. In these enhancements, the most obvious is the for command.
For example, use the appropriate parameters to use the for command to turn the DATE / T output from "SAT 07/13/2002" into the format you want, for example, "2002-07-13":
C: /> for / f "tokens = 2, 3, 4 delims = /"% a in ('Date / T') do @echo% C-% A-% B
2002-07-13
This example will be described in detail in (3).
0. Basic application
Simply put, for is a loop, you can use a series of commands with the loop range you specify. The simplest example is that manually specifies the loop range, and then executes the specified command for each value. For example, want to quickly report the remaining space of each hard disk partition:
For% a in (C: D: E: f :) do @dir% a / | find "bytes free"
Will output:
8 DIR (s) 1,361,334,272 bytes Free
15 DIR (s) 8,505,581,568 bytes Free
12 DIR (s) 12,975,149,056 bytes free
7 Dir (s) 11,658,854,400 bytes free
Use it to make some commands that do not support wildcards. In WIN9X, the Type command (display file content) is not supported * .txt this format (Win2K Start Type has supported wild). You can use for for similar situations:
For% a in (* .txt) do type% a
These are not the most powerful features. I think it is the most powerful feature, which is 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 use the text file content as a loop range
3. You can use the / F parameter to perform the result of a command as a loop range
4. You can use% to the operator to separate file names, extensions, drive et al.
The exemplifications are now illustrated as follows:
1. Traversed with / R Traversed Tree
When a file name and other filemates such as *. * Or * .txt are used as the loop range of the FOR / R, all files (including files in the subdirectory) can be operated. For example, you want to find "BlueBear" words in all TXT files (including subdirectories) of the current directory, but because Find itself can't travel through the Licen Directory, we use for:
FOR / R.% a in (* .txt) do @find "blueber"% a
The @ just in front of Find allows the output not including the find command itself. This is the function of DOS very early. Unrelated to for.
When used. As the cycle range, for only the structure of the subdirectory (directory name) as a loop range, without including the file inside. It's a bit like the Tree command, but the side focus is different. Tree's focus is with very beautiful and readable formats, while the output is suitable for some automatic tasks, for example, we all know that there is a cvs directory in each subdirectory, sometimes we are released I want to remove all of these CVS directories: for / r.% A in (.) Do @IF EXIST% A / CVS RD / S / Q% A / CVS
Judging with if exist, because the for is only listed for each directory, if some directory does not have CVS below, it will be executed. Judging by if EXIST is more secure.
This delete command is too powerful, please use it. It is best to list RD / S / Q to @echo first listen to the directory to be deleted before the delete command is really performed.
FOR / R.% a in (.) do @IF EXIST% A / CVS @echo% A / CVS
There will be a layer of ".", Such as C: / Proj / Release /./ CVS, but does not affect the execution of the command.
2. Execute the result of a file or command as a loop range:
If you have a file Todel.txt, it is a list of files to be deleted, and now you want to delete each file listed. Suppose this file is a line of each file, like this:
C: /TEMP/A1.TXT
C: /TEMP/A2.TXT
C: /TEMP/SUBDIR/B3.TXT
C: /TEMP/SUBDIR/B4.TXT
Then you can use for to complete:
FOR / F% a in (Todel.txt) Do Del% a
This command can also be more powerful. For example, your Todel.txt is not as clean like the above example, but by DIR directly, there are some information that is useless, such as this:
Volume In Drive D Is Data
Volume Serial Number IS C47C-9908
Directory Of D: / TMP
09/26/2001 12:50 PM 18,426 alg0925.txt
12/02/2001 04:29 AM 795 BSample.txt
04/11/2002 04:18 AM 2,043 Invitation.txt
4 File (s) 25,651 bytes
0 DIR (s) 4,060,700,672 bytes free
FOR still can solve the file names and operate:
FOR / F "SKIP = 5 tokens = 5"% a in (Todel.txt) Do @IF EXIST% A DEL% A
Of course, the above command is deleted. If you just want to see which files will be operated, replace DEL to Echo:
FOR / F "SKIP = 5 tokens = 5"% a in (Todel.txt) Do @if exist% a echo% a
You will see:
ALG0925.TXT
Bsample.txt
INVITATION.TXT
Skip = 5 indicates that the top 5 lines (that is, the head information of DIR output), tokens = 5 indicates that the column of each row is placed in% A, just a file name. Here I added a file existence, because the last line of "free" is also the 5th column, and I can't think of a good way to filter out the last two lines, so check if it can be guaranteed. 3. You can use the / F parameter to perform the result of a command as a loop range
Very useful feature. For example, we want to know which names have the current environment variable (we only need the name, do not value). But the output of the set command is the "name = value" format, and now you can use for only the name part:
For / f "delims =="% i in ('set') do @echo% i
Will see:
AllUsersprofile
Appdata
Classpath
Commonprogramfiles
Computername
COMSPEC
Dircmd
Homedrive
......
Here is the result of the SET command to be used as a loop range. Delims == Indicates that use = as a separator, since FOR / F is the first token per line by default, the variable name can be separated. If you want to only list values:
FOR / F "Delims == tokens = 2"% I in ('set') do @echo% i
TOKENS = 2 is the same as the previous example, indicating that the second column (by = septum) is used as a loop value.
A more useful example:
We know that DATE / T (/ T means not to ask the user input) output is like this:
SAT 07/13/2002
Now I want to separate the date, that is, 13:
FOR / F "tokens = 3 Delims = /"% a in ('Date / T') do @echo% a
In fact, replace Tokens to 1, 2, 3 or 4, you will get SAT, 07, 13, and 2002, respectively. Note that Delims = / There is also a space, indicates / and spaces are separator. Since this space Delims must be the last item of the / f option.
A more flexible, like the beginning of this article, use the date of 2002-07-13:
FOR / F "tokens = 2, 3, 4 delims = /"% a in ('Date / T') do @echo% C-% A-% B
When TOKENS is followed by multiple values, it will be mapped to% a,% b,% c, etc., respectively. In fact, it is related to the variable you specified. If you specify% i, they will use% i,% j,% k, etc.
Flexible application, almost no thing can't do.
4. You can use% to the operator to separate file names, extensions, drive et al.
This is relatively simple, that is, automatically separated the value of the cyclic variable into as long as the file name, as long as the name or the like is extension.
Example: To list all MP3's songs under C: / MP3, if you use general DIR / B / S or FOR / R, it will be like this:
g: / mp3 / archived-18-01-a / tour Hongming - Xiasha / Tour Hongming -01 under the sand. MP3
g: / mp3 / archived-18-01-a / tour Hongming - Xiasha / Tour Hongming -02 21 people. MP3
......
G: / MP3 / Archived-18-01-A / Fafei - Fables / Faye Wong - Ashura. MP3G: / MP3 / Archived-18-01-A / Fourtament / Fame - Tribun flower. MP3
g: / mp3 / archived-18-01-A / Fan Fable / Faye Wong - I don't love me. MP3
......
If I only have the song name (not the path and ".mp3"):
Tour Hongming -01 under the sand
Tour Hongming-02 21
......
Faye Wong - Ashura
Faye Wong -
Faye Wong - I don't love me.
......
So you can use for commands:
FOR / R G: / MP3% a in (* .mp3) do @echo% ~ NA
Anyone who starts with% to the beginning of the file name is separated. Please see for /? Help for details.
Examples of this paper may have no practical use, or otherwise completed. It is only used to reflect the FOR without using other tools, and only the DOS command combination can complete the fairly flexible task.
Please see FOR /? Help for details.