Posted by: yourfeng (yourfeng), board: DOS
Subject: Re: Could some expert please introduce the syntax of DOS batch files?
Posted at: BBS Shuimu Tsinghua Station (Thu Mar 27 22:12:17 2003), forwarded
echo means to display the characters after this command
echo off means that after this statement, all commands that run will not display the command line itself
@ is similar to echo off, but it is added at the very beginning of other command lines, meaning that when running, the command line itself is not displayed.
call calls another batch file (if you directly call another batch file, after that file finishes executing, the remaining commands in the current file can no longer be executed)
pause running this line will pause, display Press any key to continue... and wait for the user to press any key before continuing
rem means the characters after this command are comment lines, not executed, only for your own future reference
Example: use edit to edit the a.bat file, enter the following contents and save it as c:\a.bat. After executing this batch file, it can
do the following: write all files in the root directory into a.txt,
start UCDOS, enter WPS, and other functions.
The content of the batch file is: What each line means:
echo off do not display the command line
dir c:\*.* >a.txt write the list of files on drive C into a.txt
call c:\ucdos\ucdos.bat call ucdos
echo 你好 display "你好"
pause pause, wait for a key press to continue
rem 使用wps comment: will use wps
cd ucdos enter the ucdos directory
wps use wps
*** In batch files you can also use parameters just like in C language; this only requires a parameter symbol %.
% indicates parameters. Parameters refer to the strings added after the filename when running the batch file. Variables can be from %09
,%0 indicates the filename itself, and the strings are represented in order by %1 to %9.
For example, if there is a batch file in the root directory of C: named f.bat, with the content format %1
then if you execute C:\>f a: what is actually executed is format a:
Another example, if there is a batch file in the root directory of C: named t.bat, with the content type %1 type %2
then running C:\>t a.txt b.txt will display the contents of a.txt and b.txt in sequence
if goto choice for are relatively advanced commands in batch files. If you use these very skillfully, then you
are a batch file expert.
if means it will judge whether specified conditions are met, and thus decide which different command to execute.
There are three formats:
1、if "parameter" == "string" command to execute If the parameter equals the specified string, the condition is true,
and the command is run,
otherwise the next line is run. (Note that there are two equal signs) for example if "%1"=="a" format a:
2、if exist filename command to execute If the specified file exists, the condition is true and the command runs, otherwise
the next line is run.
For example if exist config.sys edit config.sys
3、if errorlevel number command to execute If the return code equals the specified number, the condition is true and the command runs,
otherwise the next line is run.
For example if errorlevel 2 goto x2 When a DOS program runs, it returns a number to DOS, called the error code erro
rlevel or return code
goto when the batch file runs to here, it will jump to the label specified by goto. It is generally used together with if.
For example: goto end :end echo this is the end A label is represented by :string, and the line where the label is located is not executed
choice using this command lets the user input one character, so that different commands can be run.
When using it, you should add the /c: parameter; after c: you should write the characters the prompt allows as input, with no spaces between them.
Its return codes are 1234…… for example: choice /c:dme defrag,mem,end will display defrag,mem,end
?
For example, the content of test.bat is as follows: @echo off choice /c:dme defrag,mem,end if errorlevel
3 goto defrag
You should first judge the highest errorlevel value if errorlevel 2 goto mem if errotlevel 1 goto end :def
rag c:\dos\defrag goto end :mem mem goto end :end echo good bye After this file runs,
it will display defrag,mem,end? The user can choose d m e, and then the if statement will make the judgment. d means execute
the program section labeled defrag,
m means execute the program section labeled mem, e means execute the program section labeled end. Each program section ends with goto
end to jump the program to the end label, then the program will display good bye, and the file ends.
for loop command. As long as the condition is met, it will execute the same command many times. Format FOR in (set) DO
As long as parameter f is in the specified set,
then the condition is true and the command is executed. If there is a line in a batch file: for %%c in (*.bat *.txt) do
type %%c its meaning is that if it is a file ending in bat or txt,
then display the file contents.
DOS will automatically run the autoexec.bat file at startup. We generally load into it the programs that are needed every time,
for example: path (set the path),
smartdrv (disk acceleration),
mouse (start the mouse),
mscdex (CD-ROM connection),
doskey (keyboard management),
set (set environment variables), etc.
If there is no such file in the root directory of the boot disk, the computer will ask the user to input the date and time.
For example, a typical autoexec.bat is as follows:
@echo off do not display the command line
prompt $p$g set the prompt so it includes the directory 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 mouse management
lh c:\dos\smartdrv.exe load disk acceleration management
lh c:\dos\mscdex /S /D:MSCD000 /M:12 /V load the CD-ROM driver
set temp=c:\temp set the temporary directory
【 In the post by biticrazy (none), it was mentioned: 】
: .bat files.
: How do you write loops, conditional statements, define variables, etc.,
: preferably in a bit more detail.
--
※ Source: ·BBS Shuimu Tsinghua Station smth.org·
(This article was copied using the S-Term article copy script)
==================================================
Subject: Re: Could some expert please introduce the syntax of DOS batch files?
Posted at: BBS Shuimu Tsinghua Station (Thu Mar 27 22:12:17 2003), forwarded
echo means to display the characters after this command
echo off means that after this statement, all commands that run will not display the command line itself
@ is similar to echo off, but it is added at the very beginning of other command lines, meaning that when running, the command line itself is not displayed.
call calls another batch file (if you directly call another batch file, after that file finishes executing, the remaining commands in the current file can no longer be executed)
pause running this line will pause, display Press any key to continue... and wait for the user to press any key before continuing
rem means the characters after this command are comment lines, not executed, only for your own future reference
Example: use edit to edit the a.bat file, enter the following contents and save it as c:\a.bat. After executing this batch file, it can
do the following: write all files in the root directory into a.txt,
start UCDOS, enter WPS, and other functions.
The content of the batch file is: What each line means:
echo off do not display the command line
dir c:\*.* >a.txt write the list of files on drive C into a.txt
call c:\ucdos\ucdos.bat call ucdos
echo 你好 display "你好"
pause pause, wait for a key press to continue
rem 使用wps comment: will use wps
cd ucdos enter the ucdos directory
wps use wps
*** In batch files you can also use parameters just like in C language; this only requires a parameter symbol %.
% indicates parameters. Parameters refer to the strings added after the filename when running the batch file. Variables can be from %09
,%0 indicates the filename itself, and the strings are represented in order by %1 to %9.
For example, if there is a batch file in the root directory of C: named f.bat, with the content format %1
then if you execute C:\>f a: what is actually executed is format a:
Another example, if there is a batch file in the root directory of C: named t.bat, with the content type %1 type %2
then running C:\>t a.txt b.txt will display the contents of a.txt and b.txt in sequence
if goto choice for are relatively advanced commands in batch files. If you use these very skillfully, then you
are a batch file expert.
if means it will judge whether specified conditions are met, and thus decide which different command to execute.
There are three formats:
1、if "parameter" == "string" command to execute If the parameter equals the specified string, the condition is true,
and the command is run,
otherwise the next line is run. (Note that there are two equal signs) for example if "%1"=="a" format a:
2、if exist filename command to execute If the specified file exists, the condition is true and the command runs, otherwise
the next line is run.
For example if exist config.sys edit config.sys
3、if errorlevel number command to execute If the return code equals the specified number, the condition is true and the command runs,
otherwise the next line is run.
For example if errorlevel 2 goto x2 When a DOS program runs, it returns a number to DOS, called the error code erro
rlevel or return code
goto when the batch file runs to here, it will jump to the label specified by goto. It is generally used together with if.
For example: goto end :end echo this is the end A label is represented by :string, and the line where the label is located is not executed
choice using this command lets the user input one character, so that different commands can be run.
When using it, you should add the /c: parameter; after c: you should write the characters the prompt allows as input, with no spaces between them.
Its return codes are 1234…… for example: choice /c:dme defrag,mem,end will display defrag,mem,end
?
For example, the content of test.bat is as follows: @echo off choice /c:dme defrag,mem,end if errorlevel
3 goto defrag
You should first judge the highest errorlevel value if errorlevel 2 goto mem if errotlevel 1 goto end :def
rag c:\dos\defrag goto end :mem mem goto end :end echo good bye After this file runs,
it will display defrag,mem,end? The user can choose d m e, and then the if statement will make the judgment. d means execute
the program section labeled defrag,
m means execute the program section labeled mem, e means execute the program section labeled end. Each program section ends with goto
end to jump the program to the end label, then the program will display good bye, and the file ends.
for loop command. As long as the condition is met, it will execute the same command many times. Format FOR in (set) DO
As long as parameter f is in the specified set,
then the condition is true and the command is executed. If there is a line in a batch file: for %%c in (*.bat *.txt) do
type %%c its meaning is that if it is a file ending in bat or txt,
then display the file contents.
DOS will automatically run the autoexec.bat file at startup. We generally load into it the programs that are needed every time,
for example: path (set the path),
smartdrv (disk acceleration),
mouse (start the mouse),
mscdex (CD-ROM connection),
doskey (keyboard management),
set (set environment variables), etc.
If there is no such file in the root directory of the boot disk, the computer will ask the user to input the date and time.
For example, a typical autoexec.bat is as follows:
@echo off do not display the command line
prompt $p$g set the prompt so it includes the directory 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 mouse management
lh c:\dos\smartdrv.exe load disk acceleration management
lh c:\dos\mscdex /S /D:MSCD000 /M:12 /V load the CD-ROM driver
set temp=c:\temp set the temporary directory
【 In the post by biticrazy (none), it was mentioned: 】
: .bat files.
: How do you write loops, conditional statements, define variables, etc.,
: preferably in a bit more detail.
--
※ Source: ·BBS Shuimu Tsinghua Station smth.org·
(This article was copied using the S-Term article copy script)
==================================================
ko20010214
=================================
大功告成,打个Kiss!
ko20010214@MSN.com
神州优雅Q300C
Intel CeleronM 370处理器 | 256MbDDR内存
40G硬盘 | USB2.0 | IEEE 1394
13.3 ' WXGA 宽屏(16:10) | COMBO光驱
10/100M网卡 | 四合一读卡器
=================================
大功告成,打个Kiss!
ko20010214@MSN.com
神州优雅Q300C
Intel CeleronM 370处理器 | 256MbDDR内存
40G硬盘 | USB2.0 | IEEE 1394
13.3 ' WXGA 宽屏(16:10) | COMBO光驱
10/100M网卡 | 四合一读卡器

DigestI