Batch Processing (BAT) command Overview

Source: Internet
Author: User
Tags echo command

1 echo
@ # Disable single-row echo
Echo off # Close echo from the next line
@ Echo off # disable echo from the beginning of the line. This is the first line of batch processing.
Echo on # enable echo from the next line
Echo # shows whether the current status is echo off or echo on.
Echo. # output a line break, blank line
# (Same as ECHO, ECHO; echo + echo [Echo] echo/ECHO \)

2 errorlevel
Echo % errorlevel %
After each command is run, you can view the return code in the command line format.
The default value is 0. If an error occurs during command execution, the errorlevel is set to 1.

3 dir
Dir # display files and subdirectories in the current directory
DIR/A # display files and subdirectories in the current directory, including hidden files and system files
Dir C:/A: d # display the directory in the current directory of drive C
Dir C:/a:-D # display files in the C root directory
Dir c: \/B/P #/B only display file names,/P display by PAGE
Dir *. EXE/s directory displays all the. exe files in the current and sub-directories.

4 CD
CD \ # enter the root directory
CD # display the current directory
CD/d: \ SDK # You can change the drive letter and directory at the same time.

5 MD
Md d: \ A \ B \ C # If D: \ A does not exist, the intermediate directory is automatically created.
# If the command extension is disabled, type mkdir \ A \ B \ c.

6 Rd
Rd ABC # Delete the ABC subdirectory in the current directory, which must be an empty directory
RD/S/q D: \ Temp # Delete the D: \ Temp folder and Its subfolders and files./Q quiet mode

7 del
Del D: \ test.txt # delete a specified file. It cannot be a hidden, system, or read-only file.
Del/Q/A/f d: \ temp \*.*
Delete all files in the D: \ Temp folder, including hidden, read-only, and system files, excluding subdirectories.
Del/Q/A/f/s d: \ temp \*.*
Delete D: \ Temp and all files in the subfolders, including hidden, read-only, and system files, excluding subdirectories.

8 Ren

Ren D: \ Temp TMP # folder rename is supported

9 CLS
Clear Screen

10 type
Show File Content
Type C: \ Boot. ini # displays the content of the specified file,ProgramThe file is usually garbled
Type *. txt contents displays the contents of the TXT file in the current directory.

11 copy
Copy an object
Copy c: \ test.txt D: \ test. Bak
Copy the c: \ test.txt file to D: \ and rename it test. Bak.
Copy con test.txt
Wait for the input from the screen, press Ctrl + Z then, and the input content is saved as the test.txt file.
Con stands for screen, PRN stands for printer, NUL stands for empty Device
Copy 1.txt + 2.txt 3.txt
Merge 1.txt and 2.txt contents and save them as 3.txt files.
If 3.txt is not specified, save it to 1.txt.
Copy test.txt +
Copying a file to yourself actually modifies the file date.

12 Title
Set the title of the CMD window
Title New title # The title bar of the CMD window has changed

13 ver
Show System Version

14 label and vol
Set the volume label
Vol # display the volume label
Label # displays the volume label and prompts you to enter a new volume label.
Label C: system # Set the volume of drive C to System

15 pause
Pause command

16 REM and ::
Comment command
The comment row does not perform the operation.

17 Date and Time
Date and Time
Date # displays the current date, and prompts you to enter a new date. Press \ "press enter \" to skip the input.
Date/T # only display the current date, do not prompt to enter a new date
Time # displays the current time, and prompts you to enter a new time. Press \ "press enter \" to skip the input.
Time/T # Only the current time is displayed, and a new time is not prompted

18 Goto and:
Jump command
: Label # The first line is: indicates that this line is a label line, and the label line does not perform operations
Goto label # Jump to the row of the specified tag

19 find (external command)
SEARCH Command
Find \ "ABC \" C: \ test.txt
Search for lines containing ABC strings in the C: \ test.txt File
If not, set errorlevel to 1.
Find/I "ABC" C: \ test.txt
Searches for rows containing ABC, regardless of Case sensitivity.
Find/C \ "ABC \" C: \ test.txt
Display the number of rows containing ABC

20 more (External commands)
Display On Screen
More c: \ test.txt # display the file content of C: \ test.txt on screen

21 tree
Show directory structure
Tree D: \ # display the directory structure of the D Drive

22 &
Execute multiple commands in sequence, regardless of whether the command is successfully executed.

