DOS batch processing

Source: Internet
Author: User
A bat file is a batch file and a text file. Simply put, it is used to automatically execute multiple commands continuously, and the content of the batch file is a single command. So what is its use?

For example, when you start WPS software, you must execute

C: \> Cd WPS
C: \ WPS> spdos
C: \ WPS> py
C: \ WPS> wbx
C: \ WPS> WPS

If you execute this operation before Using WPS every time, do you think it is very troublesome?

If there is a method that requires only one batch file to be compiled, all the commands just now will be automatically executed. Do you want to learn it?

After reading this section, you will be surprised to see that the first batch of processing files you have prepared are successfully executed.

In addition, 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 pause, 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: \ UCDOS. Bat call UCDOS

Echo Hello show "hello"

Pause 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 DoSProgramAt runtime, a number is returned to DOS, 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: \ dos \ defrag
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: \ tools

LH c: \ dos \ doskey.com load keyboard Management

LH c: \ mouse \ mouse.com load mouse Management

LH c: \ dos \ smartdrv.exe attach disk acceleration Management

LH c: \ dos \ mscdex/S/D: mscd000/M: 12/V loaded CD-ROM driver

Set temp = c: \ Temp

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.