4. For loop command, as long as the conditions meet, it will execute the same command multiple times.
grammar:
Execute a specific command on each file in a set of files.
FOR %%variable IN (set) DO command [command-parameters]
%%variable Specifies a single letter replaceable parameter.
(set) Specify one or a group of files. Wildcards can be used.
command Specifies the command to be executed on each file.
command-parameters
Specify parameters or command line switches for a specific command.
For example, there is a line in a batch file:
for %%c in (*.bat *.txt) do type %%c
Then this command line will display the contents of all files with bat and txt extensions in the current directory.
==== willsort Commentary Notes======================================================================
It should be pointed out that when the string in () is not a single or multiple file names, it will be simply replaced as a string. This feature plus the feature that can embed multiple strings in () is obviously for can be regarded as a traversal loop.
Of course, in the command line environment of the nt/2000/xp/2003 series, for is given more features, allowing it to analyze command output or strings in files, and many switches are also used to extend the file replacement function.
=======================================================================
Batch Processing Example
1. IF-EXIST
1) First use notepad to create a batch file in C:\, with the file contents as follows:
@echo off
IF EXIST \ TYPE \
IF NOT EXIST \ ECHO \ does not exist
Then run it:
C:\>
If C:\ file exists, then its content will be displayed. If it does not exist, the batch process will prompt you that the file does not exist.
2) Then create another file, the content is as follows:
@ECHO OFF
IF EXIST \%1 TYPE \%1
IF NOT EXIST \%1 ECHO \%1 does not exist
implement:
C:\>TEST2
The command run result is the same as above.
illustrate:
(1) IF EXIST is used to test whether the file exists, and the format is
IF EXIST [Path+File Name] Command
(2) %1 in the file is a parameter. DOS allows passing 9 batch parameter information to the batch file, which is %1~%9 (%0 represents the test2 command itself). This is a bit like the relationship between actual parameters and formal parameters in programming. %1 is a formal parameter, which is a real parameter.
==== willsort Commentary Notes======================================================================
DOS does not have a limit to "allowing 9 batch parameter information to pass". The number of parameters will only be limited by the length of the command line and the processing capability of the command being called. However, in the batch program, we can only reference 10 parameters at the same time at the same time, because DOS only gives ten parameter reference characters, %0~%9.
========================================================================
3) Going further, create a file named as follows:
@echo off
IF "%1" == "A" ECHO XIAO
IF "%2" == "B" ECHO TIAN
IF "%3" == "C" ECHO XIN
If running:
C:\>TEST3 A B C
The screen will display:
XIAO
TIAN
XIN
If running:
C:\>TEST3 A B
It will be displayed on the screen
XIAO
TIAN
During this command execution, DOS will assign an empty string to parameter %3.
2、IF-ERRORLEVEL
Established, the content is as follows:
@ECHO OFF
XCOPY C:\ D:\
IF ERRORLEVEL 1 ECHO Failed to copy the file
IF ERRORLEVEL 0 ECHO Successfully copied the file
Then execute the file:
C:\>TEST4
If the file is copied successfully, the screen will display "File copy successfully", otherwise the file copy will be displayed.
IF ERRORLEVEL is used to test the return value of its previous DOS command. Note that it is only the return value of the previous command, and the return value must be judged in order from large to small.
Therefore, the following batch file is wrong:
@ECHO OFF
XCOPY C:\ D:\
IF ERRORLEVEL 0 ECHO Successfully copied the file
IF ERRORLEVEL 1 ECHO No copy file found
IF ERRORLEVEL 2 ECHO Users aborting copy operation through ctrl-c
IF ERRORLEVEL 3 ECHO Preset errors prevent file copying operations
IF ERRORLEVEL 4 ECHO Write disk error during copying
Regardless of whether the copy is successful or not, the following:
No copy file found
User aborts the copy operation through ctrl-c
Preset errors prevent file copying operations
Writing error during copying
All will be displayed.
The following are the return values of several commonly used commands and their representative meanings:
backup
0. The backup was successful
1. No backup file found
2 File sharing conflict prevents backup from completing
3 Users abort backup with ctrl-c
4. The backup operation is aborted due to a fatal error.
diskcomp
0 The disk is the same
1. The plate is different
2 User aborted the comparison operation through ctrl-c
3 The comparison operation is aborted due to a fatal error
4 Preset error abort comparison
diskcopy
0 Disk copy operation is successful
1. Non-fatal disk read/write error
2 The user ends the copy operation through ctrl-c
3. The disk copy was aborted due to fatal processing errors.
4 Preset errors prevent copy operations
format
0 Format successful
3 User aborted formatting processing through ctrl-c
4 The formatting is aborted due to fatal processing error
5. Under the prompt "proceed with format(y/n)?", the user type n to end.
xcopy
0 Successfully copied the file
1 No copy file found
2 The user aborted the copy operation through ctrl-c
4 Preset errors prevent file copying operations
5. Writing errors during copying
chkdsk
0 No error found
255 One or more errors found
choice
0 User pressing ctrl+c/break
1 The user presses the first key
255 Error condition detected in the command line
Others The position of valid characters pressed by the user in the list
defrag
0. Fragment compression is successful
1 Internal error occurred
2 There are no empty clusters on the disk. To run DEFRAG, at least one empty cluster
3. Users use Ctrl+C to exit DEFRAG
4 General error occurred
5 DEFRAG encountered an error while reading the cluster
6 DEFRAG encountered an error while writing cluster
7 There is something wrong with allocating space
8 Memory error
9 There is not enough space to compress disk fragmentation
deltree
0 Successfully deleted a directory
diskcomp
0 The two plates are the same
1. Discover the difference
2 Press CTRL+C to terminate the comparison
3. Serious error occurred
4 Initialization error occurred
find
0 The search was successful and at least one matching string was found.
1. The search was successful but no matching string was found.
2 An error occurred during the search
keyb
0 The keyboard definition file is loaded successfully
1 Illegal keyboard code, character set or syntax was used
2 The keyboard definition file is bad or not found
4. An error occurred during communication between keyboard and monitor
5 The required character set is not ready
move
0 Successfully moved the specified file
1 An error occurred
msav /N
86 The virus was checked
replace
0 REPLACE successfully replaced or added the file
1. MS-DOS version is incompatible with REPLACE.
2 REPLACE cannot find the source file
3. REPLACE cannot find the source path or the target path.
5. The files to be replaced cannot be accessed.
8. The memory cannot be enough to execute REPLACE
11 Command line syntax error
restore
0 RESTORE successfully restored the file
1 RESTORE The file to be restored cannot be found
3. The user presses CTRL+C to terminate the recovery process
4 RESTORE terminated due to error
scandisk
0 ScanDisk did not detect any errors on the drive it checked
1. ScanDisk cannot be run due to incorrect syntax of the command line.
2 ScanDisk terminates unexpectedly due to memory exhaustion or internal errors.
3 Users let ScanDisk exit midway
4. When performing disk scanning, the user decides to exit early.
254 ScanDisk found disk failure and has been corrected all
255 ScanDisk found a disk failure, but failed to correct all
setver
0 SETVER successfully completed the task
1 The user has specified an invalid command switch
2 The user specifies an illegal file name
3. There is not enough system memory to run the command
4 The user specifies an illegal version number format
5 SETVER not found the specified item in the version table
6 SETVER file not found
7 The user has specified an illegal drive
8 The user has specified too many command line parameters
9 SETVER detected missing command line parameters
10 When reading the file, SETVER detects an error.
11 File corruption
12 The specified file does not support the version table
13 There is not enough space in the version table to store new items
14 SETVER detects an error while writing a file.
========================================================================
3、IF STRING1 == STRING2
Created, the file content is as follows:
@echo off
IF "%1" == "A" FORMAT A:
implement:
C:\>TEST5 A
The content of whether to format the A: disk appears on the screen.
Note: To prevent the parameter from being empty, the string is generally enclosed in double quotes (or other symbols, be careful not to use reserved symbols).
For example: if [%1]==[A] or if %1*==A*
5、GOTO
Created, the file content is as follows:
@ECHO OFF
IF EXIST C:\ GOTO _COPY
GOTO _DONE
:_COPY
COPY C:\ D:\
:_DONE
Notice:
(1) The colon ":" of the ASCII character is preceded by the colon, and there cannot be spaces between the colon and the label.
(2) The naming rules for labels are the same as those for file names.
(3) DOS supports labels with the longest eight-digit characters. When two labels cannot be distinguished, they will jump to the nearest label.
==== willsort Commentary Notes======================================================================
1) The label is also called label
2) Tags cannot start with most non-alphanumeric characters, while many can be used in file names
3) When two tags cannot be distinguished, they will jump to the tag with the highest position.
========================================================================
6、FOR
Create C:\, the file content is as follows:
@ECHO OFF
FOR %%C IN (*.BAT *.TXT *.SYS) DO TYPE %%C
run:
C:\>TEST7
After execution, all file contents with BAT, TXT, and SYS extensions in the root directory of the C: disk will be displayed on the screen (excluding hidden files).
Previous page12Read the full text