23 &&
Multiple commands are executed sequentially. If an error occurs, the subsequent commands are not executed.
Find \ "OK \" C: \ test.txt & Echo successful
If \ "OK \" is found, \ "Success \" is displayed. If \ "OK \" is not found

24 |
Execute multiple commands in sequence. When the correct command is executed, the subsequent commands will not be executed.
Find \ "OK \" C: \ test.txt | echo failed
If \ "OK \" is not found, \ "unsuccessful \" is displayed. If \ "OK \" is found, it is not displayed.

25 |
Pipeline command
Dir *. */S/A | find/C \ ". exe \"
The pipeline command indicates that the Dir command is executed first, and the find command after the output result is executed
Command Line result: output the number of. EXE files in the folder and all subfolders.
Type C: \ test.txt | more
This is the same as more c: \ test.txt.

26> and>
Output redirection command
> Clear the original content of the file and write it again.
> Append the content to the end of the file without clearing the original content.
Output the content originally displayed on the screen to a specified file.
If the specified file does not exist, the file is automatically generated.
Type C: \ test.txt> prn
If no file content is displayed on the screen, the system redirects to the printer.
Echo Hello World> con
Display Hello World on the screen. In fact, all outputs are by default> con
Copy c: \ test.txt F:> NUL
The \ "file copied successfully \" prompt is not displayed. However, if the f disk does not exist, an error message is displayed.
Copy c: \ test.txt F:> NUL 2> NUL
The prompt "file copied successfully" is not displayed, and the error message is not displayed if the drive F does not exist.
Echo ^ w ^> ^ W> C: \ test.txt
The generated file content is ^ W> W
^ And> are control commands. to output them to files, you must add a ^ symbol before them.

27 <
Get input information from the file instead of from the screen
It is generally used for date Time Label and other commands that need to be input
@ Echo off
Echo 2005-05-01> temp.txt
Date del temp.txt
In this way, you can directly modify the current date without waiting for the input.

28% 0% 1% 2% 3% 4% 5% 6% 7% 8% 9% *
Parameters passed by the command line to the batch processing
% 0 batch processing file itself
% 1 first Parameter
% 9 ninth Parameter
% * All parameters starting with the first parameter

The substitution of the batch parameter (% N) has been enhanced. You can use the following syntax:

