Batch file commands

Source: Internet
Author: User
Tags echo command

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.
Type indicates the file content.

For example, use Edit to edit the. BAT file, enter the following content, and save the disk as c: \ A. bat. After executing the batch file, you can write all the files in the root directory

In a.txt, start UCDOS and 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

When the DOS program runs, 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.

 

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 attach disk acceleration Management
LH c: \ dos \ mscdex/S/D: mscd000/M: 12/V loaded CD-ROM driver

Set temp = c: \ Temp

 

Introduction to simple batch processing commands:
Echo command]
Enable or disable the request echo function or display messages. If no parameters exist, the echo command displays the current echo settings.
Syntax
Echo [{on | off}] [Message]
Sample: @ echo off/ECHO Hello World
In actual application, we will combine this command with the redirection symbol (also known as the pipeline symbol) to implement input 1.
Commands to specific files.
[REM command]
The annotation command is similar to/* -------- */in the C language. It is not executed, but serves as a comment for others to read.
And you will modify it later.
: Also has the REM Function
Rem message
Sample: @ REM here is the description.
[Pause command]
Pause the command. When running the pause command, the following message is displayed:
Press any key to continue... (or: press any key to continue ...)
Sample:
@ Echo off
: Begin
Copy G: *. * D: \ back
Insert another disc for Echo...
Pause
Goto begin
In this example, all files on the disk in drive G are copied to D: \ back. The displayed note prompts you to upload another CD
When the disk is attached to the drive g, the pause command suspends the program so that you can change the disk and press any key to continue processing.
[Call Command]
Call another batch processing program from one batch processing program without terminating the parent batch processing program. The Call Command accepts
Label. If a call is used outside a script or batch file, it does not work in the command line.
Syntax
Call [[drive:] [path] filename] [: Label [arguments]
Parameters
[Drive:} [path] filename
Specifies the location and name of the batch processing program to be called.
[Start command]
Call an external program. All the DOS commands and command line programs can be called by the START command.
For example, start calc.exe enables the Windows Calculator.
Common parameters:
Minimum window size when Min starts
Separate starts a 16-bit windows program in a separate space
High starts applications in the high priority category
Realtime starts applications in the realtime priority category
Wait starts the application and waits for it to end
Parameters: these are parameters sent to the command/program.
When the executed application is a 32-bit GUI application, cmd. EXE returns a command prompt before the application is terminated. If
Execute in the script, and the new behavior will not happen.
[Goto command]
Jump command. The program pointer jumps to the specified tag and continues executing the batch processing program from the first command after the tag.
Syntax: goto label (label is a parameter that specifies the rows in the batch processing program to be switched .)
Sample:
If {% 1 }={} goto noparms
If {% 2 }={} goto noparms

@ REM check parameters if null show usage
: Noparms
Echo usage: monitor. Bat serverip portnumber
Goto end
The name of a tag can be used as needed, but it is better to have a meaningful letter. Add a letter before it to indicate that the letter is a tag, Goto
The command is based on this: to find the next step to jump there. It is better to have some explanations so that others seem to understand your intention.
[Set command]
Display, set, or delete variables.
Show variables: set or set S the former displays all the variables and their values defined by the batch processing, and the latter displays all the variables and values starting with S.
Set variable: Set AA = ABCD. You can assign a value to the variable AA. If the variable AA has been defined, the value of AA is changed to ABCD. If AA has not been defined, this command defines the new variable AA and assigns the initial value ABCD to the variable AA.
Delete variable: Set AA = this command can delete the variable AA. If the variable AA has been defined, the variable AA is deleted. If the variable AA has not been defined, this command is meaningful.
It should be noted that the variables in the batch processing do not distinguish between types, and do not need to distinguish int, float, Char, and so on like the variables in the C language. For example, after set AA = 345 is executed, the value of the variable AA can be regarded as either a number of 345 or a string of 345.
The SET command has extended functions, such as interactive input, string processing, and numerical calculation. It belongs to the category of advanced commands.

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.