Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basis of DOS batch processing?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces you what is the basis of DOS batch processing, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Brief introduction of commonly used batch Internal commands

Batch definition: as the name implies, a batch file is a collection of commands in a certain order into an executable text file with the extension BAT or CMD. These commands are collectively referred to as batch commands.

Tip: you can press the Ctrl+C key combination on the keyboard to forcibly terminate the execution of a batch.

After understanding the general meaning, we officially began to study. First look at a simple example!

@ echo offecho "Welcome to very BAT!" pause

Save the above three commands as a file test.bat or test.cmd and execute them, and he will display the following two jargon on the screen:

The code is as follows:

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!

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 contents > 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 + 155 @ > PRN (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 the color it was when CMD.EXE started. This value comes from the current console window, the / T switch, or the DefaultColor registry value.

If you execute the COLOR command with the same foreground and background color, the COLOR command sets ERRORLEVEL 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 noparmsif "% 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

9 、 find

Search for strings in the file.

FIND [/ V] [/ C] [/ N] [/ I] [/ off [line]] "string" [[drive:] [path] filename [...]]

/ V displays all lines that do not contain the specified string.

/ C displays only the number of lines that contain a string.

/ N displays the line number.

/ I ignores case when searching for strings.

/ OFF [LINE] do not skip files with offline property sets.

"string" specifies the text string to search for

[drive:] [path] filename

Specify the files to search for.

If no path is specified, FIND searches for text typed or generated by another command.

Find is often used in conjunction with the type command

Type [drive:] [path] filename | find "string" [> tmpfile] # pick lines containing string

Type [drive:] [path] filename | find / v "string" # remove lines containing string in the file

Type [drive:] [path] filename | find / c # shows the number of file lines

The above usage removes the prompt (file name prompt) that comes with the find command.

Example:

@ echo offecho 111> test.txtecho 222> test.txtfind "111" test.txtdel test.txtpause

The run is shown as follows:

-TEST.TXT

one hundred and eleven

Please press any key to continue. . .

Example:

@ echo offecho 111 > test.txtecho 22 2 > test.txttype test.txt | find "111" del test.txtpause

The run is shown as follows:

one hundred and eleven

Please press any key to continue. . .

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 and 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 expand% 1 to the first fully qualified name found. If the environment variable name is not defined, or if the file is not found, this key combination is extended 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 > tmp.txtRem the following line to save the current directory first Then set c:\ windows to the current directory pushd c:\ windowsCall: sub tmp.txtRem downlink to restore the previous current directory PopdCall: sub tmp.txtpauseDel tmp.txtexit:subEcho delete quotation marks:% ~ 1Echo extension to path:% ~ f1Echo extension to a drive letter:% ~ d1Echo extension to a path:% ~ p1 Echo extension to a file name:% ~ n1Echo extension to a file extension:% ~ x1Echo extension refers to a path that contains Short name:% S1 Echo extension to file attributes:% a1Echo extension to file date / time:% ~ T1 Echo extension to file size:% ~ Z1 Echo extension to drive letter and path:% ~ dp1Echo extension to file name and extension:% ~ nx1Echo extension to DIR-like output line:% ~ ftza1Echo.Goto: eof

The code is as follows:

Set aa=123456

Set 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:

If the @ echo offdir c:rem exit code is > = 1, jump to the title 1 and execute the two lines above the IF ERRORLEVEL 1 goto 1IF ERRORLEVEL 0 goto 0Rem if the exit code > = 0 is executed. Otherwise, failure will show success. The 0echo command was executed successfully! After the execution of the Rem program, jump to the title exit to exit the goto exit:1echo command execution failed! After the execution of the Rem program, skip to the title exit and exit goto exit:exitpause.

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 offIF 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.

Analyze example 2. First, "setlocal enabledelayedexpansion" turns on variable delay, and then "set axiom 4" first assigns variable a to 4, "set axiom 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 CMD@XP:: 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 autorun

Attrib + a + s + h autorun

The above command creates the folder autorun, and then sets it as archive, system, hidden properties

Section 2 Special symbols commonly used

1. @ command line echo mask

2.% batch variable leader

3. > redirector

4, > > redirector

5. &, redirector

Output redirect command

This character means to pass and overwrite, and its role is to pass the result of the run to the following scope (either a file or the default system console).

In the NT series command line, the scope of redirection changes from the entire command line to a single command statement, which is restricted by the command separator &, & &, | | and statement blocks.

For example:

Use the command: echo hello > 1.txt to create the file 1.txt with the content "hello" (notice a space at the end of the line)

Use the command: echo hello > 1.txt to create the file 1.txt with the content "hello" (note that there is no space at the end of the line)

4, > > redirector

Output redirect command

The function of this symbol is somewhat similar to >, but the difference is that > > is passed and appended at the end of the file, while > is overridden.

Usage is the same as above

Also take 1.txt as an example.

Use the command:

Echo hello > 1.txtecho world > > 1.txt

At this point, the 1.txt content is as follows:

Hello

World

5. &, nul means to prohibit the output of correct information

2 > nul means that the output of error messages is prohibited.

Where 1 and 2 are addresses that represent the input and output of a data stream (NT CMD calls it a handle and MSDOS calls it a device).

Handle 0: standard input stdin, keyboard input

Handle 1: standard output stdout, output to the command prompt window (console, code CON)

Handle 2: standard error stderr, output to the command prompt window (console, code CON)

The stdin can be redirected by >.

We already know that you can use the for command to read the contents of the text, but it's a bit troublesome to use the for command if you only need to read the first line. The simple solution is as follows:

@ echo offset / p str=1.txt

The result is: test > 1.txt

He didn't add it to 1.txt, hehe. It just shows it.

In addition, this escape character can also be used as a continuation symbol.

Take a simple example:

@ echo offecho hero ^ is ^ good ^ man pause

Needless to say, you'll see if you try it yourself.

Why does an escape character act as a continuation character at the end of a line? The reason is simple, because there is an invisible symbol at the end of each line, the carriage return, which is invalidated when the escape character is at the end of the line, thus continuing the line.

8. & combine commands

Syntax: the first command & the second command [& the third command.]

&, & &, | | is a combined command, which, as the name implies, can be executed by combining multiple commands into a single command. This is allowed in batch scripts and is widely used. Because the batch process does not recognize the number of commands.

This symbol allows you to use more than two different commands on one line, and when the first command fails, it does not affect the execution of subsequent commands.

Here & the commands on both sides are executed sequentially, from the back.

For example:

The code is as follows:

Dir z:\ & dir y:\ & dir c:\

The above command will continuously display the contents of disk zMY and c, regardless of whether the disk exists.

9. & & combination command

Syntax: the first command & the second command [& & the third command.]

In this way, multiple commands can be executed at the same time. When there is an error in the execution of the command, the subsequent command will not be executed, and all the commands will be executed all the time if there has been no error.

This command is similar to the one above, but the difference is that when the first command fails, the later command will not be executed

The code is as follows:

Dir z:\ & & dir y:\ & & dir c:\

10. | | combine commands

Syntax: first command | | second command [| | third command.]

In this way, multiple commands can be executed at the same time. When one command fails, the second command is executed. When the correct command is executed, the subsequent command will not be executed. If there is no correct command, all the commands will be executed all the time.

Tip: priority must be paid to when using the combined command with the redirect command.

Pipe commands take precedence over redirect commands, and redirect commands take precedence over combined commands.

Problem: list the files and folders of disk C and disk D in the a.txt file. Look at examples

The code is as follows:

Dir c:\ & & dir d:\ > a.txt

After this execution, there is only D disk information in the a.txt! Why? Because the priority of the combined command is not as high as that of the redirect command! So this sentence divides the line into two parts: dir c:\ and dir d:\ > a.txt, not as you might think: dir c:\ & & dir d:\ and > a.txt. To use the combined command & & to meet the requirements of the title, you must write:

The code is as follows:

Dir c:\ > a.txt & & dir d:\ > a.txt

In this way, according to the priority, DOS will divide the sentence into the following two parts: dir c:\ > a.txt and dir d:\ > a.txt. The difference of a few sentences in example 18 is quite special and is worth studying and understanding.

Of course, you can also use the command here (think for yourself):

The code is as follows:

Dir c:\ > a.txt & dir d:\ > a.txt

11. "string delimiter"

Double quotation marks allow you to include spaces in a string, and you can enter a special directory in the following ways

The code is as follows:

Cd "program files"

Cd progra~1

Cd pro*

The above three methods can be entered into the program files directory

12. Comma

Commas are equivalent to spaces, and in some cases "," can be used as spaces to make

such as

Dir,c:\

13; semicolon

Semicolon, when the command is the same, different targets can be used to isolate, but the execution effect remains the same. If an error occurs during execution, only an error report is returned, but the program will still execute. (some people say that they will not continue to execute, but in fact, you will know after a test.)

For example:

The code is as follows:

Dir c:\; d:\; e:\; z:\

The above command is equivalent to

Dir c:\

Dir d:\

Dir e:\

Dir f:\

If the z disk does not exist, the run shows that the system cannot find the specified path. Then terminate the execution of the command.

Example: dir c:\; d:\; e:\ 1.txt

The above command is equivalent to

Dir c:\

Dir d:\

Dir e:\ 1.txt

The file e:\ 1.txt does not exist, but the e disk exists and there is an error prompt, but the command will still be executed.

Why? If the destination path does not exist, execution is terminated; if the path exists and only the file does not exist, execution continues.

That's all! If you have any comments, please reply! If you have any questions, please go to the BAT communication area to post! The next section is improved!

14. () brackets

Parentheses play a special role in batch programming, the left and right parentheses must be used in pairs, and the parentheses can include multiple lines of commands, which are treated as a whole and as a command line.

Parentheses are common in for and if statements and are used to nest loops or conditional statements. In fact, parentheses () can also be used alone, as shown in the example.

Example:

Command: echo 1 & echo 2 & echo 3

It can be written as:

The code is as follows:

(

Echo 1

Echo 2

Echo 3

)

The effect of the above two ways of writing is the same, both of which are considered to be a command line.

Note: when such multiple commands are treated as one command line, variable latency is involved if there are variables in it.

15 、! Exclamation point

Nothing to say, in the case of variable delay, it is used to represent a variable, that is,% var% should be expressed as! var!.

On the basis of DOS batch processing is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report