DOS batch processing tutorial

Source: Internet
Author: User
DOS batch processing tutorialAuthor: siyizhu Date: 2006-01-19 files with the suffix bat are batch files and are text files. It is rarely used now, but the recovery disks of systems like IBM laptops rely on batch processing. 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, you must execute the following command every time you start WPS:
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.
Common commands
   Echo, @, call, pause, and REM are the most common commands for batch processing files.We can start from them.
Echo indicates the character after this command is displayed; 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 the ParameterA parameter is a string that is 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:
For example, if the name of a batch of files processed in the root directory is T. bat and the content is type % 1 type % 2, running C:/> T a.txt B .txt will display the contents of a.txt and B .txt in sequence.
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.
Special commands
If goto choiceIt is a relatively advanced command in the batch processing file. If you are familiar with these commands, you are the expert of the batch processing file. 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 existCommand to be executed for file name
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 errorlevelNumber of commands 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.
GotoWhen the 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.
ChoiceYou can run different commands by entering a single character. 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/CME defrag, mem, and end will display defrag, mem, end [d, M, E]?
For example, the content of test. bat is as follows:
@ Echo off
Choice/CME defrag, mem, end
If errorlevel 3 goto defrag, first judge the highest error code.
If errorlevel 2 goto mem
If errotlevel 1 goto end
EFRAG
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.
ForLoop Command, as long as the condition is met, 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.
Autoexec. bat
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 load disk acceleration Management
Lh c:/DOS/mscdex/S/D: mscd000/M: 12/V loaded CD-ROM driver
Set temp = C:/temp
If-exist
First, use NotePad to create a file with the following content:
@ Echo off
If exist/autoexec. bat type/autoexec. bat
If not exist/autoexec. Bat echo/autoexec. bat does not exist
Save the file test. bat on drive C.
Then execute the command
C:/> test1.bat
In this case, if there is content in autoexec. bat, it will be displayed.
Create another file with the following content:
@ Echo off
If exist % 1 type % 1
If not exist % 1 echo % 1 does not exist
It is also stored in drive C, with the file name test2.bat
Then execute the command
C:/> Test2 autoexec. bat
Similarly, if there is content in autoexec. bat, it will be displayed.
Note:
1. If exist is used to test whether a file exists in the format
If exist [path + file name] command
2. in the second file, % 1 is a parameter. Dos allows nine batch parameter information to be passed to the batch processing file, which is % 1 --------- % 9, respectively, % 1 is a form parameter, autoexec. bat is a real parameter.
Furthermore, create a file named ABC. bat with the following content:
If '% 1' = 'A' echo Xiao
If '% 1' =' B 'echo Tian
If '% 1' = 'C' echo Xin
Run C:/> ABC. bat a B c
The screen displays C:/> xiaotianxia
If you execute C:/> ABC. bat a B
The screen displays C:/> Xiaotian
DOS attaches an empty string to parameter % 3.
You can place not between the IF and condition to indicate if to execute a command when the condition is false.
Note: This command can be directly run under DOS.
IF-ERRORLEVEL
Use NotePad to create a file Xiao. bat with the following content:
@ Echo off
Xcopy C:/autoexec. Bat D :/
If errorlevel = 0 ECHO, the file is successfully copied.
Then execute the file
C:/> Xiao. bat
If the file is successfully copied, the screen displays: successfully copied
If errorlevel is used to test the return value of the previous DOS command. Note that it is only the return value of the previous command. Therefore, the following batch file is incorrect.
@ Echo off
Xcopy C:/autoexec. Bat D :/
If errorlevel = 0 ECHO, the file is successfully copied.
If errorlevel = 1 echo the copied file is not found
If errorlevel = 2 Echo: the user uses Ctrl-C to stop the copy operation.
If errorlevel = 3 Echo preset Error Blocking file copy operation
If errorlevel = 4 echo disk write error during copy
Whether the copy is successful or not, the following:
Copy file not found
You can use ctrl-C to stop the copy operation.
Preset errors prevent file copy operations
An error occurred while writing the disk during the copy process.
Are displayed.
Note: This command can be directly input in DOS.
For example:
C:/> xcopy/autoexec. Bat D :/
Then you can execute
C:/> If errorlevel = 0 ECHO, the file is successfully copied.
If successful, the screen displays:
Successfully copied the file.
The return values of several common commands are as follows:
Backup
Exit status meaning
0 successfully backed up
1 backup file not found
2. File Sharing conflicts prevent backup from being completed
3. Use Ctrl-C to stop the backup.
4. the backup operation is aborted due to a fatal error.

Diskcomp
Exit status meaning
0 disks are the same
1 disk is different
2. You can use ctrl-C to stop the comparison operation.
3. The comparison operation is aborted due to a fatal error.
4 preset error abort comparison
Diskcopy
Exit status meaning
0 disk copy operation successful
1 non-fatal disk read/write error
2. You can use ctrl-C to end the copy operation.
3. the disk copy is aborted due to a fatal processing error.
4. The copy operation is blocked due to a preset error.
Format
Exit status meaning
0: formatted successfully.
3. Use Ctrl-C to stop formatting.
4. The format is aborted due to a fatal processing error.
5. When "Proceed with format (y/n)?" is displayed )?" End with user input n
Xopy
Exit status meaning
0 successfully copied the file
1 copy file not found
2. You can use ctrl-C to stop the copy operation.
4. preset errors prevent file copy operations
5. An error occurred while writing the disk during the copy process.
If String = string
First, use NotePad to create a file named Xiao. bat. The file content is as follows:
@ Echo off
If "% 1" = "A" format:
Then execute
C:/> Xiao
Whether to format the: disk is displayed on the screen.
The statement format is
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: This is the only option for direct use under DOS.
C:/> if "A" = "A" format:
Meaningless.
Goto
First, use NotePad to create a file named Xiao. bat. The file content is as follows:
@ Echo off
If exist C:/autoexec. Bat goto KB
: KB
Copy C:/autoexec. Bat D :/
: Done
Note:
1. The label is preceded by a colon (:)
2. the last line of the label is: Dong
3. Dos supports a maximum of eight labels. When two labels cannot be distinguished, the last one is redirected.
For
First, use NotePad to create a file named Xiao. bat. The file content is as follows:
@ Echo off
For % C in (*. bat *. txt *. sys) do type % C
Then execute
C:> Xiao. bat
After execution, all files with the *. bat *. txt *. sys extension on the C: disk are displayed on the screen, excluding hidden files.
Note: character % C indicates the for command variable. For supports wildcards.

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.