Batch files are files with the extension .BAT composed of one or more DOS commands and executable commands. When the user uses the batch file name as a command, DOS will automatically execute the commands in the file in sequence one by one. The characteristic of batch files is that they can be established once and executed multiple times.
There is a special batch file in batch files. Every time the computer is started, the system automatically executes each command in this file. This file must meet two conditions: first, the file name is AUTOEXEC.BAT, and second, the location of this file must be in the root directory of the boot disk (also called the system disk).
In batch files, in addition to using DOS commands, batch subcommands can also be used. These commands can also be regarded as internal commands of DOS. They are:
1) ECHO - Display mode setting; where ECHO ON makes the subsequent commands display before execution, ECHO OFF makes the subsequent commands not display before execution, and ECHO MESSAGE displays the information specified by MESSAGE regardless of the state of ECHO being ON or OFF.
2) REM - Comment command.
3) PAUSE - Pause the system processing. The system displays "Press any key to continue…" and waits for the user to press any key to continue execution.
4) GOTO - Transfer subcommand.
5) IF - Conditional subcommand.
6) FOR - Loop subcommand.
7) SHIFT - Change the position of parameters.
The computer looks for the autoexec.bat batch file every time it starts, so that some commands that need to be executed every time the computer is turned on can be executed, such as setting the path path, loading the mouse driver mouse, disk acceleration smartdrv, etc., which can truly automate your computer.
echo, @, call, pause, rem are several of the most commonly used commands in batch files. We start learning from them. echo means to display the characters after this command. echo off means that all running commands after this statement do not display the command line itself. @ is similar to echo off, but it is added at the front of other command lines, indicating that the command line itself is not displayed during operation. call calls another batch file (if you directly call another batch file, you will not be able to execute the subsequent commands of the current file after executing that file). pause running this sentence will pause, display "Press any key to continue...", and wait for the user to press any key to continue. rem means that the characters after this command are explanation lines, which are not executed, just for future self-finding.
Example: Use edit to edit the a.bat file, enter the following content and save it as c:\a.bat. After executing this batch file, the functions of writing all files in the root directory into a.txt, starting UCDOS, entering WPS, etc. can be realized.
The content of the batch file is: File representation:
echo off Do not display the command line
dir c:\*.* >a.txt Write the file list of drive C into a.txt
call c:\ucdos\ucdos.bat Call ucdos
echo Hello Display "Hello"
pause Pause, wait for the key to continue
rem Use WPS Comment that WPS will be used
cd ucdos Enter the ucdos directory
wps Use WPS
Parameters can also be used in batch files like in the C language, which only requires a parameter identifier %.
% represents a parameter, and the parameter refers to the string added after the file name when running the batch file. Variables can range from %0 to %9, %0 represents the file name itself, and the strings are represented in sequence by %1 to %9.
For example, there is a batch file named f.bat in the root directory of C, and the content is format %1.
Then if you execute C:\>f a:, the actual execution is format a:.
Another example: There is a batch file named t.bat in the root directory of C, and the content is type %1 type %2.
Then running C:\>t a.txt b.txt will sequentially display the contents of the a.txt and b.txt files.
if goto choice for are relatively advanced commands in batch files. If you are very proficient in these, you are an expert in batch files. if means to judge whether the specified condition is met, so as to decide to execute different commands. There are three formats:
1. if "parameter" == "string" command to be executed. If the parameter is equal to the specified string, the condition is established, and the command is run; otherwise, the next sentence is run. (Note that there are two equal signs) For example, if "%1"=="a" format a:.
2. if exist file name command to be executed. If the specified file exists, the condition is established, and the command is run; otherwise, the next sentence is run. For example, if exist config.sys edit config.sys.
3. if errorlevel number command to be executed. If the return code is equal to the specified number, the condition is established, and the command is run; otherwise, the next sentence is run. For example, if errorlevel 2 goto x2. All DOS programs return a number to DOS when running, which is called the error code errorlevel or return code.
goto The batch file will jump to the label specified by goto when it runs here. It is generally used in conjunction with if. For example:
goto end
:end
echo this is the end
The label is represented by :string, and the line where the label is located is not executed.
choice Using this command can let the user enter a character, so as to run different commands. When using it, the /c: parameter should be added. The characters that can be entered should be written after c:, with no spaces in between. Its return code is 1234...
For example: choice /c:dme defrag,mem,end
It will display
defrag,mem,end[D,M,E]?
For example, the content of test.bat is as follows:
@echo off
choice /c:dme defrag,mem,end
if errorlevel 3 goto defrag should judge the highest numerical error code first
if errorlevel 2 goto mem
if errotlevel 1 goto end
:defrag
c:\dos\defrag
goto end
:mem
mem
goto end
:end
echo good bye
After this file runs, it will display defrag,mem,end[D,M,E]? The user can choose d m e, and then the if statement will make a judgment. d means to execute the program segment labeled defrag, m means to execute the program segment labeled mem, e means to execute the program segment labeled end. Each program segment ends with goto end to jump the program to the end label, and then the program will display good bye, and the file ends.
The for loop command will execute the same command multiple times as long as the condition is met.
Format FOR [%%f] in (collection) DO [command]
As long as the parameter f is in the specified collection, the condition is established, and the command is executed.
If there is a line in a batch file:
for %%c in (*.bat *.txt) do type %%c
It means that if the file ends with bat or txt, the content of the file is displayed.
DOS will automatically run the autoexec.bat file when it starts. Generally, we load the programs that are used every time in it, such as: path (set path), smartdrv (disk acceleration), mouse (mouse start), mscdex (CD-ROM connection), doskey (keyboard management), set (set environment variables), etc.
If this file is not in the root directory of the boot disk, the computer will ask the user to enter the date and time.
For example, a typical autoexec.bat content is as follows:
@echo off Do not display the command line
prompt $p$g Set the directory prompt before the prompt
path c:\dos;c:\;c:\windows;c:\ucdos;c:\tools Set the path
lh c:\dos\doskey.com Load keyboard management
lh c:\mouse\mouse.com Load mouse management
lh c:\dos\smartdrv.exe Load disk acceleration management
lh c:\dos\mscdex /S /D:MSCD000 /M:12 /V Load CD-ROM driver
set temp=c:\temp Set the temporary directory
There is a special batch file in batch files. Every time the computer is started, the system automatically executes each command in this file. This file must meet two conditions: first, the file name is AUTOEXEC.BAT, and second, the location of this file must be in the root directory of the boot disk (also called the system disk).
In batch files, in addition to using DOS commands, batch subcommands can also be used. These commands can also be regarded as internal commands of DOS. They are:
1) ECHO - Display mode setting; where ECHO ON makes the subsequent commands display before execution, ECHO OFF makes the subsequent commands not display before execution, and ECHO MESSAGE displays the information specified by MESSAGE regardless of the state of ECHO being ON or OFF.
2) REM - Comment command.
3) PAUSE - Pause the system processing. The system displays "Press any key to continue…" and waits for the user to press any key to continue execution.
4) GOTO - Transfer subcommand.
5) IF - Conditional subcommand.
6) FOR - Loop subcommand.
7) SHIFT - Change the position of parameters.
The computer looks for the autoexec.bat batch file every time it starts, so that some commands that need to be executed every time the computer is turned on can be executed, such as setting the path path, loading the mouse driver mouse, disk acceleration smartdrv, etc., which can truly automate your computer.
echo, @, call, pause, rem are several of the most commonly used commands in batch files. We start learning from them. echo means to display the characters after this command. echo off means that all running commands after this statement do not display the command line itself. @ is similar to echo off, but it is added at the front of other command lines, indicating that the command line itself is not displayed during operation. call calls another batch file (if you directly call another batch file, you will not be able to execute the subsequent commands of the current file after executing that file). pause running this sentence will pause, display "Press any key to continue...", and wait for the user to press any key to continue. rem means that the characters after this command are explanation lines, which are not executed, just for future self-finding.
Example: Use edit to edit the a.bat file, enter the following content and save it as c:\a.bat. After executing this batch file, the functions of writing all files in the root directory into a.txt, starting UCDOS, entering WPS, etc. can be realized.
The content of the batch file is: File representation:
echo off Do not display the command line
dir c:\*.* >a.txt Write the file list of drive C into a.txt
call c:\ucdos\ucdos.bat Call ucdos
echo Hello Display "Hello"
pause Pause, wait for the key to continue
rem Use WPS Comment that WPS will be used
cd ucdos Enter the ucdos directory
wps Use WPS
Parameters can also be used in batch files like in the C language, which only requires a parameter identifier %.
% represents a parameter, and the parameter refers to the string added after the file name when running the batch file. Variables can range from %0 to %9, %0 represents the file name itself, and the strings are represented in sequence by %1 to %9.
For example, there is a batch file named f.bat in the root directory of C, and the content is format %1.
Then if you execute C:\>f a:, the actual execution is format a:.
Another example: There is a batch file named t.bat in the root directory of C, and the content is type %1 type %2.
Then running C:\>t a.txt b.txt will sequentially display the contents of the a.txt and b.txt files.
if goto choice for are relatively advanced commands in batch files. If you are very proficient in these, you are an expert in batch files. if means to judge whether the specified condition is met, so as to decide to execute different commands. There are three formats:
1. if "parameter" == "string" command to be executed. If the parameter is equal to the specified string, the condition is established, and the command is run; otherwise, the next sentence is run. (Note that there are two equal signs) For example, if "%1"=="a" format a:.
2. if exist file name command to be executed. If the specified file exists, the condition is established, and the command is run; otherwise, the next sentence is run. For example, if exist config.sys edit config.sys.
3. if errorlevel number command to be executed. If the return code is equal to the specified number, the condition is established, and the command is run; otherwise, the next sentence is run. For example, if errorlevel 2 goto x2. All DOS programs return a number to DOS when running, which is called the error code errorlevel or return code.
goto The batch file will jump to the label specified by goto when it runs here. It is generally used in conjunction with if. For example:
goto end
:end
echo this is the end
The label is represented by :string, and the line where the label is located is not executed.
choice Using this command can let the user enter a character, so as to run different commands. When using it, the /c: parameter should be added. The characters that can be entered should be written after c:, with no spaces in between. Its return code is 1234...
For example: choice /c:dme defrag,mem,end
It will display
defrag,mem,end[D,M,E]?
For example, the content of test.bat is as follows:
@echo off
choice /c:dme defrag,mem,end
if errorlevel 3 goto defrag should judge the highest numerical error code first
if errorlevel 2 goto mem
if errotlevel 1 goto end
:defrag
c:\dos\defrag
goto end
:mem
mem
goto end
:end
echo good bye
After this file runs, it will display defrag,mem,end[D,M,E]? The user can choose d m e, and then the if statement will make a judgment. d means to execute the program segment labeled defrag, m means to execute the program segment labeled mem, e means to execute the program segment labeled end. Each program segment ends with goto end to jump the program to the end label, and then the program will display good bye, and the file ends.
The for loop command will execute the same command multiple times as long as the condition is met.
Format FOR [%%f] in (collection) DO [command]
As long as the parameter f is in the specified collection, the condition is established, and the command is executed.
If there is a line in a batch file:
for %%c in (*.bat *.txt) do type %%c
It means that if the file ends with bat or txt, the content of the file is displayed.
DOS will automatically run the autoexec.bat file when it starts. Generally, we load the programs that are used every time in it, such as: path (set path), smartdrv (disk acceleration), mouse (mouse start), mscdex (CD-ROM connection), doskey (keyboard management), set (set environment variables), etc.
If this file is not in the root directory of the boot disk, the computer will ask the user to enter the date and time.
For example, a typical autoexec.bat content is as follows:
@echo off Do not display the command line
prompt $p$g Set the directory prompt before the prompt
path c:\dos;c:\;c:\windows;c:\ucdos;c:\tools Set the path
lh c:\dos\doskey.com Load keyboard management
lh c:\mouse\mouse.com Load mouse management
lh c:\dos\smartdrv.exe Load disk acceleration management
lh c:\dos\mscdex /S /D:MSCD000 /M:12 /V Load CD-ROM driver
set temp=c:\temp Set the temporary directory
Recent Ratings for This Post
( 2 in total)
Click for details
| Rater | Score | Time |
|---|---|---|
| dywjzh | +1 | 2008-09-09 11:40 |
| magiciankcat | +1 | 2009-11-10 12:27 |
弄花香满衣,掬水月在手。
明月鹭鸟飞, 芦花白马走。
我自一过后,野渡现横舟。
青云碧空在,净瓶水不流。
http://dos.e-stone.cn/guestbook/index.asp
======中國DOS聯盟=====
我的新网页http://rsds.7i24.com欢迎光顾
明月鹭鸟飞, 芦花白马走。
我自一过后,野渡现横舟。
青云碧空在,净瓶水不流。
http://dos.e-stone.cn/guestbook/index.asp
======中國DOS聯盟=====
我的新网页http://rsds.7i24.com欢迎光顾





