Tutorial on recommended batch processing for DOS and BAT 1th/2 page _dos/bat

Source: Internet
Author: User
Tags error code goto sub command
The teaching of batch processing
(A batch file is made up of one or more DOS commands and executable commands with an extension.) BAT's file. When the user names the batch file as a command, DOS automatically executes the commands in the file sequentially. Batch files are characterized by a single build that can be executed multiple times. 

There is a special batch file in the batch file, and every command in the file is automatically executed each time the computer is started. The file must meet two criteria: first, the file name is AUTOEXEC. BAT, and the second is that the location of the file must be placed in the root directory of the boot disk (also known as the system disk). 

In addition to using DOS commands in batch files, you can also use batch subcommand, which can also be considered as internal commands for DOS, which are:

1 echo--display mode, where echo on is so that subsequent commands are displayed before execution, echo off is so that subsequent commands do not appear before execution, and the echo massage is the information specified by the message, regardless of the state of ECHO on or off. 

2) rem--annotation command. 

3) pause--suspend system processing, the system displays the press any key to continue ..., waits for the user presses any one key to continue execution. 

4) goto--Steering Sub command. 

5) if--Conditional subcommand. 

6) for--Loop subcommand. 

7) shift--Change the position of the parameter. 




Every time the computer starts to look for Autoexec.bat this batch file, so that you can perform some of the commands to be executed every time, such as setting path path, loading mouse drive mouse, disk acceleration smartdrv, etc., so that your computer is truly automated.


Echo, @, call, pause, rem are some of the most commonly used commands for batch files, and we start with them. Echo indicates the character after this command is displayed
echo off means that none of the commands running after this statement show the command line itself
@ is the same as echo off, but it is added to the front of the other command lines, indicating that the runtime does not display the command line itself.
Call invokes another batch file (if another batch file is called directly, the current file successor command cannot be executed after the file is executed)
Pause run this sentence will be paused, show press any key to continue ... Wait for the user to press any key to continue
REM indicates that the word identifier after this command interprets the line, does not execute, but only gives itself a future lookup using the

Example: Edit the A.bat file with edit, enter the following content to save for C:\a.bat, after the execution of the batch file can be realized: All files in the root directory into the a.txt, start Ucdos, into WPS and other functions.

The contents of the batch file are: File representation:

echo off does not display the command line

Dir c:\*.* >a.txt writes a list of C-disk files to A.txt

Call C:\ucdos\ucdos.bat calls Ucdos

echo, how do you show "Hello"?

Pause paused, waiting for key to continue

REM using WPS annotations will use WPS

CD Ucdos into Ucdos directory

WPS using WPS

The batch file can also use parameters like the C language, which requires only a single parameter notation.

% represents a parameter, which is a string that is added after the file name when the batch file is run. A variable can represent the file name itself from%0 to%9,%0, and the string is represented in the order of%1 to%9.

For example, the C: root directory is f.bat with the following batch file name, content is format%1

If you execute c:\>f A: Then the format a is actually executed:

Also like the C: root directory The next batch of processing files is named T.bat, content is type%1 type%2

Then running C:\>t a.txt B.txt will sequentially display the contents of A.txt and B.txt files


If Goto choice for is a more advanced command in a batch file, you are an expert in batch files if you use them skillfully.
If the condition is judged to be in compliance with the stipulated conditions, it is decided to execute different commands. There are three different formats:
1, if "parameter" = = "string" command to be executed
If the parameter is equal to the specified string, the condition is set, the command is run, or the next sentence is run. (note is two equals)
If "%1" = = "A" format a:

2, if exist file name to execute command
If you have the specified file, the condition is set, run the command, or run the next sentence. such as if exist Config.sys edit Config.sys

3, if errorlevel the number of orders to be executed
If the return code equals the specified number, the condition is set, the command is run, or the next sentence is run. If the IF errorlevel 2 goto x2 DOS program runs, it will return a number to DOS, called the error code ERRORLEVEL or return code.

The goto batch file runs here and jumps to the label specified by Goto, and is generally used in conjunction with IF. Such as:

Goto END

: End
The echo this are the end

Label used: string indicating that the line of the label is not executed

Choice Use this command to allow the user to enter a character to run different commands. Use should be added/C: parameter, the letter should be written after the prompt can enter characters, no spaces between. Its return code is 1234 ...

such as: Choice/c:dme defrag,mem,end
will display
Defrag,mem,end[d,m,e]?

For example, the contents of Test.bat are as follows:
@echo off
Choice/c:dme Defrag,mem,end
if errorlevel 3 goto defrag should first determine the highest number of error codes
if errorlevel 2 goto MEM
If Errotlevel 1 goto end

:d Efrag
C:\dos\defrag
Goto END

: Mem
Mem
Goto END

: End
echo Good bye

When this file is run, will display defrag,mem,end[d,m,e]? The user may select D m e, and then the IF statement will make a judgment, d means the execution label is defrag the program segment, m means the execution marking is the program segment of Mem, E represents the execution label as end of the program segment, each program paragraph ends with Goto ended the program jumps to the final label place, The program then displays the good bye and ends the file.

The For loop command, which executes the same command multiple times, as long as the condition is met.

format for [%%f] in (set) do [command]
If the parameter f is within the specified set, the condition is set and the command is executed

If there is a line in a batch file:
For%%c in (*.bat *.txt) do type%%c
The implication is that if the file ends with a bat or TXT, the contents of the file are displayed.


Dos in the startup will automatically run Autoexec.bat this file, we normally load every required program, such as: path (set path), Smartdrv (disk acceleration), mouse (mouse boot), MSCDEX (CD-ROM connection), Doskey (keyboard management) , set (setting environment variables), and so on.

If this file is not in the boot packing directory, the computer will let the user enter the date and time.

For example, a typical Autoexec.bat content is as follows:

@echo off does not display the command line

Prompt $p $g There is a directory hint before the prompt is set

Path C:\dos;c:\;c:\windows;c:\ucdos;c:\tools Set paths

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 drive

Set temp=c:\temp Setting Temp directory
Current 1/2 page 12 Next read the full text

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.