DOS command - "for" - Advanced application example
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, available for commands to turn the DATE / T output from "SAT 07/13/2002" into the format you want, such as "2002-07-13": C: /> for / f "tokens = 2, 3, 4 delims = /"% a in ('Date / T') Do @ech / t ') Do @echo% C-% A-% B 2002-07-13 This example will be detailed in (3) . 0. Basic applications Simply said that for is a loop, you can generate a range 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 can make some commands that do not support wildcards are operated on a series of files. In Win9x, Type commands (display file content) Is not supported * .txt This format (Win2K start Type has supported wild). You can use for: for% a in (* .txt) do type% a most powerful for: for% a in (* .txt) Do Type%. Function. 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 / F parameters Execute the result of a certain command as a loop range 4. You can separate the file name into a file name, extension, and the independent portion of the file name, extension, drive letter, and other independent parts: 1. Use / R Traversal Tree Use * . * Or * .txt file name wildcard When the FOR / R cycle range, you can operate all files (including files in the subdirectory) in the current directory. For example, you want all TXT files in the current directory. (Including subdirectory) content lookup "BlueBear" words, but because Find itself cannot travel through the Lapding Directory, we use for: for / r.% A in (* .txt) do @find "blueber"% a find front @ Just let the output result does not include the Find command itself. This is the function of DOS very early. He is independent. When it is used. For the loop range, for only the structure of the subdirectory (directory name) as a loop range, not Including the file inside. It is a bit like the Tree command, but the side focus is different. Tree's focus is to use very beautiful and readable format, while the output is suitable for some automatic tasks, for example, we all know the project of CVS management, each There will be a CVS directory under the catalog, sometimes we want to remove all these CVS directories when the software is released: for / r.% A in (.) Do @IF EXIST% A / CVS RD / S / Q% A / CVSJudging 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 actually implemented. After confirming that there is no error, then change back to RD / S / Q: for / r.% A in DO @IF EXIST% A / CVS @echo% A / CVS may 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 row 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 do it with for / f% a in (Todel.txt) Do Del% a. This command can be more powerful. For example, your Todel.txt is not as clean as above, 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:19 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 can still solve file names and operate: for / f "Skip = 5 tokens = 5"% a in (Todel.txt) do @IF EXIST% A DEL% a Of course, this command Is to delete, if you just want to see which files will be operated, change the 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 the top 5 lines (the head information of DIR output), tokens = 5 indicates that the 5th column of each row is placed as a loop value % 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 function of a loop range. For example, we want to know which names have the current environment variable (we only need the name, do not value).
However, the output of the set command is the format of "name = value", 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 ... This 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 only: for / f "delims == tokens = 2"% I in ('set') DO @echo% I tokens = 2 is the same, indicating the second column (by = as separator As the cycle 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 part, that is, 13: for / f " Tokens = 3 delims = / "% a in ('Date / T') do @echo% a actually replaced 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, as mentioned in this article, use the date 2002-07-13 format output: for / f "tokens = 2, 3, 4 delims = /"% a in ('Date / T') do @ ECHO% C-% A-% B When the token 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 separate the file name, extended name, drive letter, etc. and many more.