![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-23 12:55 |
47,812 topics / 349,917 posts / today 0 new / 48,273 members |
| DOS批处理 & 脚本技术(批处理室) » Re: Which expert can help introduce the syntax of DOS batch files |
| Printable Version 4,497 / 5 |
| Floor1 ko20010214 | Posted 2003-06-01 00:00 |
| 版主 Posts 1,628 Credits 7,296 | |
|
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) ================================================== |
|
| Floor2 junyee | Posted 2006-06-01 18:22 |
| 中级用户 Posts 112 Credits 253 | |
|
A post like this can even be included in the digest??..
|
|
| Floor3 s2731 | Posted 2006-07-14 17:50 |
| 中级用户 Posts 182 Credits 397 | |
| Floor4 loadtt | Posted 2007-05-15 11:30 |
| 新手上路 Posts 3 Credits 6 | |
|
Did you add your own post to the digest yourself?
|
|
| Floor5 studythedos | Posted 2007-05-17 23:09 |
| 初级用户 Posts 45 Credits 91 | |
|
Making it sticky seems good enough,
adding it to the digest is a bit... |
|
| Floor6 sunwayle | Posted 2008-08-26 11:22 |
| 初级用户 Posts 9 Credits 22 | |
|
Pretty good. For beginners like me, it's very useful.
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |