Teaching of advanced Batch Processing

Source: Internet
Author: User

A batch file is a file with an extension. BAT consisting of one or more doscommands and executable commands. When a user uses the batch processing file name as a command, DOS will automatically execute the commands in the file in sequence. Batch files are created and executed multiple times at a time.

There is a special batch processing file in the batch processing file. Every time you start the computer, the system automatically runs each command in the file. The file must meet two conditions: first, the file name is AUTOEXEC. BAT, and second, the file must be placed in the root directory of the boot disk (also known as the system disk.

In addition to the DOS command, you can also use the batch processing sub-commands in the batch processing file. These commands can also be seen as DOS internal commands, which are:

1) ECHO -- Display Mode settings; echo on enables subsequent commands to be displayed before execution, and echo off ensures that subsequent commands are not displayed before execution, echo massage displays the information specified by the MESSAGE regardless of whether the ECHO status is ON or OFF.

2) REM -- Comment command.

3) PAUSE-PAUSE system processing and display Press any key to continue ..., Wait for the user to press any key and continue.

4) GOTO -- turn to sub-commands.

5) IF -- condition subcommand.

6) FOR-loop subcommands.

7) SHIFT -- change the parameter position.

The computer will look for autoexec every time it starts up. bat: this batch file allows you to execute commands to be executed every time you start the system, such as setting the path, loading the mouse driver, and accelerating disk smartdrv, can make your computer truly automated.


Echo, @, call, pause, and rem are the most common commands for batch file processing. We started from them. Echo indicates the characters after this command
Echo off indicates that after this statement, all running commands do not display the command line itself.
@ Is similar to echo off, but it is added at the top of other command lines, indicating that the command line itself is not displayed during running.
Call calls another batch file (if another batch file is called directly, subsequent commands of the current file cannot be executed after that file is executed)
Running pause will be paused, and Press any key to continue...
Rem indicates that the character after this command is interpreted as a line. If it is not executed, it will only be used for future search.

For example, use edit to edit. bat file, enter the following content and save the disk as c:. bat, after executing the batch processing file, you can: write all the files in the root directory into a.txt, start UCDOS, enter WPS and other functions.

The content of the batch file is: file representation:

Echo off command line not displayed

Dir c: *. *> a.txt: Write the c-drive file into a.txt.

Call c: ucdosucdos. bat call ucdos

Echo Hello show "hello"

Pause and wait for the button to continue

Rem uses wps annotation to use wps

Cd ucdos enters the ucdos directory

Wps use wps

You can also use parameters in a batch file like the C language, which requires only one parameter identifier %.

% Indicates a parameter. A parameter is a string added after the file name when a batch file is run. Variables can be represented from % 0 to % 9, % 0 indicates the file name itself, and strings are represented in the order of % 1 to % 9.

For example, C: the name of the processing file in the root directory is f. bat, and the content is format % 1.

If C:> f a: is executed, format:

Another example is C: The name t. bat of the batch of files processed in the root directory, and the content is type % 1 type % 2.

Run C:> t a.txt B .txt to display the contents of a.txt and B .txt in sequence.

If goto choice for is a relatively advanced command in the batch processing file. if you are familiar with these commands, you are an expert in batch processing files.
If indicates whether the specified conditions are met, and then different commands are executed. There are three formats:
1. if "parameter" = "string" command to be executed
If the parameter is equal to the specified string, the condition is true. Run the command. Otherwise, run the next sentence. (Note that there are two equal signs)
For example, if "% 1" = "a" format:

2. if exist file name command to be executed
If a specified file exists, the condition is true. Run the command. Otherwise, run the next sentence. For example, if exist config. sys edit config. sys

3. if errorlevel: command to be executed
If the return code is equal to the specified number, the condition is true. Run the command. Otherwise, run the next sentence. For example, if errorlevel 2 goto x2 DOS returns a number to DOS when running the program, which is called the error code errorlevel or return code.

When the goto batch file runs here, it will jump to the label specified by goto, which is generally used with if. For example:

Goto end

: End
Echo this is the end

The label is represented by a string. The row where the label is located is not executed.

Choice uses this command to allow users to enter a single character to run different commands. The/c: parameter should be added for use, and c: should be followed by a prompt to enter characters without spaces. Its return code is 1234 ......

For example: choice/c: dimethyl defrag, mem, end
Will display
Defrag, mem, end [D, M, E]?

For example, the content of test. bat is as follows:
@ Echo off
Choice/c: dimethyl defrag, mem, end
If errorlevel 3 goto defrag, first judge the highest error code.
If errorlevel 2 goto mem
If errotlevel 1 goto end

: Defrag
C: dosdefrag
Goto end

: Mem
Mem
Goto end

: End
Echo good bye

After this file is run, defrag, mem, end [D, M, E]? You can select d m e, and then the if statement will make a judgment. d indicates the program segment with the execution label as defrag, and m indicates the program segment with the execution label as mem, e indicates the program segment whose execution label is end. Each program segment finally jumps the program to the end label using goto end. Then, the program displays good bye, and the file ends.

For Loop Command, as long as the conditions match, it will execute the same command multiple times.

Format: FOR [% f] in (SET) DO [command]
If the parameter f is in the specified set, the condition is true and the command is executed.

If one batch file contains one row:
For % c in (*. bat *. txt) do type % c
It indicates that if a file ends with bat or txt, the file content is displayed.


DOS automatically runs autoexec at startup. bat files are usually loaded with the required programs, such as path, smartdrv, mouse, and mscdex) doskey, set, and so on.

If the root directory of the boot disk does not contain this file, the computer will ask the user to enter the date and time.

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

@ Echo off command line not displayed

Prompt $ p $ g set a directory prompt before the prompt

Path c: dos; c: windows; c: ucdos; c: ools

Lh c: dosdoskey.com load keyboard Management

Lh c: mousemouse.com

Lh c: dossmartdrv.exe disk loading acceleration Management

Lh c: dosmscdex/S/D: MSCD000/M: 12/V loaded CD-ROM drive

Set temp = c: emp set temporary directory
Some Dangerous commands will be written into batch files by some people who are interested and spread around the Internet for destruction, such as writing in. bat:

Deltree-y c: delimiter

The next thing is that you need to take a towel to wipe your tears. In this sense, it is more vicious than a virus.

Similarly. hlp (Help file ),. pif (shortcut to DOS ),. dangerous commands can also be written to files such as lnk (WINDOWS shortcuts). It is dangerous to accidentally execute these commands. To prevent the above files from being damaged by calling the DOS command, the passive method is to change the name of a file such as format and deltree.
(1) Application DOS redirection

Standard Input and Output of DOS are usually performed on the keyboard and display of the standard device. With redirection, you can easily change the input and output to a disk file or another device. For example, during the execution of batch commands, the screen may be disturbed to prohibit the command or program output information after execution, and the DOS redirection function can be used to change the output to the NUL device (NUL does not point to any actual device ): c:> copy. txt B. TXT> NUL.

After the command is executed, the "1 file (s) copied" information is not displayed. Some interactive programs require a lot of keyboard input during execution, but sometimes the input is fixed. To speed up the operation, you can create an input file in advance, the content of this file is the keyboard input item of the program. Each input item occupies one line. If there is a program ZB, all its input items are included IN the file IN. DAT, execute C:> zb nul program to automatically execute.

(2) Application of the DOS Pipeline Function

The DOS pipeline function enables standard output of one program or command to be used as standard input of another program or command. For example, if you write the DEBUG input command to the file AAA, use the TYPE command to transmit the AAA content to DEBUG through the pipeline function, and no command parameters are requested from the console during the DEBUG execution, this improves machine efficiency. Command: C:> type aaa | DEBUG> BBB.

(3) subroutine

In a batch file, you can use the CALL command to CALL another sub-batch file. when the execution of the sub-batch file ends, the sub-batch file is automatically returned to the parent batch file for further execution. For example, A. bat B. BAT, A calls B, and A. BAT has the following content:

@ ECHO OFF
CALL B
CD BASIC
BASICA BG
@ ECHO ON

(4) menu selection

The DOS Function calls the One-byte return code provided by 31 H or 4 CH, and processes the return code through the batch processing sub-commands IF and ERRORLEVEL, so as to automatically execute a batch of commands. Implement all menu prompts of advanced languages in batch files to make batch files more flexible and convenient. Use DEBUG to create a MENU driver MENU. COM and write a batch file LG. BAT accordingly. The specific content and methods are shown in the following table:

DEBUG
-
-166C: 0100 mov dx, 111
-166C: 0103 mov ah, 09
-166C: 0105 INT 21
-166C: 0107 mov ah, 01
-166C: 0109 INT 21
-166C: 010B mov ah, 4C
-166C: 010D INT 21
-166C: 010F INT 20
-166C: 0111 DB ****************************** 0D 0A
-166C: 0131 DB * 1. Turbo Pascal 5.00 * 0D 0A
-166C: 0151 DB * 2. Turbo Basci 1.00 * 0D 0A
-166C: 0171 DB * 3. Turbo Prolog 2.00 * 0D 0A
-166C: 0191 DB * 4. Turbo C

Related Article

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.