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
Created, the content is as follows:
@ECHO OFF
XCOPY C:\ D:\
IF ERRORLEVEL 1 ECHO file copy failed
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 User aborted the copy operation through ctrl-c
IF ERRORLEVEL 3 ECHO Preset error prevents file copying operations
IF ERRORLEVEL 4 ECHO Error writing during copying
Regardless of whether the copy is successful or not, the following:
No copy file found
User aborts 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 Backup successfully
1 No backup file found
2 File sharing conflict prevents backup from completing
3 User aborts backup with ctrl-c
4 The backup operation is aborted due to a fatal error
diskcomp
0 disks are the same
1. The disk is different
2 The 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 succeeded
1 Non-fatal disk read/write error
2 The user ends the copy operation through ctrl-c
3 The disk copy is aborted due to a fatal processing error
4 Preset errors prevent copy operations
format
0 The format was successful
3 User aborted formatting processing through ctrl-c
4 The formatting is aborted due to a 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
==== willsort Editor's Note =====================================================================
chkdsk
0 No error found
255 One or more errors found
choice
0 User press ctrl+c/break
1 The user presses the first key
255 Error condition detected in the command line
Others The position of the valid characters pressed by the user in the list
defrag
0 Fragment compression succeeded
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 an error in allocating space
8 Memory error
9 There is not enough space to compress disk fragmentation
deltree
0 Successfully deleted a directory
diskcomp
0 Two disks are the same
1 Discover different
2 Press CTRL+C to terminate the comparison
3 A 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 search
keyb
0 The keyboard definition file was 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 while communicating on keyboard and monitor
5 The required character set is not ready
move
0 The specified file was successfully moved
1 An error occurred
msav /N
86 Virus was checked
replace
0 REPLACE successfully replaced or added the file
1 MS-DOS version is incompatible with REPLACE
2 REPLACE The source file cannot be found
3 REPLACE Cannot find the source path or the target path
5 The files to be replaced cannot be accessed
8 The memory is insufficient and the REPLACE cannot be executed
11 Command line syntax error
restore
0 RESTORE successfully restored the file
1 RESTORE The file to be recovered cannot be found
3 The user presses CTRL+C to terminate the recovery process
4 RESTORE terminated due to error
scandisk
0 ScanDisk does not detect any errors on the drive it checks
1 ScanDisk cannot be run due to incorrect syntax of the command line
2 ScanDisk terminates unexpectedly due to memory exhaustion or internal error
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
255 ScanDisk found disk failure but failed to correct all
setver
0 SETVER successfully completed the task
1 The user specifies an invalid command switch
2 The user specifies an illegal file name
3 Not enough system memory to run the command
4 The user specifies an illegal version number format
5 SETVER specified item is not found in the version table
6 SETVER file not found
7 The user specifies an illegal drive
8 The user has specified too many command line parameters
9 SETVER detected missing command line parameters
10 While 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 detected 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 Editor's Note =====================================================================
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 page12345Read the full text