Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

For tutorials


May 23, 2021 DOS Command learning manual



Source: Http://www.cn-dos.net/forum/viewthread.php?tid=19331


Landlord: Strongest dos command - for (a good source for nods to learn FOR statements)


This article is found on the Internet, many friends of the first-time DOS on the FOR statement master is not too familiar with, I hope this article can give you open ideas to help you learn the FOR statement well, the main parameters of the introduction and examples are very clear, but also hope to DOS more and more interested---> play your imagination


The body begins


It used to be felt that the command-line functionality of DOS was too weak to perform very complex operations on the command line like UNIX. I n fact, when MS began to enhance the command line from WIN2K, it had learned a considerable number of UNIX advantages, and although it was not as flexible as UNIX, the vast majority of tasks, such as connecting two (or more) commands, with the previous return value to determine whether the next one was executed, and so on. O f these enhancements, the most obvious is the FOR command.

For example, with the appropriate parameters, you can use the FOR command to change the output of date /t from "Sat 07/13/2002" to 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 is detailed in (3).

0. Basic applications

Simply put, FOR is a loop that can generate a series of commands using the loop range you specify. T he simplest example is to manually specify the loop range and then execute the specified command for each value. F or example, you want to quickly report the remaining space for each hard drive 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


It allows some commands that do not support wildcards to manipulate a series of files. I n WIN9X, the TYPE command (which displays the contents of the file) does not support the format of .txt (WIN2K starts TYPE has supported the match). I n a similar situation, you can use FOR:


for %a in (*.txt) do type %a


These are not for the most powerful features of FOR. I think it's the most powerful feature in these advanced applications:


1. You can traverse the entire tree with the /r parameter

2. You can use the /f parameter to circle the contents of the text file

3. A command execution result can be used with the /f parameter as a loop range

4. You can separate the file names into separate parts such as file names, extensions, disk characters, and so on, using the % to operator


Here are some examples:

1. Traverse the tree with /r

You can manipulate all files in the current directory, .txt including those in the subdirectory, when you use a file name wildcard, such as a file name wildcard, such as . . . or a wildcard for /r. F or example, you want to find the word "bluebear" in all txt files (including subdirectts) in the current directory, but since the find itself cannot traverse the subdirecte, we use for:


for /r . % a in (*.txt) do @find "bluebear" %a


Find the previous s just so that the output does not include the find command itself. T his is a feature that DOS has had for a long time. I t has nothing to do with FOR.

When using . W hen it is a loop scope, for only the structure of the subdirectory (directory name) is used as the loop scope, not the files inside. I t's a bit like the TREE command, but with a different focus. T REE's focus is on output in a beautiful, easy-to-read format, and FOR's output is suitable for automated tasks, for example, we all know that in projects managed with CVS, there is a CVS directory in each subdirect directory, and sometimes when the software is released we want to get rid of all of these CVS directories:


for /r . % a in (.) do @if exist %a\CVS rd /s /q %a\CVS


Let's start by using if exist, because for is just a mechanical listing of each directory, and if there are some directories below CVS, it will be executed. I t's safer to judge with if exist.

This delete command is too powerful, please use it with care. I t's a good idea to replace rd /s /q with @echo before actually executing the above delete command, list the directory you want to delete first, confirm that it's correct, and then return to rd /s /q:


for /r . % a in (.) do @if exist %a\CVS @echo %a\CVS


Perhaps there will be an more layer of "." in the directory, such as c:?proj\release\?CVS, but it will not affect the execution of the command.

2. Use the contents of a file or the result of command execution as a loop:

If you have a file todel .txt, which is a list of all the files to be deleted, now you want to delete every file listed inside. S uppose this file is one line per file name, like this:


c:\temp\a1.txt

c:\temp\a2.txt

c:\temp\subdir\b3.txt

c:\temp\subdir\b4.txt


So you can do this with FOR:


for /f %a in (todel.txt) do del %a


This command can also be more powerful. F or example, .txt is not as clean as the example above, but is generated directly by DIR, and there is some useless information, 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 can still unseed the file name and take action:


for /f "skip=5 tokens=5" %a in (todel.txt) do @if exist %a DEL %a


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 %a


You'll see:


alg0925.txt

bsample.txt

invitation.txt


Skip=5 means skipping the first 5 rows (that is, the header information of the DIR output), and tokens=5 means putting the fifth column of each row into %a as a circular value, which is exactly the file name. H ere I added a file existence judgment, because the last row of "free" is just the fifth column, at present can not think of a good way to filter out the last two lines, so check to be safe.

3. A command execution result can be used with the /f parameter as a loop range

Very useful features. F or example, we want to know what names are in the current environmental variables (we just need names, not values). H owever, the output of the SET command is in the format of "Name- Value" and can now be used for for only the name part:


FOR /F "delims==" %i IN ('set') DO @echo %i


You'll see:


ALLUSERSPROFILE

APPDATA

CLASSPATH

CommonProgramFiles

COMPUTERNAME

ComSpec

dircmd

HOMEDRIVE

......


Here is the result of the set command execution as a loop range. D elims means that the variable name can be detached because FOR/F defaults to the first TOKEN per row. I f you want to list only the 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 the s/he separator) is used as a circular value.

Another, more useful example:

We know that the output of date /t (/t for not asking the user for input) is something like this:


Sat 07/13/2002


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


for /f "tokens=3 delims=/ " %a in ('date /t') do @echo %a


Actually replace tokens with 1, 2, 3 or 4 and you'll get Sat, 07, 13 and 2002, respectively. N otice that there is a space after the delims/, indicating that /and spaces are separators. S ince this space delims must be the last item of the /f option.

Be flexible and, as mentioned at the beginning of this article, output the date in the format of 2002-07-13:


for /f "tokens=2,3,4 delims=/ " %a in ('date /t') do @echo %c-%a-%b


When tokens are followed by multiple values, they are mapped to %a, %b, %c, and so on. I t's actually about the variables you specify, and if you specify %i, they'll use %i, %j, %k, and so on.

There is little you can do to apply this flexibly.

4. You can separate the file names into separate parts such as file names, extensions, disk characters, and so on, using the % to operator

This is relatively simple, that is, the value of the loop variable is automatically separated into as long as the file name, as long as the extension, or as long as the disk character and so on.

Example: To list all mp3 song names under c:\mp3, this would be the case if you used the general dir /b/s or for /r:


g:?mp3\Archived?05-18-01-A?Yu Hongming-Seosa-Yu Hongming-01 Seha .mp3

g:?mp3\Archived?05-18-01-A?Yu Hongming-Seosa-Yu Hongming-02 21 .mp3

......

g:'mp3'Archived'05-18-01-A'Wang Fei-Fyw-Fable'-Wang Fei-Asuro.mp3

g:'mp3'Archived'05-18-01-A'Wang Fei-Fyfe's fable?Wang Fei-.mp3

g:'mp3'Archived'05-18-01-A'-Wang Fei-Fyfe-Don't Love Me I Don't Love .mp3

......


If I just want the song name (don't path .mp3):


Yu Hongming-01 Under the Sand

Yu Hongming -02 21 people

......

Wang Fei-Asuro

Wang Fei - Flowers on the other side

Wang Fei - Don't love me I don't love

......


Then you can use the FOR command:


for /r g:\mp3 %a in (*.mp3) do @echo %~na


Any operator from % to the beginning is a separation of file names. S ee for /? H elp.

Some of the examples given in this article may not be of practical use or may be done by other means. Using FOR alone can accomplish fairly flexible tasks with only a combination of DOS commands without the help of other tools