In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to use common batch internal commands". In daily operation, I believe many people have doubts about how to use common batch internal commands. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use common batch internal commands". Next, please follow the editor to study!
@ echo offecho "Welcome to very BAT!" pause
Save the above three commands as a file test.bat or test.cmd and execute them
He will display the following two jargon on the screen:
Welcome to very BAT! Please press any key to continue. . .
This is a simple batch file with a total of two commands "echo" and "pause" and a special symbol "@"
From the above simple batch processing, we can find that batch processing is actually a combination of some symbols with special meaning and some commands that complete specified functions, so how many such special symbols and functional commands are there in batch processing? Let's take a closer look at some of the most commonly used ones now!
(the following content comes from the network, please read it carefully so that you can get to the example in the next section)
=
Common commands for batch processing (there are still many commands not enumerated, please check the help information)
1. REM and:
2, ECHO and @
3 、 PAUSE
4 、 ERRORLEVEL
5 、 TITLE
6 、 COLOR
7. Mode configures system devices
8. GOTO and:
9 、 FIND
10 、 START
11, assoc and ftype
12. Pushd and popd
13 、 CALL
14 、 shift
15 、 IF
16. Setlocal and variable delay
17. ATTRIB displays or changes file properties
Introduction command
1. REM and:
REM is a comment command, which is generally used to annotate the program. The content after the command is not executed, but can be echoed.
Second,:: can also play the role of rem annotation, and more concise and effective, but there are two points to note:
First, any character line that begins with a colon is treated as a label in the batch and everything that follows is directly ignored.
Valid label: the colon is followed by an alphanumeric string that can be recognized by the goto statement.
Invalid label: the colon is followed by a special symbol that is not alphanumeric, and the label that goto cannot recognize can play the role of annotation, so:: is often used as an annotation symbol, in fact: + can also play the role of annotation.
Second, unlike rem, the character line after:: will not be echoed when executed, regardless of whether you open the command line echo state with echo on or not, because the command interpreter does not think it is a valid command line, from this point of view, rem will be more applicable than:: in some cases; in addition, rem can be used in config.sys files.
Inline comment format:% comment content% (not commonly used, use with caution)
2, ECHO and @
Placing the @ character in front of a command turns off the echo of the command, regardless of whether the echo is open or not.
The functions of the echo command are listed as follows:
(1) turn echo on or off
Format: echo [{on | off}]
If you want to turn off the display of the "ECHO OFF" command line itself, you need to precede the command line with "@".
(2) display the current ECHO setting status
Format: echo
(3) output prompt information
Format: ECHO message content
These are the three common uses of the ECHO command, which you are familiar with and can use, but as a gold digger of the DOS command, you should also know the following techniques:
(4) close the DOS command prompt
By typing ECHO OFF at the DOS prompt, you can turn off the display of the DOS prompt so that only the cursor is left on the screen until you type ECHO ON before the prompt reappears.
(5) output a blank line, which is equivalent to entering an enter
Format: ECHO.
It is worth noting that the "." on the command line should be followed by ECHO without a space, otherwise "." will be output to the screen as a prompt. In addition, "." can be replaced with any symbol such as,:; / [\] +.
The return of the command ECHO. Output can be turned through the DOS pipeline as input to other commands, such as echo. | time is equivalent to giving a carriage return after the execution of the TIME command. So when executing, the system will automatically return to the DOS prompt after the current time is displayed.
(6) reply to questions in the order
Format: ECHO reply | Command file name
The above format can be used to simplify the operation of some commands that require man-machine dialogue (such as: CHKDSK/F;FORMAT Drive:;del *.). It uses the preset response language output of the ECHO command as the input of the man-machine dialogue command through the DOS pipeline command. The following example is equivalent to entering "Y" enter when a man-machine conversation occurs in the invoked command:
C: > ECHO Y | CHKDSK/F
C: > ECHO Y | DEL A: *. *
(7) create new documents or add document contents.
Format: ECHO file content > file name
ECHO file contents > > file name
For example:
C: > ECHO @ ECHO OFF > AUTOEXEC.BAT sets up automatic batch files
C: > ECHO C:\ CPAV\ BOOTSAFE > > AUTOEXEC.BAT appends content to the automatic batch file
C: > TYPE AUTOEXEC.BAT displays the automatic batch file
@ ECHO OFF
C:\ CPAV\ BOOTSAFE
(8) output the printed content or print control code to the printer
Format: ECHO printer control code >; PRN
ECHO print content >; PRN
The following example is to input a print control code to the MMel 1724 printer. < Alt > 156 is to hold down the alt key and type 156 on the keypad, and so on:
C: > ECHO + 156 / 42 / 116 >; PRN (enter the underscore command FS*t)
C: > ECHO [email=+155@] + 155 @ >; PRN [/ email] (enter the initialization command ESC@)
C: > ECHO. >; PRN (newline)
(9) make the horn sound
C: > ECHO ^ G
"^ G" is entered with Ctrl+G or Alt+007 in the dos window. Entering multiple ^ G can produce multiple tones. The way to use it is to add it directly to the batch file or make it into a batch file call.
The "^ G" here belongs to the use of special symbols. Please see the later chapter of this article.
3 、 PAUSE
PAUSE, everyone who plays the game knows the meaning of time-out.
In this case, stop the execution of the system command and display the following.
Example:
PAUSE
The run shows:
Please press any key to continue. . .
To display other prompts, you can use:
Other prompts for Echo & pause > nul
4 、 errorlevel
Program return code
Echo errorlevel%
At the end of each command run, you can view the return code in this command line format
Used to judge whether the command just was executed successfully
The default value is 0. If an error occurs in the execution of a command, errorlevel is set to 1.
5 、 title
Set the title of the cmd window
Title new title # you can see that the title bar of the cmd window has changed
6 、 COLOR
Sets the default console foreground and background colors.
COLOR [attr]
Attr specifies the color attribute of the console output
The color property is specified by two hexadecimal digits-the first is the background and the second is
Foreground. Each number can be one of the following values:
0 = black 8 = gray
1 = blue 9 = light blue
2 = green A = light green
3 = lake blue B = light green
4 = red C = light red
5 = purple D = lavender
6 = yellow E = light yellow
7 = White F = bright white
If no parameters are given, the command restores the color to when CMD.EXE starts
It's the color. This value comes from the current console window, / T switch, or
DefaultColor registry value.
If you execute the COLOR command with the same foreground and background color, the COLOR command
ERRORLEVEL is set to 1.
For example, "COLOR fc" produces bright red on bright white.
7. Mode configures system devices
Configure system devices.
Serial port: MODE COMm [:] [BAUD=b] [PARITY=p] [DATA=d] [STOP=s]
[to=on | off] [xon=on | off] [odsr=on | off]
[octs=on | off] [dtr=on | off | hs]
[rts=on | off | hs | tg] [idsr=on | off]
Device status: MODE [device] [/ STATUS]
Print redirection: MODE LPTn [:] = COMm [:]
Selected code page: MODE CON [:] CP SELECT=yyy
Code page status: MODE CON [:] CP [/ STATUS]
Display mode: MODE CON [:] [COLS=c] [LINES=n]
Keystroke rate: MODE CON [:] [RATE=r DELAY=d]
Example:
Mode con cols=113 lines=15 & color 9f
This command sets the DOS window size: 15 rows, 113 columns
8. GOTO and:
GOTO's friends who know how to program will know that this means jump.
It is allowed to build a label with ": XXX" in the batch, then use GOTO XXX to jump to the label: XXX, and then execute the labeled command.
Example:
If {% 1} = = {} goto noparms
If "% 2" = "" goto noparms
The name of the tag can be chosen at will, but it is better to be a meaningful string, preceded by a colon to indicate that the string is a tag, and the goto command is based on this colon (:) to find the next step to jump there. You'd better have some instructions so that you and others will seem to understand your intentions.
Example:
@ echo off:startset / a var+=1echo var%if var% leq 3 GOTO startpause
The run shows:
one
two
three
four
10. Start command
A command that invokes an external program in a batch (the external program runs in a new window, and the batch program continues to execute, regardless of the running status of the external program). If you run the external program directly, you must wait for the external program to complete before continuing to execute the remaining instructions
Example: start explorer d:\
Call the graphical interface to open the D disk
11, assoc and ftype
File association
Assoc sets the 'file extension' association, associated to the 'file type'
Ftype sets the 'file type' association, which is associated with the 'executor and parameters'
When you double-click a .txt file, windows does not open it with notepad.exe based on the .txt direct judgment.
Instead, it is judged that .txt belongs to txtfile 'file type'.
Then call the command line associated with txtfile txtfile=%SystemRoot%\ system32\ NOTEPAD.EXE% 1
You can modify these two associations in "folder options" → "File types".
Assoc # shows all 'file extension' associations
Assoc .txt # shows the file type represented by .txt, and the result shows .txt = txtfile
Assoc .doc # shows the file type represented by .doc, and the result shows .doc = Word.Document.8
Assoc .exe # shows the file type represented by .exe, and the result shows .exe = exefile
Ftype # shows all 'file type' associations
Ftype exefile # displays the command line associated with the exefile type, which shows exefile= "% 1"% *
Assoc .txt = Word.Document.8
If you set .txt to a document of type word, you can see that the icons of the .txt file have changed.
Assoc .txt = txtfile
Restore the correct association of .txt
Ftype exefile='1'% *
Restore the correct association of exefile
If the association has been broken, you can run command.com and enter this command
12. Pushd and popd
Toggle current directory
@ echo off
C: & cd\ & md mp3 # create a mp3 folder in C:\
Md d:\ mp4 # create a mp4 folder in D:\
Cd / d d:\ mp4 # change the current directory to d:\ mp4
Pushd c:\ mp3 # saves the current directory and changes the current directory to c:\ mp3
Popd # restore the current directory to the d:\ mp4 you just saved
It is generally not very useful, and it will be helpful when the current directory name is uncertain. (useful in dos programming)
13 、 CALL
The CALL command can invoke another batch during batch execution, and then continue to execute the original batch after the other batch has been executed.
CALL command
Invoking a batch command has the same effect as executing the command directly, which is useful in special cases, such as multi-level nesting of variables, as shown later in the tutorial. In batch programming, you can generate a command string according to certain conditions, which can be executed with call, as shown in the example.
CALL [drive:] [path] filename [batch-parameters]
Other batch programs called. The filename parameter must have a .bat or .cmd extension.
CALL: label arguments
Call the command section in this file, which is equivalent to a subroutine. The called command segment begins with the label: label
Ends with the command goto: eof.
In addition, the parameters of the script (% 0,% 1, etc.) have been changed as follows:
The% * in the batch script indicates all parameters (such as% 1% 2% 3% 4% 5.)
The substitution of the batch parameter (% n) has been enhanced. You can use the following syntax: (run the following example directly if you don't understand)
% ~ 1-delete quotation marks ("), expand% 1
% ~ F1-extends% 1 to a fully qualified pathname
% ~ D1-expand% 1 to only one drive letter
% ~ p1-extend% 1 to only one path
% ~ N1-expand% 1 to only one file name
% ~ x1-extend% 1 to only one file extension
The path of% S1-extension contains a short name
% ~ A1-extends% 1 to file attributes
% ~ T1-extend% 1 to the date / time of the file
% ~ Z1-expand% 1 to the size of the file
% ~ $PATH: 1-find the directory listed in the PATH environment variable and set% 1
Expand to the first fully qualified name found. If the environment
If the variable name is not defined, or the file is not found, this key combination will
Expand to an empty string
You can combine modifiers to achieve multiple results:
% ~ dp1-extends% 1 only to the drive letter and path
% ~ nx1-extends% 1 only to the file name and extension
% ~ dp$PATH:1-look for% 1 in the directory listed in the PATH environment variable
And expand to the drive letter and path of the first file found.
% ~ ftza1-extends% 1 to output lines like DIR.
In the above example,% 1 and PATH can be replaced by other valid values.
% ~ the syntax is terminated by a valid parameter number. % ~ modifier cannot be used with% *
Note: when expanding the parameter, regardless of whether the file represented by the parameter exists or not, it is expanded with the current directory.
To understand the above knowledge, the following example is crucial.
Example:
@ echo offEcho generates a temporary file > the tmp.txtRem lower line saves the current directory first, and then sets c:\ windows to the current directory pushd c:\ windowsCall: sub tmp.txtRem downline to restore the previous current directory PopdCall: sub tmp.txtpauseDel tmp.txtexit:sub
Echo delete quotation marks:% ~ 1
Echo extended to path:% ~ F1
Echo is extended to a drive letter:% ~ D1
Echo extends to a path:% ~ p1
Echo is expanded to a file name:% ~ N1
Echo extends to a file extension:% ~ x1
The path to the Echo extension contains the short name:% ~ S1
Echo is extended to file attribute:% ~ A1
Date / time when Echo was extended to the file:% ~ T1
Echo extends to file size:% ~ Z1
Echo extends to drive letter and path:% ~ dp1
Echo extends to file name and extension:% ~ nx1
Echo extends to output lines like DIR:% ~ ftza1
Echo.
Goto: eof
Example:
Set aa=123456set cmdstr=echo aa%call cmdstr%pause
In this example, running% cmdstr%, instead of call will show the result% aa%, instead of 123456
14 、 shift
Change the location of replaceable parameters in the batch file.
SHIFT [/ n]
If the command extension is enabled, the SHIFT command supports the / n command line switch, which tells the
The command shifts from the nth argument; n is between zero and eight. For example:
SHIFT / 2
Will shift 3 to 2, 4 to 3, and so on; and will not affect 0 and 1.
15 、 IF
The syntax format of IF conditional judgment statement is as follows:
IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
The following is described one by one. For a more detailed analysis, please see the following chapters.
(1) IF [NOT] ERRORLEVEL number command
The sentence IF ERRORLEVEL must be placed after a command, and it is up to IF ERRORLEVEL to determine the return value of the command after the command is executed.
The numerical value range of Number is 0255. it is judged that the order of values should be from large to small. The condition holds when the returned value is greater than or equal to the specified value.
Example:
@ echo off
Dir c:
If the rem exit code is > = 1, jump to heading 1, and jump to title 0 if > = 0.
IF ERRORLEVEL 1 goto 1
IF ERRORLEVEL 0 goto 0
The two lines above the Rem cannot be swapped, otherwise failure will show success.
: 0
The echo command was executed successfully!
After the execution of the Rem program, jump to the title exit and exit.
Goto exit
: 1
Failed to execute the echo command!
After the execution of the Rem program, jump to the title exit and exit.
Goto exit
: exit
Pause
The operation shows that the command was executed successfully!
(2) IF [NOT] string1==string2 command
String1 and string2 are both character data. The case of characters in English will be regarded as different. In this condition, the equal sign must be two (absolutely equal).
When the conditions are equal, the subsequent command is executed.
To detect the value of the current variable to make a decision, to prevent spaces in the string, you can use the following format
If [NOT] {string1} = = {string2} command
If [NOT] [string1] = = [string2] command
If [NOT] "string1" = = "string2" command
This way of writing actually treats parentheses or quotation marks as part of a string, as long as the left and right sides of the equal sign are consistent, such as the following:
If {string1} = = [string2] command
(3) IF [NOT] EXIST filename command
EXIST filename means the existence of a file or directory
Echo off
IF EXIST autoexec.bat echo file exists!
IF not EXIST autoexec.bat echo file does not exist!
You can execute this batch on disk C and disk D respectively to see the effect.
16. Setlocal and variable delay
The content of this article refers to the batch processing tutorial produced by Hero:
If you want to advance, variable delay is a must! So I hope you can take a closer look at this part.
In order to better illustrate the problem, let's introduce an example first.
Example 1:
@ echo offset a=4set adept 5 & echo% a%pause
Results: 4
Explanation: why 4 instead of 5? Have you changed the value of variable a to 5 before echo?
Let's first take a look at the mechanism for batch running commands:
When a batch reads a command, it is read by line (for example, the for command, etc., and all statements closed with a pair of parentheses are treated as one line), and the necessary preprocessing work is completed before processing, including the assignment of variables in the line command. Let's now analyze example 1. The batch process reads and preprocesses the whole sentence before running to the sentence "set a% & echo% a%"-- if you assign a value to the variable a, then% a% is of course 4! (for no reason, that's what batch processing does. )
In order to perceive the dynamic changes of environmental variables, the variable delay is designed for batch processing. To put it simply, after reading a complete statement, the variable of the row is not assigned immediately, but before a single statement is executed, that is, the assignment of the variable is "delayed".
So how do you turn on variable delay? What do you need to pay attention to about variable delay? Give an example to illustrate:
Example 2:
@ echo offsetlocal enabledelayedexpansionset a=4set axi5 & echo! axipause
Results: 5
Explanation: variable delay is started and the correct answer is obtained. The delayed startup statement for the variable is "setlocal enabledelayedexpansion", and the variable uses a pair of exclamation marks "!!" Wrap it up (pay attention to the English exclamation mark), otherwise there will be no effect of variable delay.
To analyze example 2, first "setlocal enabledelayedexpansion" turns on the variable delay, and then "set axiom 4" first assigns the variable a to
4, "set averse 5 & echo! a!" This sentence assigns the variable a to 5 and outputs (because of the delay in starting the variable, the batch can sense the dynamic change, that is, it does not assign a value to the variable first, but to the variable during operation, so the value of an is 5).
Give me another example to consolidate it.
Example 3:
@ echo offsetlocal enabledelayedexpansionfor / l% I in (1penny 1je 5) do (set a=%%iecho! a!) pause
Results:
one
two
three
four
five
Explanation: this example turns on variable delay and uses "!!" Expand the variables so that we get the expected results. What happens if you don't use variables to delay?
What's the result? The result is this:
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
That is, you are not aware of the dynamic changes in the for statement.
Tip: in the case of no delay in opening variables, variable changes in a command line must not be reflected until the next command. This point can also be taken advantage of. Look at the example.
Example: exchange the values of two variables without intermediate variables
@ echo off:: purpose: exchange the values of two variables, but do not use temporary variables:: Code by JM 2007-1-24 [email=CMD@XP] CMD@ XP [/ email]:: source: http://www.cn-dos.net/forum/viewthread.php?tid=27078set var1=abcset var2=123echo before exchange: after var1=%var1% var2=%var2%set var1=%var2%& set var2=%var1%echo exchange: var1=%var1% var2=%var2%pause
17. ATTRIB displays or changes file properties
ATTRIB [+ R |-R] [+ A |-A] [+ S |-S] [+ H |-H] [[drive:] [path] filename] [/ S [/ D]]
+ set properties.
-clear the attribute.
R read-only file properties.
An Archive file properties.
S system file properties.
H hides file properties.
[drive:] [path] [filename]
Specifies the file properties to be processed.
/ S processes matching files in the current folder and its subfolders.
/ D also handles folders.
Example:
Md autorunattrib + a + s + h autorun
The above command creates the folder autorun, and then sets it as archive, system, hidden properties
At this point, the study on "how to use common batch internal commands" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.