% ~ 1-delete quotation marks (\ ") and expand % 1
% ~ F1-extended % 1 to a fully qualified path name
% ~ D1-only expand % 1 to one drive letter
% ~ P1-only expand % 1 to one path
% ~ N1-only expand % 1 to one file name
% ~ X1-only expand % 1 to one file extension
% ~ S1-expanded path refers to short name
% ~ A1-extended % 1 to file attributes
% ~ T1-extend % 1 to the date/time of the file
% ~ Z1-expand % 1 to the file size
% ~ $ Path: 1-find the directory of the PATH environment variable and set % 1
Extended to the first fully qualified name found. If the environment
The variable name is not defined, or the file is not found.
Extended to a null string

You can combine the modifier to obtain multiple results:

% ~ Dp1-extend % 1 to drive letter and path only
% ~ Nx1-only extend % 1 to the file name and extension
% ~ DP $ path: 1-find % 1 in the directory column in the PATH environment variable,
And extend to the drive letter and path of the first file.
% ~ Ftza1-extend % 1 to output rows similar to Dir.
Refer to call /? Or /? See the meaning of each parameter
Echo load \ "% 1 \" \ "% 2 \"> C: \ test.txt
The generated file content is load \ "% 1 \" \ "% 2 \"
In the batch file, output the command line parameters to the file in this format

29 if
Judgment command
If \ "% 1 \" = \ "/A \" Echo, the first parameter is/
If/I \ "% 1 \" equ \ "/A \" Echo, the first parameter is/
/I indicates case-insensitive. EQU and = are the same. For other operators, see if /?
If exist c: \ test. Bat echo exists in the C: \ test. BAT file.
If not exist C: \ WINDOWS (
ECHO does not exist in the C: \ Windows Folder
)
If exist c: \ test. BAT (
ECHO has c: \ test. bat
) Else (
ECHO does not exist c: \ test. bat
)

30 setlocal and endlocal
Set "command extension" and "delay expansion of environment variables"
Setlocal enableextensions # enable \ "command extension \"
Setlocal disableextensions # disable \ "command extension \"
Setlocal enabledelayedexpansion # enable \ "Deferred Environment Variable Expansion \"
Setlocal disabledelayedexpansion # Stop \ "Deferred Environment Variable Expansion \"
Endlocal # restore to the status before using the setlocal statement
The "command extension" is enabled by default.
"Extended environment variables" is disabled by default.
The default value is automatically restored after the batch processing is completed.
You can modify the Registry to disable \ "command extension \". For details, see CMD /? . Therefore, the \ "command extension \" is used.
It is recommended that you add the setlocal enableextensions and endlocal statements at the beginning and end to confirm
Programs can run correctly on other systems
\ "Deferred Environment Variable Expansion \" is mainly used for the IF and for Statement, in the Set description There are actually use routines

31 set
Set Variables
You can add % before and after the variable name, that is, % variable name %.
Set # display all currently available variables, including system variables and custom Variables
Echo % systemdrive % # displays the system disk drive letter. System variables can be directly referenced.
Set P # Show all variables starting with P. If one variable does not exist, set errorlevel = 1.
Set P = aa1bb1aa2bb2 # Set the Variable P and assign the value = The following string, that is, aa1bb1aa2bb2
Echo % P % # display the string represented by Variable P, that is, aa1bb1aa2bb2
Echo % P :~ 6% # display all the characters after the 6th characters in Variable P, that is, aa2bb2
Echo % P :~ 6, 3% #3 characters after 6th characters are displayed, that is, Aa2
Echo % P :~ 0, 3% # display the first three characters, namely aa1
Echo % P :~ -2% # display the last two characters (B2)
Echo % P :~ 0,-2% # display other characters except the last two characters, that is, aa1bb1aa2b
Echo % P: AA = C % # Replace all AA in Variable P with C, that is, c1bb1c2bb2 is displayed.
Echo % P: AA =%# replace all AA strings in Variable P with null, that is, 1bb12bb2 is displayed.
Echo % P: * BB = C % # The first BB and all its previous characters are replaced with C, that is, c1aa2bb2 is displayed.
Set P = % P: * BB = C % # Set the Variable P and assign the value to % P: * BB = C %, that is, c1aa2bb2.
Set/a p = 39 # Set P to a numeric variable with a value of 39
Set/a p = 39/10 # support operators. If there is a decimal number, use the Tail removal method. The value 39/10 = 3.9 indicates 3, P = 3.
Set/a p = P/10 # When the/a parameter is used, the variable after = can be directly referenced without %
Set/a p = "1 & 0" # "and" must be enclosed by quotation marks. For other supported operators, see set /?
Set P = # cancel the P variable
Set/P = enter
"Enter" is displayed on the screen, and the input string is assigned to Variable P.
Note that this line can be used to replace the choice command
Note that all variables are replaced in the IF and for compound statements at one time, as shown in figure
@ Echo off
Set P = aaa
If % P % = aaa (
Echo % P %
Set P = bbb
Echo % P %
)
The result is displayed.
Aaa
Aaa
Because all % P % has been replaced with AAA when reading the if statement
Here \ "replace \", in /? The help indicates \ "Expansion \" and \ "Environment Variable Expansion \"
You can enable "delay expansion of environment variables! To reference variables, that is! Variable name!
@ Echo off
Setlocal enabledelayedexpansion
Set P = aaa
If % P % = aaa (
Echo % P %
Set P = bbb
Echo! P!
)
Endlocal
The result is displayed.
Aaa
Bbb
There are several dynamic variables that cannot be seen when running set.
% Cd % # represents the string of the current directory
% Date % # current date
% Time % # current time
% Random % # random integer, ranging from 0 ~ 32767
% Errorlevel % # current errorlevel Value
% Cmdextversion % # current command processor extension version number
% Worker line % # Call the original command line of the command processor
You can use the echo command to view the value of each variable, such as Echo % time %
Note that % time % is accurate to milliseconds, which can be used when batch processing requires delay.

32 start
Execute the remaining commands only after the external program is completed.

33 call
Call another batch processing command in batch processing; otherwise, the remaining batch processing commands will not be executed.
Sometimes some applications use start to call an error or call

34 choice (external command)
Select command
Let the user enter a character to choose to run different commands. The Returned Code errorlevel is 1234 ......
Choice.com in Win98
Not in win2000pro. You can copy it from Win98.
Choice.exe is in win2003.
Choice/n/c y/T 5/d y> NUL
Latency: 5 seconds

35 assoc and FTYPE
File Association
Assoc sets 'file extension' Association and is associated with 'file type'
FTYPE sets 'file type' Association and is associated with 'execution program and parameter'
When you double-click a. txt file, Windows does not use Notepad. txt to directly determine whether to use notepad.exe to open the file.
First, cmd.txt belongs to the txtfile 'file type'
Then, call the txtfile-related command line txtfile = % SystemRoot % \ system32 \ notepad. EXE % 1.
You can modify these two associations in \ "Folder Options \" → \ "file type \"
Assoc # display all 'file extension' associations
Assoc. txt example shows the 'file type' represented by .txt, and the result shows. txt = txtfile
Assoc. Doc types: 'file type' represented by .doc. The result shows. Doc = word. document.8
Assoc. EXE displays the 'file type' represented by .exe, and the result shows. EXE = exefile.
FTYPE # display all 'file type' associations
FTYPE exefile # display the command line associated with the exefile type. The result shows exefile = \ "% 1 \" % *
Assoc. txt = word. document.8
Set .txtas the wordmarked document. You can see that the icons in 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 damaged, run command.com and enter this command.

36 pushd and popd
Switch the current directory
@ Echo off
C: & CD \ & MD MP3 # create an MP3 folder in c: \
Md d: \ MP4 # create an MP4 folder in D: \
CD/d: \ MP4 # change the current directory to D: \ MP4
Pushd c: \ MP3 # Save the current directory and switch to C: \ MP3
Popd # restore the current directory to the saved D: \ MP4 file.

37
Loop command
This is complicated. See /? Let's see
For % I in (C: D: e: F :) Do echo % I
Call each string in parentheses and execute the command after do.
Note % I: In batch processing, the for statement calls two parameters: %
The default string separator is \ "Space key \", \ "tab key \", \ "Enter key \"
For % I in (*. txt) do find \ "ABC \" % I
Execute the find command on all TXT files in the current directory.
For/R. % I in (*. txt) do find \ "ABC \" % I
Search for lines containing the ABC string in the TXT file in the current directory and the subdirectory.
For/R. % I in (.) Do echo % ~ PNI
Displays the current directory name and all subdirectory names, including paths, excluding drive letters.
For/r d: \ MP3 % I in (*. mp3) Do echo % I> D: \ mp3.txt
Save the file names of the MP3 files in D: \ MP3 and Its subdirectories to D: \ mp3.txt.
For/L % I in (2, 1, 8) Do echo % I
Generate a string of 2345678 numbers. 2 indicates the beginning of the number sequence, 8 indicates the end, and 1 indicates that 1 is added each time.
For/F % I in ('set') Do echo % I
Cyclically call the output result of the SET command. Each line has one
For/f \ "EOL = p \" % I in ('set') Do echo % I
Take the output result of the SET command and ignore the rows starting with P.
For/F % I In (d: \ mp3.txt) Do echo % I
Each file name in D: \ mp3.txt is displayed. Each row has one file name. names with spaces are not supported.
For/f \ "delims = \" % I In (d: \ mp3.txt) Do echo % I
Each file name in D: \ mp3.txt is displayed. Each row has one file name and supports names with spaces.
For/f \ "Skip = 5 tokens = 4 \" % A in ('dir') Do echo %
For the result of the Dir command, skip the first five lines and take 4th columns for the remaining lines.
The delimiter between each column is the default \ "Space \"
Note that the first five lines output by the Dir command have no file names.
For/f \ "tokens = 1, 2, 3 delims =-\" % A in ('date/t') Do (
Echo %
Echo % B
Echo % C
)
For the output result of date/t, columns 1, 2, and 3 are taken for each row.
The first column corresponds to the specified % A, followed by % B and % C are derived, corresponding to other columns
The separator is-and \ "Space \". Note that there is a \ "Space \" after delims = \"
Here, if tokens = 1, 2, and 3 are replaced with tokens = 1-3, the effect is the same.
For/f \ "tokens = 2 * delims =-\" % A in ('date/t') Do echo % B
Take the 2nd column to % A, and all the subsequent columns to % B

38 SUBST (external command)
Map disks.
Subst z: \ Server \ D # Enter Z: To access \ Server \ D.
Subst z:/D # cancel the ing
SUBST # display all current Mappings

39 xcopy (external command)
File copy
Xcopy D: \ MP3 E: \ MP3/S/e/I/y
Copy the D: \ MP3 folder, all subfolders, and files to E: \ to overwrite existing files.
Adding/I indicates that if E: \ does not have an MP3 folder, a new one will be automatically created; otherwise, a query will be made.

 

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.