China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-01 12:15
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Re: Which expert can help introduce the syntax of DOS batch files DigestI View 4,325 Replies 5
Original Poster Posted 2003-06-01 00:00 ·  中国 江西 吉安 电信
版主
★★★★
Credits 7,296
Posts 1,628
Joined 2002-10-16 12:00
23-year member
UID 10
Gender Male
Status Offline
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)
==================================================
ko20010214
=================================
大功告成,打个Kiss!
ko20010214@MSN.com
神州优雅Q300C
Intel CeleronM 370处理器 | 256MbDDR内存
40G硬盘 | USB2.0 | IEEE 1394
13.3 ' WXGA 宽屏(16:10) | COMBO光驱
10/100M网卡 | 四合一读卡器
Floor 2 Posted 2006-06-01 18:22 ·  中国 江西 吉安 遂川县 电信
中级用户
★★
Credits 253
Posts 112
Joined 2006-05-31 11:12
20-year member
UID 56308
Gender Male
Status Offline
A post like this can even be included in the digest??..
Floor 3 Posted 2006-07-14 17:50 ·  中国 吉林 长春 电信
中级用户
★★
dos联盟算卦的
Credits 397
Posts 182
Joined 2006-07-13 15:06
20-year member
UID 58507
Gender Male
Status Offline
Floor 4 Posted 2007-05-15 11:30 ·  中国 广东 东莞 电信
新手上路
Credits 6
Posts 3
Joined 2007-05-14 17:38
19-year member
UID 88517
Gender Male
Status Offline
Did you add your own post to the digest yourself?
Floor 5 Posted 2007-05-17 23:09 ·  中国 河南 郑州 联通
初级用户
Credits 91
Posts 45
Joined 2007-03-14 07:04
19-year member
UID 81689
Gender Male
Status Offline
Making it sticky seems good enough,
adding it to the digest is a bit...
Floor 6 Posted 2008-08-26 11:22 ·  中国 江苏 南京 联通
初级用户
Credits 22
Posts 9
Joined 2008-08-25 09:42
17-year member
UID 124086
Gender Male
Status Offline
Pretty good. For beginners like me, it's very useful.
Forum Jump: