Bat Batch File Syntax (2)
Batch Processing example:
1. If-exist
1) First, use NotePad to create a test1.bat batch file in C:/. The content of the file is as follows:
@ Echo off
If exist/autoexec. bat type/autoexec. bat
If not exist/autoexec. Bat echo/autoexec. bat does not exist
Then run it:
C:/> test1.bat
If the C:/file contains the autoexec. BAT file, its content will be displayed. If it does not exist, the batch processing will prompt you that the file does not exist.
2) create another test2.bat file with the following content:
@ Echo off
If exist/% 1 type/% 1
If not exist/% 1 echo/% 1 does not exist
Run:
C:/> Test2 autoexec. bat
The command runs the same result as above.
Note:
(1) If exist is used to test whether a file exists. The format is
If exist [path + file name] command
(2) % 1 in the test2.bat file is a parameter. Dos allows passing nine batch parameter information to the batch processing file, which is % 1 ~ % 9 (% 0 indicates the Test2 command itself), which is a bit like the relationship between real parameters and parameters in programming. % 1 is the form parameter, and autoexec. bat is the real parameter.
===== Gjr choreographer ====================================== ==============================
DOS does not have the limit of "allow Passing 9 batch parameter information". The number of parameters is limited only by the length of the command line and the processing capability of the called command. however, in the batch processing program, only 10 parameters can be referenced at the same time, because dos only gives % 0 ~ % 9.
========================================================== ======================================
3) create a file named test3.bat with the following content:
@ Echo off
If "% 1" = "A" Echo Xiao
If "% 2" = "B" Echo Tian
If "% 3" = "C" Echo Xin
If you run:
C:/> test3 A B C
The screen displays:
Xiao
Tian
Xin
If you run:
C:/> test3 A B
Displayed on the screen
Xiao
Tian
During the command execution, DOS will specify an empty string to the parameter % 3.
2. IF-ERRORLEVEL
Create test4.bat with the following content:
@ Echo off
Xcopy C:/autoexec. Bat D :/
If errorlevel 1 ECHO file copy failed
If errorlevel 0 ECHO, the file is successfully copied.
Then execute the file:
C:/> test4
If the file is successfully copied, the screen displays "successfully copied file"; otherwise, the screen displays "failed file copy ".
If errorlevel is used to test the return value of the previous DOS command. Note that it is only the return value of the previous command, and the return value must be determined in order from large to small.
Therefore, the following batch file is incorrect:
@ Echo off
Xcopy C:/autoexec. Bat D :/
If errorlevel 0 ECHO, the file is successfully copied.
If errorlevel 1 ECHO does not find the copy object
If errorlevel 2 ECHO, the user uses Ctrl-C to stop the copy operation.
If errorlevel 3 Echo preset Error Blocking file copy operation
If errorlevel 4 echo disk write error during copy process
Whether the copy is successful or not, the following:
Copy file not found
You can use ctrl-C to stop the copy operation.
Preset errors prevent file copy operations
An error occurred while writing the disk during the copy process.
Are displayed.
The return values of several common commands and their meanings are as follows:
Backup
0 successfully backed up
1 backup file not found
2. File Sharing conflicts prevent backup from being completed
3. Use Ctrl-C to stop the backup.
4. the backup operation is aborted due to a fatal error.
Diskcomp
0 disks are the same
1 disk is different
2. You can use ctrl-C to stop the comparison operation.
3. The comparison operation is aborted due to a fatal error.
4 preset error abort comparison
Diskcopy
0 disk copy operation successful
1 non-fatal disk read/write error
2. You can use ctrl-C to end the copy operation.
3. the disk copy is aborted due to a fatal processing error.
4. The copy operation is blocked due to a preset error.
Format
0: formatted successfully.
3. Use Ctrl-C to stop formatting.
4. The format is aborted due to a fatal processing error.
5. When the message "Proceed with format (y/n )? "End with user type N
Xcopy
0 successfully copied the file
1 copy file not found
2. You can use ctrl-C to stop the copy operation.
4. preset errors prevent file copy operations
5. An error occurred while writing the disk during the copy process.
Chkdsk
0: no error found
255 locate one or more errors
Choice
0. Press Ctrl + C/break.
1. the user presses the first key.
255 the error conditions in the command line are detected.
Position of valid characters pressed by other users in the list
Defrag
0 fragment compressed successfully
1 Internal error
2. There is no empty cluster on the disk. To run defrag, at least one empty cluster is required.
3. the user uses Ctrl + C to exit defrag.
4. General errors
5 defrag errors when reading the Cluster
6. defrag encountered an error when writing the Cluster
7. An error occurred while allocating space.
8 Memory Errors
9 there is not enough space to compress disk fragments
Deltree
0 successfully deletes a directory
Diskcomp
0. The two disks are the same.
1. Different
2. Press Ctrl + C to terminate the comparison.
3. Serious Error
4. initialization error
Find
0. The query is successful and at least one matching string is found.
1. The search is successful but the matching string is not found.
2. An error occurred while searching.
Keyb
0. The keyboard definition file is successfully loaded.
1. An invalid keyboard code, character set, or syntax is used.
2. the keyboard definition file is broken or not found.
4. An error occurred while communicating with the 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 virus detected
Replace
0 replace successfully replaces or adds a file
1 MS-DOS version and replace incompatible
2 replace cannot find the source file
3 replace cannot find the source or target path
5. You cannot access the file to be replaced.
8. Unable to execute replace due to insufficient memory
11 command line syntax error
Restore
0 restore successfully restored the file
1. the file to be restored cannot be found in Restore.
3. Press Ctrl + C to terminate the restoration process.
4. Restore terminated due to an error
Scandisk
0 Scandisk does not detect any errors on the drive it checks
1. Because the syntax of the command line is incorrect, Scandisk cannot be run.
2. Scandisk ended unexpectedly due to memory exhaustion or internal errors
3. the user exits Scandisk midway through
4. During disk scanning, the user decides to exit early.
254 Scandisk found the disk fault and corrected all
255 Scandisk found disk fault, but failed to correct all
Setver
0 setver successfully completed the task
1. the user specified an invalid command switch.
2. the user specified an invalid file name.
3. Insufficient system memory to run commands
4. the user specified an invalid version number format.
5. setver does not find the specified item in the version table.
6. setver. EXE file not found in setver
7. The user specified an invalid drive.
8. Too many command line parameters are specified.
9 setver detects that the command line parameter is missing
10 when reading the setver. EXE file, setver detects an error
11. The setver. EXE file is corrupted.
12 The specified setver. EXE file does not support version tables.
The version 13 table does not have enough space to store new items.
14 setver detected an error when writing the setver. EXE file
========================================================== ======================================
3. If string1 = string2
Create test5.bat. The file content is as follows:
@ Echo off
If "% 1" = "A" format:
Run:
C:/> test5
Whether to format the: disk is displayed on the screen.
Note: To prevent the parameter from being empty, double quotation marks (or other symbols) are generally used to enclose the string.
For example, if [% 1] = [a] Or if % 1 * = *
5. Goto
Create test6.bat. The file content is as follows:
@ Echo off
If exist C:/autoexec. Bat goto _ copy
Goto _ done
: _ Copy
Copy C:/autoexec. Bat D :/
: _ Done
Note:
(1) The colon Before the label is an ASCII character ":", there must be no space between the colon and the label.
(2) The naming rules for labels are the same as those for file names.
(3) DoS supports a maximum of eight characters. When two labels cannot be distinguished, the system redirects to the nearest one.
===== Gjr choreographer ====================================== ==============================
1) label is also called label)
2) labels cannot start with most non-alphanumeric characters, but can be used in file names.
3) when there is no difference between the two tags, the tag will jump to the top of the position
========================================================== ======================================
6.
Create C:/test7.bat. The file content is as follows:
@ Echo off
For % C in (*. bat *. txt *. sys) do type % C
Run:
C:/> test7
After the command is executed, all files with the extension bat, txt, and sys in the C: root directory are displayed on the screen (excluding hidden files ).