BAT Command Daquan
1 Echo and @
@ #关闭单行回显
echo off #从下一行开始关闭回显
@echo off #从本行开始关闭回显. General batch The first line is this.
echo on #从下一行开始打开回显
Echo #显示当前是 echo off state or echo on state
Echo. #输出一个 "Carriage return", Blank line
# (with Echo, echo; echo+ echo[Echo] echo/echo\)
2 ERRORLEVEL
Echo%errorlevel%
Each command runs to the end, you can view the return code in this command line format
The default value is 0, and General Command execution errors are set to ERRORLEVEL to 1
3 dir
Dir #显示当前目录中的文件和子目录
dir/a #显示当前目录中的文件和子目录, including hidden files and system files
Dir c:/a:d #显示 C-Disk directory in the current directory
Dir c:/a:-d #显示 C files in the packing directory
Dir c \/b/p #/b only displays the file name,/p pagination display
Dir *.exe/s #显示当前目录和子目录里所有的. exe files
4 CD
Cd\ #进入根目录
CD #显示当前目录
CD/D D:\SDK #可以同时更改盘符和目录
5 MD
MD d:\a\b\c #如果 d:\a not present, intermediate directories will be created automatically
#如果命令扩展名被停用, you need to type mkdir \a\b\c.
6 Rd
RD ABC #删除当前目录里的 ABC subdirectory, requires an empty directory
rd/s/q d:\temp #删除 d:\temp folder and its subfolders and files,/q Quiet mode
7 del
Del D:\test.txt #删除指定文件, cannot be a hidden, system, read-only file
del/q/a/f d:\temp\*.* #删除 All files in the D:\temp folder, including hidden, read-only, system files, not including subdirectories
del/q/a/f/s d:\temp\*.* #删除 All files in d:\temp and subfolders, including hidden, read-only, system files, not including subdirectories
8 Ren
ren d:\temp tmp #支持对文件夹的重命名
9 CLS
Clear Screen
Ten type
Show file contents
Type C:\Boot.ini #显示指定文件的内容, program files will generally show garbled
Type *.txt #显示当前目录里所有. txt file contents
Copy
Copy files
Copy C:\test.txt D:\test.bak
Copy c:\test.txt file to D:\ and rename to Test.bak
Copy con test.txt
Wait for input from the screen, press CTRL + Z to end the input, save the input as a test.txt file
Con represents the screen, PRN represents the printer, NUL represents the empty device
Copy 1.txt + 2.txt 3.txt
Merge 1.txt and 2.txt content, save as 3.txt file
If you do not specify 3.txt, save to 1.txt
Copy Test.txt +
Copy files to yourself and actually modify the file date
Title
Set the title of the cmd window
Title New Headline #可以看到cmd窗口的标题栏变了
Ver
Show System version
Label and vol
Set the volume label
Vol. #显示卷标
Label #显示卷标 and prompt for new volume label
Label C:system #设置C盘的卷标为 System
Pause
Pause command
REM and::
Comment Commands
Comment lines do not perform actions
Date and time
Date and time
Date #显示当前日期 and prompt for a new date, press \ "Enter \" to skip the input
date/t #只显示当前日期, do not prompt to enter a new date
Time #显示当前时间, and prompt for new times, press \ "Enter \" to skip input
time/t #只显示当前时间, do not prompt to enter a new time
Goto and:
Jump command
: Label #行首为: Indicates that the row is a label row, and the label row does not perform an action
Goto Label #跳转到指定的标签那一行
Find (External command)
Find command
Find \ "Abc\" C:\test.txt
Find lines with an ABC string in the C:\test.txt file
If not found, the ERRORLEVEL return code is set to 1
find/i "ABC" C:\test.txt
Find rows with ABC, ignoring case
FIND/C \ "Abc\" C:\test.txt
Displays the number of rows with ABC
More (External command)
Per-screen display
More C:\test.txt #逐屏显示 c:\test.txt file contents
Tree
Show directory Structure
Tree D:\ #显示D盘的文件目录结构
&
Executes multiple commands sequentially, regardless of whether or not the command is executed successfully
&&
Executes multiple commands sequentially and does not execute subsequent commands when a command that performs an error is encountered
Find \ "Ok\" C:\test.txt && Echo Success
If the word "ok\" is found, it displays \ "Success \" and does not appear if it is not found.
24 | |
Executes multiple commands sequentially and does not execute subsequent commands when the correct command is run
Find \ "Ok\" C:\test.txt | | Echo Not successful
If the word "ok\" is not found, it will display \ "unsuccessful \" and it will not be displayed
25 |
Pipeline command
Dir * */s/a | FIND/C \ ". exe\"
The pipe command indicates that the DIR command is executed first, and the result of its output executes the following find command
The command line results: output the number of. exe files in the current folder and all subfolders
Type C:\test.txt|more
The effect of this and more c:\test.txt is the same
> >>
Output REDIRECT Command
> Erase the contents of the file before writing
>> append content to the end of the file without erasing the original content
Output the content that would otherwise appear on the screen to the specified file
Specifies that the file is automatically generated if it does not exist
Type C:\test.txt >PRN
File contents are not displayed on the screen, turn output to printer
echo Hello World>con
The Hello world is displayed on the screen, and virtually all output is the default >con
Copy c:\test.txt F: >nul
Copy the file and do not display \ "File copy succeeded \" message, but if the F drive does not exist, an error message will be displayed
Copy c:\test.txt f: >nul 2>nul
Does not display the "File copy succeeded" message, and the F disk does not exist, also does not display the error message echo ^^ W ^> ^w>c:\test.txt
The resulting file content is ^w > W
^ and > are control commands, to output them to a file, you must add a ^ symbol in front
<
Get input from a file, not from the screen
Commands that are typically used for a date time label, such as waiting to be entered
@echo off
Echo 2005-05-01>temp.txt
Date <temp.txt
Del Temp.txt
This allows you to modify the current date without waiting for input
28%0%1%2%3%4%5%6%7%8%9%*
Parameters passed to the batch by the command line
%0 The batch file itself
The first parameter of%1
%9 nineth parameter
%* all parameters starting from the first argument
The substitution of the batch parameter (%n) has been enhanced. You can use the following syntax:
%~1-delete quotation marks (\ "), expand%1
%~F1-Expanded%1 to a fully qualified pathname
%~D1-expand%1 to only one drive letter
%~P1-expand%1 to only one path
%~N1-expands%1 to a file name only
%~X1-Expanded%1 to only one file name extension
%~S1-extended path refers to a short name
%~A1-Extend%1 to file properties
%~T1-date/time when%1 was expanded to a file
%~Z1-Expansion of%1 to file size
%~ $PATH: 1-finds the directory that is listed in the PATH environment variable and adds%1
Expands to the first fully qualified name found. If the environment
The variable name is not defined, or the file is not found, this key combination will
Extend to an empty string
Modifier can be combined to achieve multiple results:
%~DP1-Extend%1 to drive letter and path only
%~NX1-Extension%1 to file name and extension only
%~dp$path:1-Look for% 1 in the directory listed in the PATH environment variable
and expands to the drive letter and path of the first file found.
%~FTZA1-Extended%1 to an output line similar to DIR.
Can you refer to call/? or for/? See the meaning of each parameter
echo load \ "%%1\" \ "%%2\" >c:\test.txt
The resulting file content is load \ "%1\" \ "%2\"
Batch file, use this format to output command line arguments to a file
If
Judging commands
If \ "%1\" ==\ "/a\" echo the first parameter is a/a
if/i \ "%1\" equ \ "/a\" echo the first parameter is/a
/I means case insensitive, equ and = = are the same, other operators see if/?
If exist C:\test.bat echo exists C:\test.bat file
If not exist C:\Windows (
Echo does not exist C:\Windows folder
)
If exist C:\test.bat (Echo exists c:\test.bat) Else (Echo does not exist C:\test.bat)
Setlocal and Endlocal
Setting command extensions and delaying environment variable expansion
SETLOCAL enableextensions #启用 \ "command extension \"
SETLOCAL disableextensions #停用 \ "command extension \"
SETLOCAL enabledelayedexpansion #启用 \ "Delay environment variable expansion \"
SETLOCAL disabledelayedexpansion #停用 \ "Delay environment variable expansion \"
Endlocal #恢复到使用SETLOCAL语句以前的状态
Command extension is enabled by default
"Delay environment variable expansion" is disabled by default
The system automatically restores the default value after the batch process ends
You can modify the registry to disable \ "command extension \", see CMD/?. So the use of the \ "command extension \" process
SETLOCAL Enableextensions and Endlocal statements at the beginning and the end to ensure
The program can operate correctly on other systems
\ "Delay environment variable expansion \" is mainly used for if and for the compliance statement, in the description of the set is actually a use case process
Set
Setting variables
Reference variables can be added before and after the variable name, i.e.% variable name%
Set #显示目前所有可用的变量, including system variables and custom variables
Echo%systemdrive% #显示系统盘盘符. System variables can be referenced directly
Set P #显示所有以p开头的变量, if not a errorlevel=1
Set P=aa1bb1aa2bb2 #设置变量p, and assigns a value = the string after it, i.e. AA1BB1AA2BB2
echo%p% #显示变量p代表的字符串, i.e. AA1BB1AA2BB2
echo%p:~6% #显示变量p中第6个字符以后的所有字符, i.e. AA2BB2
echo%p:~6,3% #显示第6个字符以后的3个字符, i.e. AA2
echo%p:~0,3% #显示前3个字符, i.e. Aa1
echo%p:~-2% #显示最后面的2个字符, i.e. B2
echo%p:~0,-2% #显示除了最后2个字符以外的其它字符, i.e. aa1bb1aa2b
Echo%p:aa=c% #用c替换变量p中所有的aa, which shows C1BB1C2BB2
Echo%p:aa=% #将变量p中的所有aa字符串置换为空, which shows 1BB12BB2
Echo%p:*bb=c% #第一个bb及其之前的所有字符被替换为c, which shows C1AA2BB2
Set p=%p:*bb=c% #设置变量p, assigned to%p:*bb=c%, i.e. C1AA2BB2
set/a p=39 #设置p为数值型变量 with a value of 39
set/a P=39/10 #支持运算符, there are decimals with the tail method, 39/10=3.9, to the tail 3,p=3
set/a P=P/10 #用/A parameter, the variable after = can be directly referenced without a%
set/a p= "1&0″#" and "operation, add quotation marks." Other supported operators see set/?
Set p= #取消p变量
set/p p= Please enter
"Please enter" is displayed on the screen, and the input string is assigned to the variable p
Note that this can be used to replace the choice command.
Note that the variable is replaced in the compound statement of the IF and for all at once, as
@echo off
Set P=AAA
If%P%==AAA (
Echo%p%
Set P=BBB
Echo%p%
)
The results are displayed
Aaa
Aaa
Because all%p% are replaced with AAA when the IF statement is read
Here's \ "replace \", IN/? Help means \ "expand \", \ "Environment variable expansion \"
can enable "Delay environment variable expansion", with! To reference a variable, that is! variable name!
@echo off
SETLOCAL enabledelayedexpansion
Set P=AAA
If%P%==AAA (
Echo%p%
Set P=BBB
Echo!p!
)
Endlocal
The results are displayed
Aaa
Bbb
There are also several dynamic variables that run the set and cannot see
%cd% #代表当前目录的字符串
%date% #当前日期
%time% #当前时间
%random% #随机整数, between 0~32767
%errorlevel% #当前 ERRORLEVEL Value
%cmdextversion% #当前命令处理器扩展名版本号
%cmdcmdline% #调用命令处理器的原始命令行
You can use the echo command to view each variable value, such as Echo%time%
Note that the%time% is accurate to milliseconds and can be used when batch processing requires deferred processing
Start
commands from external programs are called in batches, otherwise the remaining instructions are not executed until the external program finishes
Call
Another batch command is called in the batch, otherwise the remaining batch instructions will not be executed
Sometimes some applications use start to invoke an error or call
Choice (external command)
Select command
Let the user enter a character, thus choosing to run different commands, return code ERRORLEVEL is 1234 ...
Win98 is choice.com.
Win2000pro, you can cuff it from the Win98.
Win2003 is Choice.exe.
choice/n/C y/t 5/d Y>nul
5 Seconds delay
Assoc and Ftype
File associations
Assoc Set ' File name extension ' Association to ' file type '
Ftype set ' file Type ' association to ' execute program and Parameters '
When you double-click a. txt file, Windows is not directly judged by the. txt to open with Notepad.exe
Instead, you first determine that. txt belongs to txtfile ' file type '
Call the txtfile associated command-line Txtfile=%systemroot%\system32\notepad again. EXE%1
You can modify these 2 associations in the \ "Folder Options \" →\ "File type \"
Assoc #显示所有 ' File Extension ' Association
Assoc. txt #显示. txt represents the ' file type ' and results are displayed. Txt=txtfile
Assoc. Doc #显示. doc represents the ' file type ', and the results are displayed. Doc=word.document.8
Assoc. exe #显示. exe represents the ' file type ' and results are displayed. Exe=exefile
Ftype #显示所有 ' File Type ' association
Ftype Exefile #显示exefile类型关联的命令行, results show exefile=\ "%1\"%*
Assoc. txt=word.document.8
You can see that the icon for a. txt file changes when you set the. txt to a Word type document.
Assoc. txt=txtfile
Restoring the correct association of. txt
Ftype exefile=\ "%1\"%*
Restore the correct association of Exefile
If the association has been corrupted, you can run Command.com, and then enter this command
Pushd and POPD
Toggle current Directory
@echo off
C: & cd\ & MD mp3 #在 \ C Create a MP3 folder
MD d:\mp4 #在 D:\ Create a MP4 folder
cd/d D:\mp4 #更改当前目录为 D:\mp4
pushd C:\mp3 #保存当前目录, and switch the current directory to C:\mp3
POPD #恢复当前目录为刚才保存的 D:\mp4
PNS for
Looping commands
This is more complicated, please control for/? View
For%%i in (c:d: e:f:) do echo%%i
Call each string in parentheses, and execute the command following the Do
Note%%i that the FOR statement call parameter in a batch is 2%
The default string delimiter is \ "SPACEBAR \", \ "tab \", \ "enter key \"
For%%i in (*.txt) do find \ "Abc\"%%i
Execute the Find command for all txt files in the current directory
For/r. %%i in (*.txt) do find \ "Abc\"%%i
Searches all. txt files in the current directory and subdirectories for lines containing the ABC string
For/r. %%i in (.) Do echo%%~PNI
Displays the current directory name and all subdirectory names, including the path, not including the drive letter
FOR/R D:\mp3%%i in (*.mp3) do echo%%i>>d:\mp3.txt
Save the filename of the mp3 file in the D:\mp3 and its subdirectories in D:\mp3.txt.
FOR/L%%i in (2,1,8) do echo%%i
Generate 2345678 of a string of numbers, 2 is the beginning of the sequence of numbers, 8 is the end, 1 means that each add 1
for/f%%i in (' Set ') do echo%%i
Loop calls to the output of the set command, one per line
for/f \ "Eol=p\"%%i in (' Set ') do echo%%i
Take the output of the set command, ignoring the lines that begin with P
for/f%%i in (d:\mp3.txt) do echo%%i
Displays each file name in the D:\mp3.txt, one per line, and does not support names with spaces
for/f \ "Delims=\"%%i in (d:\mp3.txt) do echo%%i
Displays each file name in the D:\mp3.txt, one per line, supporting names with spaces
for/f \ "Skip=5 tokens=4\"%%a in (' dir ') do echo%%a
For the result of the dir command, skip the previous 5 lines, and the remaining rows take the 4th column
The delimiter between each column is the default \ "Space \"
Note that the first 5 lines of the dir command output do not have a file name
for/f \ "tokens=1,2,3 delims=-\"%%a in (' date/t ') do (
Echo%%a
Echo%%b
Echo%%c
)
1, 2, 3 columns per row for the output of the date/t
The first column corresponds to the specified%%a, and the subsequent%%b and%%c are derived, corresponding to the other columns
The delimiter is specified as-and \ "Space \", note that there is a \ "space \" behind delims=-
where tokens=1,2,3 Replace 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 the subsequent columns to%%b
Subst (external command)
Mapped disks.
Subst Z: \server\d #这样输入z: You can access \server\d.
Subst Z:/d #取消该映射
Subst #显示目前所有的映时
* 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:\, overwriting existing files
Plus/I indicates if e:\ Create a new one automatically without the MP3 folder, or you will be asked
BAT Command Daquan