### 1 echo and @Echo Control Commands
@ # Turn off single-line echoing
echo off # Turn off echoing from the next line
@echo off # Turn off echoing from this line. Usually the first line of a batch file is this
echo on # Turn on echoing from the next line
echo # Display whether current state is echo off or echo on
echo. # Output a "carriage return line feed", generally refers to a blank line
echo hello world # Output hello world
"Turning off echoing" means that when running a batch file, it does not display each command in the file, only the running results. The system automatically turns on echoing at the beginning and end of the batch processing.
### 2 errorlevel Program Return Code
echo %errorlevel% Each command ends, you can use this command line format to view the return code
Used to judge whether the previous command was executed successfully
Default value is 0, generally if a command execution fails, errorlevel will be set to 1
### 3 dir Display File and Subdirectory List in Directory
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 directories in the current directory of drive C
dir c:\ /a:-d # Display files in the root directory of drive C
dir d:\mp3 /b/p # Display files in the d:\mp3 directory one screen at a time, only display file names, not time and size
dir *.exe /s Display all .exe files in the current directory and subdirectories
Where * is a wildcard, representing all file names, and another wildcard ? represents one arbitrary letter or Chinese character
Such as c*.* represents all files starting with c
?.exe represents all .exe files with a file name of one letter
If the specified directory or file does not exist, it will return errorlevel 1
Each folder's dir output will have 2 subdirectories . and ..., representing the current directory
.. represents the parent directory of the current directory
dir . # Display files and subdirectories in the current directory
dir .. # Display files and subdirectories in the parent directory of the current directory
Other parameters can be referred to dir /?
### 4 cd Change Current Directory
cd mp3 # Enter the mp3 directory in the current directory
cd .. # Enter the parent directory in the current directory
cd\ # Enter the root directory
cd # Display the current directory
cd /d d:\mp3 # Can change both the drive letter and the directory at the same time
cd "Documents and Settings"\All users If the file name has spaces, it is recommended to add quotes. Sometimes not adding quotes may cause errors, such as in login scripts
If the directory to be changed does not exist, an error will occur and return errorlevel=1
### 5 md Create Directory
md abc # Create a subdirectory abc in the current directory
md d:\a\b\c # If d:\a does not exist, it will be created automatically
### 6 rd Delete Directory
rd abc # Delete the abc subdirectory in the current directory, requires it to be an empty directory
rd /s/q d:\temp # Delete the d:\temp folder and its subfolders and files, no need to press Y to confirm
### 7 del Delete File
del d:\test.txt # Delete the specified file, cannot be a hidden, system, or read-only file
del *.* Delete all files in the current directory, not including hidden, system, or read-only files, requires pressing Y to confirm
del /q/a/f d:\temp\*.* Delete all files in the d:\temp folder, including hidden, read-only, system files, not including subdirectories
del /q/a/f/s d:\temp\*.* Delete all files in d:\temp and subfolders, including hidden, read-only, system files, not including subdirectories
### 8 ren Rename File
ren 1.txt 2.bak # Rename 1.txt to 2.bak
ren *.txt *.ini # Rename all .txt files in the current directory to .ini files
ren d:\temp tmp # Supports renaming folders
### 9 cls Clear Screen
### 10 type Display File Content
type c:\boot.ini # Display the content of the specified file, program files generally display garbled characters
type *.txt # Display the content of all .txt files in the current directory
### 11 copy Copy File
copy c:\test.txt d:\ Copy the c:\test.txt file to d:\
copy c:\test.txt d:\test.bak Copy the c:\test.txt file to d:\ and rename it to test.bak
copy c:\*.* Copy all files in c:\ to the current directory, not including hidden files and system files
If the target path is not specified, the default target path is the current directory
copy con test.txt Wait for input from the screen, press Ctrl+Z to end input, and the input content is saved as test.txt file
con represents the screen, prn represents the printer, nul represents the empty device
copy 1.txt + 2.txt 3.txt Merge the contents of 1.txt and 2.txt and save it as 3.txt file
If 3.txt is not specified, it is saved to 1.txt
copy test.txt + Copy the file to itself, actually modifies the file date
### 12 title Set the Title of the cmd Window
title New Title # You can see that the title bar of the cmd window has changed
### 13 ver Display System Version
### 14 label and vol Set Volume Label
vol # Display volume label
label # Display volume label and prompt to enter a new volume label
label c:system # Set the volume label of drive C to system
### 15 pause Pause Command
When running this command, the following message will be displayed: Press any key to continue . . .
Generally used to see the content displayed on the screen clearly
### 16 rem and :: Comment Command
Comment lines do not perform operations
### 17 date and time Date and Time
date # Display current date and prompt to enter a new date, press "Enter" to skip input
date/t # Only display current date, do not prompt to enter new date
time # Display current time and prompt to enter new time, press "Enter" to skip input
time/t # Only display current time, do not prompt to enter new time
### 18 goto and : Jump Command
:label # A line starting with : indicates that this line is a label line, and label lines do not perform operations
goto label # Jump to the specified label line
### 19 find (External Command) Find Command
find "abc" c:\test.txt Find lines containing the string abc in the c:\test.txt file
If not found, it will set errorlevel return code to 1
find /i "abc" c:\test.txt Find lines containing abc, ignoring case
find /c "abc" c:\test.txt Display the number of lines containing abc
### 20 more (External Command) Display Screen by Screen
more c:\test.txt # Display the content of c:\test.txt file screen by screen
### 21 tree Display Directory Structure
tree d:\ # Display the file directory structure of drive D
### 22 & Execute Multiple Commands in Sequence, Regardless of Whether the Command is Executed Successfully
c: & cd\ & dir /w Equivalent to writing the following 3 lines of commands into one line
c:
cd\
dir /w
### 23 && Execute Multiple Commands in Sequence, and Do Not Execute Subsequent Commands When Encountering a Command That Fails to Execute
f: && cd\ && dir >c:\test.txt
Note: If drive F does not exist, then the following 2 commands will not be executed
find "ok" c:\test.txt && echo Success If the word "ok" is found, display "Success", otherwise do not display
### 24 || Execute Multiple Commands in Sequence, and Do Not Execute Subsequent Commands When Encountering a Command That Executes Correctly
f: || e: If there is drive F, do not enter drive E
find "ok" c:\test.txt || echo Not successful If the word "ok" is not found, display "Not successful", if found, do not display
### 25 | Pipe Command
The execution result of the previous command is output to the next command
dir *.* /s/a | find /c ".exe"
The pipe command means to first execute the dir command, and execute the subsequent find command on its output result
The result of this command line: output the number of .exe files in the current folder and all subfolders
type c:\test.txt|more This has the same effect as more c:\test.txt
### 26 > and >> Output Redirection Command
> Clear the original content in the file and then write
>> Append content to the end of the file without clearing the original content
Mainly output the content originally displayed on the screen to the specified file
If the specified file does not exist, it will be automatically generated
echo hello world>c:\test.txt Generate the c:\test.txt file with the content hello world
This format is used a lot in batch files, and can generate temporary files such as .reg .bat .vbs
type c:\test.txt >prn Do not display the file content on the screen, redirect to output to the printer
echo hello world>con Display hello world on the screen, in fact, all output is default >con
copy c:\test.txt f: >nul Copy the file and do not display the prompt "File copied successfully", but if drive F does not exist, the error message will still be displayed
copy c:\test.txt f: >nul 2>nul Do not display the prompt "File copied successfully", and if drive F does not exist, do not display the error prompt
echo ^^W ^> ^W>c:\test.txt The content of the generated file is ^W > W^. The > is a control command. To output them to the file, you must add a ^ in front
### 27 < Get Input Information from the File Instead of from the Screen
Generally used for commands that need to wait for input such as date time label
@echo off
echo 2005-05-01>temp.txt
date <temp.txt
del temp.txt
In this way, the current date can be modified without waiting for input
### 28 %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 %* Parameters Passed to the Batch File from the Command Line
%0 Batch file itself
%1 First parameter
%9 Ninth parameter
%* All parameters starting from the first parameter
Create test.bat in the root directory of drive C with the following content:
@echo off
echo %0
echo %1
echo %2
echo %*
Run cmd and enter c:\test.bat "/a" /b /c /d to see the meaning of each parameter
Modify the content of test.bat as follows
@echo off
echo %1
echo %~1
echo %0
echo %~f0
echo %~d0
echo %~p0
echo %~n0
echo %~x0
echo %~s0
echo %~a0
echo %~t0
echo %~z0
Run cmd again and enter c:\test.bat "/a" /b /c /d. You can refer to call/? or for/? to see the meaning of each parameter. Note that date comparison and size comparison can be performed on files here
echo load "%%1" "%%2">c:\test.txt The content of the generated file is load "%1" "%2"
In the batch file, use this format to output command line parameters to the file
### 29 if Judgment Command
if "%1"=="/a" echo The first parameter is /a
if /i "%1" equ "/a" echo The first parameter is /a
/i means case-insensitive, equ is the same as ==, other operators are referred to if/?
if exist c:\test.bat echo There is a c:\test.bat file
if not exist c:\windows (
echo There is no c:\windows folder
rem Multiple commands can be enclosed in parentheses, called "compound statement"
rem The spaces in front of the line are for clarity
)
if exist c:\test.bat (
echo There is a c:\test.bat
) else (
echo There is no c:\test.bat
rem else means "otherwise", execute the subsequent command when the judgment result is false
)
### 30 setlocal and endlocal Set "Command Extensions" and "Delayed Environment Variable Expansion"
SETLOCAL ENABLEEXTENSIONS # Enable "command extensions"
SETLOCAL DISABLEEXTENSIONS # Disable "command extensions"
SETLOCAL ENABLEDELAYEDEXPANSION # Enable "delayed environment variable expansion"
SETLOCAL DISABLEDELAYEDEXPANSION # Disable "delayed environment variable expansion"
ENDLOCAL # Restore to the state before using the SETLOCAL statement
"Command extensions" are enabled by default
"Delayed environment variable expansion" is disabled by default
The system will automatically restore the default value at the end of the batch processing
The "command extensions" can be disabled by modifying the registry, see cmd /? for details. So for programs that use "command extensions", it is recommended to add SETLOCAL ENABLEEXTENSIONS and ENDLOCAL statements at the beginning and end to ensure that the program can run correctly on other systems
"Delayed environment variable expansion" is mainly used in compound statements of if and for, and there are practical examples in the description of set
### 31 set Set Variable
To reference a variable, you can add % before and after the variable name, that is %variable name%
set # Display all currently available variables, including system variables and custom ones
echo %SystemDrive% # Display the system drive letter. System variables can be directly referenced
set p # Display all variables starting with p, if none, set errorlevel=1
set p=aa1bb1aa2bb2 # Set variable p and assign the string after =, that is aa1bb1aa2bb2
echo %p% # Display the string represented by variable p, that is aa1bb1aa2bb2
echo %p:~6% # Display all characters after the 6th character in variable p, that is aa2bb2
echo %p:~6,3% # Display 3 characters after the 6th character, that is aa2
echo %p:~0,3% # Display the first 3 characters, that is aa1
echo %p:~-2% # Display the last 2 characters, that is b2
echo %p:~0,-2% # Display all characters except the last 2 characters, that is aa1bb1aa2b
echo %p:aa=c% # Replace all aa in variable p with c, that is display c1bb1c2bb2
echo %p:aa=% # Replace all aa strings in variable p with empty, that is display 1bb12bb2
echo %p:*bb=c% # Replace all characters before the first bb and the first bb with c, that is display c1aa2bb2
set p=%p:*bb=c% # Set variable p and assign the value of %p:*bb=c%, that is c1aa2bb2
set /a p=39 # Set p as a numeric variable with a value of 39
set /a p=39/10 # Support operators, use the truncation method for decimals, 39/10=3.9, truncate to 3, p=3
set /a p=p/10 # When using the /a parameter, the variable after = can be directly referenced without adding %
set /a p="1&0" #"And" operation, need to add quotes. Other supported operators are referred to set/?
set p= # Cancel variable p
set /p p=Please enter Display "Please enter" on the screen and assign the input string to variable p
Note that this can be used to replace the choice command
Note that variables are all replaced at one time in the compound statements of if and for. For example:
@echo off
set p=aaa
if %p%==aaa (
echo %p%
set p=bbb
echo %p%
)
The result will display aaaaaa because all %p% have been replaced with aaa when reading the if statement
The "replacement" here is referred to as "expansion" and "environment variable expansion" in /? help. You can enable "delayed environment variable expansion" and use ! 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 will display aaabbb
There are also several dynamic variables, which cannot be seen by running set
%CD% # String representing the current directory
%DATE% # Current date
%TIME% # Current time
%RANDOM% # Random integer, between 0~32767
%ERRORLEVEL% # Current ERRORLEVEL value
%CMDEXTVERSION% # Current command processor extension version number
%CMDCMDLINE% # Original command line that called 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, and can be used for delay processing in batch processing
### 32 start Command to Call External Programs in Batch Processing, Otherwise Continue to Execute the remaining instructions after the external program is completed
start explorer d:\ Call the graphical interface to open drive D
@echo off
cd /d %~dp0
regedit /s 劲舞团.reg
start patcher.exe
Without the start command, when "劲舞团" runs, there will be a black cmd window behind
### 33 call Command to Call Another Batch File in Batch Processing, Otherwise the remaining batch processing instructions will not be executed
Sometimes some applications call incorrectly with start, and can also be called with call
### 34 choice (External Command) Choice Command
Let the user enter a character to choose to run different commands, the return code errorlevel is 1 2 3 4...
In win98 it is choice.com
In win2000pro it is not available, you can copy it from win98
In win2003 it is choice.exe
choice /N /C y /T 5 /D y>nul Delay for 5 seconds
The following is an example of a choice statement
@echo off
rem The following runs through in win2000pro, the chioce.com file copied from win98
choice /c:abc aaa,bbb,ccc
if errorlevel 3 goto ccc
if %errorlevel%==2 goto bbb
if errorlevel==1 goto aaa
rem Must judge the return code of higher values first
rem You can see that there are 3 ways to judge the errorlevel value. Sometimes one way is not easy to use, you can use another way
rem Directly running chioce is equivalent to running choice /c:yn
:aaa
echo aaa
goto end
:bbb
echo bbb
goto end
:ccc
echo ccc
goto end
:end
### 35 assoc and ftype File Association
assoc Set 'file extension' association, associated with 'file type'
ftype Set 'file type' association, associated with 'executable program and parameters'
When you double-click a .txt file, windows does not directly judge to open it with notepad.exe according to .txt
But first judge that .txt belongs to the txtfile 'file type' and then call the command line txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1 associated with txtfile
You can modify these 2 associations in "Folder Options"→"File Types"
assoc # Display all 'file extension' associations
assoc .txt # Display the 'file type' represented by .txt, the result shows .txt=txtfile
assoc .doc # Display the 'file type' represented by .doc, the result shows .doc=Word.Document.8
assoc .exe # Display the 'file type' represented by .exe, 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 .txt as a word type document, you can see that the icon of the .txt file has changed
assoc .txt=txtfile Restore the correct association of .txt
ftype exefile="%1" %* Restore the correct association of exefile
If this association is damaged, you can run command.com and then enter this command
### 36 pushd and popd Switch 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 d:\mp4 # Change the current directory to d:\mp4
pushd c:\mp3 # Save the current directory and switch the current directory to c:\mp3
popd # Restore the current directory to the previously saved d:\mp4
Generally not very useful, it will be a little helpful when the current directory name is uncertain
### 37 for Loop Command
This is relatively complex, please refer to for/?
for %%i in (c: d: e: f:) do echo %%i Call each string in the parentheses in turn and execute the command after do
Note %%i, in the batch processing, the for statement calls parameters with 2%
The default string delimiter is "space bar", "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 string abc in all .txt files in the current directory and subdirectories
for /r . %%i in (.) do echo %%~pni Display 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 file names of mp3 files in d:\mp3 and its subdirectories to d:\mp3.txt
for /l %%i in (2,1,8) do echo %%i Generate a series of numbers 2345678, 2 is the start of the number sequence, 8 is the end, 1 means add 1 each time
for /f %%i in ('set') do echo %%i Loop and call the output result of the set command, one line at a time
for /f "eol=P" %%i in ('set') do echo %%i Take the output result of the set command and ignore the lines starting with P
for /f %%i in (d:\mp3.txt) do echo %%i Display each file name in d:\mp3.txt, one line at a time, does not support names with spaces
for /f "delims=" %%i in (d:\mp3.txt) do echo %%i Display each file name in d:\mp3.txt, one line at a time, supports names with spaces
for /f "skip=5 tokens=4" %%a in ('dir') do echo %%a For the result of the dir command, skip the first 5 lines, and take the 4th column of the remaining lines
The delimiter between columns is the default "space"
You can notice that the first 5 lines of the dir command output have no file names
for /f "tokens=1,2,3 delims=- " %%a in ('date /t') do (
echo %%a
echo %%b
echo %%c
)
For the output result of date /t, take columns 1, 2, 3 of each line
The first column corresponds to the specified %%a, and the subsequent %%b and %%c are derived, corresponding to other columns
The delimiter is specified as - and "space", note that there is a "space" after delims=-
Among them, tokens=1,2,3 can be replaced with tokens=1-3, and the effect is the same
for /f "tokens=2* delims=- " %%a in ('date /t') do echo %%b Take the 2nd column for %%a, and all subsequent columns for %%b
### 32 subst (External Command) Map Disk.
subst z: \\server\d # In this way, entering z: can access \\server\d
subst z: /d # Cancel this mapping
subst # Display all current mappings
### 38 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:\, overwrite existing files
Add /i means if e:\ does not have an mp3 folder, it will be created automatically, otherwise there will be a query
### 39 Some Less Commonly Used Internal Commands
>& Write the output of one handle to the input of another handle
<& Read input from one handle and write it to the output of another handle
shift When there are more than 9 parameters passed to the batch file from the command line, use it to switch parameters
color Set the display color of the cmd window
pormpt Change the command prompt symbol, the default is all drive letter:\path\>, such as c:\>
### 40 format (External Command) Format Hard Disk
format c: /q/u/autotest
/q means quick format, /autotest means automatic format, no need to press Y to confirm
/u means overwrite the hard disk data with F6 per byte, making it unavailable for software recovery
format c: /c Format drive C and detect bad sectors
### 41 fdisk (External Command) Hard Disk Partition
win2000 does not bring this command
fdisk in win98 does not support large hard disks above 80G, fdisk in winme supports
fdisk/mbr Rebuild the hard disk partition table, generally used to clear the boot sector virus and restore the spirit
Note that when using this command, you cannot boot from the hard disk, you must start directly from the floppy disk or CD-ROM
### 42 ping (External Command)
ping -l 65500 -t 192.168.1.200 Continuously send packets with a size of 65500byte to the computer 192.168.1.200
ping -n 10 127.0.0.1>nul Ping yourself 10 times, which can be used for batch processing delay of 10 seconds
### 43 SC (External Command) Service Control Command
sc create aaa displayname= bbb start= auto binpath= "C:\WINDOWS\System32\alg.exe"
Create a service, service name aaa, display name bbb, startup type: automatic
Path of executable file "C:\WINDOWS\System32\alg.exe"
sc description aaa "ccc" Change the description of aaa to ccc
sc config aaa start= disabled binpath= "C:\WINDOWS\System32\svchost.exe -k netsvcs"
Change the startup type of aaa: disabled
Change the path of the executable file of aaa "C:\WINDOWS\System32\svchost.exe -k netsvcs"
sc config aaa start= demand displayname= ddd
Change the startup type of aaa: manual
Change the display name of aaa ddd
sc start aaa Start the aaa service
sc stop aaa Stop the aaa service
sc delete aaa Delete the aaa service
@ # Turn off single-line echoing
echo off # Turn off echoing from the next line
@echo off # Turn off echoing from this line. Usually the first line of a batch file is this
echo on # Turn on echoing from the next line
echo # Display whether current state is echo off or echo on
echo. # Output a "carriage return line feed", generally refers to a blank line
echo hello world # Output hello world
"Turning off echoing" means that when running a batch file, it does not display each command in the file, only the running results. The system automatically turns on echoing at the beginning and end of the batch processing.
### 2 errorlevel Program Return Code
echo %errorlevel% Each command ends, you can use this command line format to view the return code
Used to judge whether the previous command was executed successfully
Default value is 0, generally if a command execution fails, errorlevel will be set to 1
### 3 dir Display File and Subdirectory List in Directory
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 directories in the current directory of drive C
dir c:\ /a:-d # Display files in the root directory of drive C
dir d:\mp3 /b/p # Display files in the d:\mp3 directory one screen at a time, only display file names, not time and size
dir *.exe /s Display all .exe files in the current directory and subdirectories
Where * is a wildcard, representing all file names, and another wildcard ? represents one arbitrary letter or Chinese character
Such as c*.* represents all files starting with c
?.exe represents all .exe files with a file name of one letter
If the specified directory or file does not exist, it will return errorlevel 1
Each folder's dir output will have 2 subdirectories . and ..., representing the current directory
.. represents the parent directory of the current directory
dir . # Display files and subdirectories in the current directory
dir .. # Display files and subdirectories in the parent directory of the current directory
Other parameters can be referred to dir /?
### 4 cd Change Current Directory
cd mp3 # Enter the mp3 directory in the current directory
cd .. # Enter the parent directory in the current directory
cd\ # Enter the root directory
cd # Display the current directory
cd /d d:\mp3 # Can change both the drive letter and the directory at the same time
cd "Documents and Settings"\All users If the file name has spaces, it is recommended to add quotes. Sometimes not adding quotes may cause errors, such as in login scripts
If the directory to be changed does not exist, an error will occur and return errorlevel=1
### 5 md Create Directory
md abc # Create a subdirectory abc in the current directory
md d:\a\b\c # If d:\a does not exist, it will be created automatically
### 6 rd Delete Directory
rd abc # Delete the abc subdirectory in the current directory, requires it to be an empty directory
rd /s/q d:\temp # Delete the d:\temp folder and its subfolders and files, no need to press Y to confirm
### 7 del Delete File
del d:\test.txt # Delete the specified file, cannot be a hidden, system, or read-only file
del *.* Delete all files in the current directory, not including hidden, system, or read-only files, requires pressing Y to confirm
del /q/a/f d:\temp\*.* Delete all files in the d:\temp folder, including hidden, read-only, system files, not including subdirectories
del /q/a/f/s d:\temp\*.* Delete all files in d:\temp and subfolders, including hidden, read-only, system files, not including subdirectories
### 8 ren Rename File
ren 1.txt 2.bak # Rename 1.txt to 2.bak
ren *.txt *.ini # Rename all .txt files in the current directory to .ini files
ren d:\temp tmp # Supports renaming folders
### 9 cls Clear Screen
### 10 type Display File Content
type c:\boot.ini # Display the content of the specified file, program files generally display garbled characters
type *.txt # Display the content of all .txt files in the current directory
### 11 copy Copy File
copy c:\test.txt d:\ Copy the c:\test.txt file to d:\
copy c:\test.txt d:\test.bak Copy the c:\test.txt file to d:\ and rename it to test.bak
copy c:\*.* Copy all files in c:\ to the current directory, not including hidden files and system files
If the target path is not specified, the default target path is the current directory
copy con test.txt Wait for input from the screen, press Ctrl+Z to end input, and the input content is saved as test.txt file
con represents the screen, prn represents the printer, nul represents the empty device
copy 1.txt + 2.txt 3.txt Merge the contents of 1.txt and 2.txt and save it as 3.txt file
If 3.txt is not specified, it is saved to 1.txt
copy test.txt + Copy the file to itself, actually modifies the file date
### 12 title Set the Title of the cmd Window
title New Title # You can see that the title bar of the cmd window has changed
### 13 ver Display System Version
### 14 label and vol Set Volume Label
vol # Display volume label
label # Display volume label and prompt to enter a new volume label
label c:system # Set the volume label of drive C to system
### 15 pause Pause Command
When running this command, the following message will be displayed: Press any key to continue . . .
Generally used to see the content displayed on the screen clearly
### 16 rem and :: Comment Command
Comment lines do not perform operations
### 17 date and time Date and Time
date # Display current date and prompt to enter a new date, press "Enter" to skip input
date/t # Only display current date, do not prompt to enter new date
time # Display current time and prompt to enter new time, press "Enter" to skip input
time/t # Only display current time, do not prompt to enter new time
### 18 goto and : Jump Command
:label # A line starting with : indicates that this line is a label line, and label lines do not perform operations
goto label # Jump to the specified label line
### 19 find (External Command) Find Command
find "abc" c:\test.txt Find lines containing the string abc in the c:\test.txt file
If not found, it will set errorlevel return code to 1
find /i "abc" c:\test.txt Find lines containing abc, ignoring case
find /c "abc" c:\test.txt Display the number of lines containing abc
### 20 more (External Command) Display Screen by Screen
more c:\test.txt # Display the content of c:\test.txt file screen by screen
### 21 tree Display Directory Structure
tree d:\ # Display the file directory structure of drive D
### 22 & Execute Multiple Commands in Sequence, Regardless of Whether the Command is Executed Successfully
c: & cd\ & dir /w Equivalent to writing the following 3 lines of commands into one line
c:
cd\
dir /w
### 23 && Execute Multiple Commands in Sequence, and Do Not Execute Subsequent Commands When Encountering a Command That Fails to Execute
f: && cd\ && dir >c:\test.txt
Note: If drive F does not exist, then the following 2 commands will not be executed
find "ok" c:\test.txt && echo Success If the word "ok" is found, display "Success", otherwise do not display
### 24 || Execute Multiple Commands in Sequence, and Do Not Execute Subsequent Commands When Encountering a Command That Executes Correctly
f: || e: If there is drive F, do not enter drive E
find "ok" c:\test.txt || echo Not successful If the word "ok" is not found, display "Not successful", if found, do not display
### 25 | Pipe Command
The execution result of the previous command is output to the next command
dir *.* /s/a | find /c ".exe"
The pipe command means to first execute the dir command, and execute the subsequent find command on its output result
The result of this command line: output the number of .exe files in the current folder and all subfolders
type c:\test.txt|more This has the same effect as more c:\test.txt
### 26 > and >> Output Redirection Command
> Clear the original content in the file and then write
>> Append content to the end of the file without clearing the original content
Mainly output the content originally displayed on the screen to the specified file
If the specified file does not exist, it will be automatically generated
echo hello world>c:\test.txt Generate the c:\test.txt file with the content hello world
This format is used a lot in batch files, and can generate temporary files such as .reg .bat .vbs
type c:\test.txt >prn Do not display the file content on the screen, redirect to output to the printer
echo hello world>con Display hello world on the screen, in fact, all output is default >con
copy c:\test.txt f: >nul Copy the file and do not display the prompt "File copied successfully", but if drive F does not exist, the error message will still be displayed
copy c:\test.txt f: >nul 2>nul Do not display the prompt "File copied successfully", and if drive F does not exist, do not display the error prompt
echo ^^W ^> ^W>c:\test.txt The content of the generated file is ^W > W^. The > is a control command. To output them to the file, you must add a ^ in front
### 27 < Get Input Information from the File Instead of from the Screen
Generally used for commands that need to wait for input such as date time label
@echo off
echo 2005-05-01>temp.txt
date <temp.txt
del temp.txt
In this way, the current date can be modified without waiting for input
### 28 %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 %* Parameters Passed to the Batch File from the Command Line
%0 Batch file itself
%1 First parameter
%9 Ninth parameter
%* All parameters starting from the first parameter
Create test.bat in the root directory of drive C with the following content:
@echo off
echo %0
echo %1
echo %2
echo %*
Run cmd and enter c:\test.bat "/a" /b /c /d to see the meaning of each parameter
Modify the content of test.bat as follows
@echo off
echo %1
echo %~1
echo %0
echo %~f0
echo %~d0
echo %~p0
echo %~n0
echo %~x0
echo %~s0
echo %~a0
echo %~t0
echo %~z0
Run cmd again and enter c:\test.bat "/a" /b /c /d. You can refer to call/? or for/? to see the meaning of each parameter. Note that date comparison and size comparison can be performed on files here
echo load "%%1" "%%2">c:\test.txt The content of the generated file is load "%1" "%2"
In the batch file, use this format to output command line parameters to the file
### 29 if Judgment Command
if "%1"=="/a" echo The first parameter is /a
if /i "%1" equ "/a" echo The first parameter is /a
/i means case-insensitive, equ is the same as ==, other operators are referred to if/?
if exist c:\test.bat echo There is a c:\test.bat file
if not exist c:\windows (
echo There is no c:\windows folder
rem Multiple commands can be enclosed in parentheses, called "compound statement"
rem The spaces in front of the line are for clarity
)
if exist c:\test.bat (
echo There is a c:\test.bat
) else (
echo There is no c:\test.bat
rem else means "otherwise", execute the subsequent command when the judgment result is false
)
### 30 setlocal and endlocal Set "Command Extensions" and "Delayed Environment Variable Expansion"
SETLOCAL ENABLEEXTENSIONS # Enable "command extensions"
SETLOCAL DISABLEEXTENSIONS # Disable "command extensions"
SETLOCAL ENABLEDELAYEDEXPANSION # Enable "delayed environment variable expansion"
SETLOCAL DISABLEDELAYEDEXPANSION # Disable "delayed environment variable expansion"
ENDLOCAL # Restore to the state before using the SETLOCAL statement
"Command extensions" are enabled by default
"Delayed environment variable expansion" is disabled by default
The system will automatically restore the default value at the end of the batch processing
The "command extensions" can be disabled by modifying the registry, see cmd /? for details. So for programs that use "command extensions", it is recommended to add SETLOCAL ENABLEEXTENSIONS and ENDLOCAL statements at the beginning and end to ensure that the program can run correctly on other systems
"Delayed environment variable expansion" is mainly used in compound statements of if and for, and there are practical examples in the description of set
### 31 set Set Variable
To reference a variable, you can add % before and after the variable name, that is %variable name%
set # Display all currently available variables, including system variables and custom ones
echo %SystemDrive% # Display the system drive letter. System variables can be directly referenced
set p # Display all variables starting with p, if none, set errorlevel=1
set p=aa1bb1aa2bb2 # Set variable p and assign the string after =, that is aa1bb1aa2bb2
echo %p% # Display the string represented by variable p, that is aa1bb1aa2bb2
echo %p:~6% # Display all characters after the 6th character in variable p, that is aa2bb2
echo %p:~6,3% # Display 3 characters after the 6th character, that is aa2
echo %p:~0,3% # Display the first 3 characters, that is aa1
echo %p:~-2% # Display the last 2 characters, that is b2
echo %p:~0,-2% # Display all characters except the last 2 characters, that is aa1bb1aa2b
echo %p:aa=c% # Replace all aa in variable p with c, that is display c1bb1c2bb2
echo %p:aa=% # Replace all aa strings in variable p with empty, that is display 1bb12bb2
echo %p:*bb=c% # Replace all characters before the first bb and the first bb with c, that is display c1aa2bb2
set p=%p:*bb=c% # Set variable p and assign the value of %p:*bb=c%, that is c1aa2bb2
set /a p=39 # Set p as a numeric variable with a value of 39
set /a p=39/10 # Support operators, use the truncation method for decimals, 39/10=3.9, truncate to 3, p=3
set /a p=p/10 # When using the /a parameter, the variable after = can be directly referenced without adding %
set /a p="1&0" #"And" operation, need to add quotes. Other supported operators are referred to set/?
set p= # Cancel variable p
set /p p=Please enter Display "Please enter" on the screen and assign the input string to variable p
Note that this can be used to replace the choice command
Note that variables are all replaced at one time in the compound statements of if and for. For example:
@echo off
set p=aaa
if %p%==aaa (
echo %p%
set p=bbb
echo %p%
)
The result will display aaaaaa because all %p% have been replaced with aaa when reading the if statement
The "replacement" here is referred to as "expansion" and "environment variable expansion" in /? help. You can enable "delayed environment variable expansion" and use ! 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 will display aaabbb
There are also several dynamic variables, which cannot be seen by running set
%CD% # String representing the current directory
%DATE% # Current date
%TIME% # Current time
%RANDOM% # Random integer, between 0~32767
%ERRORLEVEL% # Current ERRORLEVEL value
%CMDEXTVERSION% # Current command processor extension version number
%CMDCMDLINE% # Original command line that called 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, and can be used for delay processing in batch processing
### 32 start Command to Call External Programs in Batch Processing, Otherwise Continue to Execute the remaining instructions after the external program is completed
start explorer d:\ Call the graphical interface to open drive D
@echo off
cd /d %~dp0
regedit /s 劲舞团.reg
start patcher.exe
Without the start command, when "劲舞团" runs, there will be a black cmd window behind
### 33 call Command to Call Another Batch File in Batch Processing, Otherwise the remaining batch processing instructions will not be executed
Sometimes some applications call incorrectly with start, and can also be called with call
### 34 choice (External Command) Choice Command
Let the user enter a character to choose to run different commands, the return code errorlevel is 1 2 3 4...
In win98 it is choice.com
In win2000pro it is not available, you can copy it from win98
In win2003 it is choice.exe
choice /N /C y /T 5 /D y>nul Delay for 5 seconds
The following is an example of a choice statement
@echo off
rem The following runs through in win2000pro, the chioce.com file copied from win98
choice /c:abc aaa,bbb,ccc
if errorlevel 3 goto ccc
if %errorlevel%==2 goto bbb
if errorlevel==1 goto aaa
rem Must judge the return code of higher values first
rem You can see that there are 3 ways to judge the errorlevel value. Sometimes one way is not easy to use, you can use another way
rem Directly running chioce is equivalent to running choice /c:yn
:aaa
echo aaa
goto end
:bbb
echo bbb
goto end
:ccc
echo ccc
goto end
:end
### 35 assoc and ftype File Association
assoc Set 'file extension' association, associated with 'file type'
ftype Set 'file type' association, associated with 'executable program and parameters'
When you double-click a .txt file, windows does not directly judge to open it with notepad.exe according to .txt
But first judge that .txt belongs to the txtfile 'file type' and then call the command line txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1 associated with txtfile
You can modify these 2 associations in "Folder Options"→"File Types"
assoc # Display all 'file extension' associations
assoc .txt # Display the 'file type' represented by .txt, the result shows .txt=txtfile
assoc .doc # Display the 'file type' represented by .doc, the result shows .doc=Word.Document.8
assoc .exe # Display the 'file type' represented by .exe, 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 .txt as a word type document, you can see that the icon of the .txt file has changed
assoc .txt=txtfile Restore the correct association of .txt
ftype exefile="%1" %* Restore the correct association of exefile
If this association is damaged, you can run command.com and then enter this command
### 36 pushd and popd Switch 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 d:\mp4 # Change the current directory to d:\mp4
pushd c:\mp3 # Save the current directory and switch the current directory to c:\mp3
popd # Restore the current directory to the previously saved d:\mp4
Generally not very useful, it will be a little helpful when the current directory name is uncertain
### 37 for Loop Command
This is relatively complex, please refer to for/?
for %%i in (c: d: e: f:) do echo %%i Call each string in the parentheses in turn and execute the command after do
Note %%i, in the batch processing, the for statement calls parameters with 2%
The default string delimiter is "space bar", "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 string abc in all .txt files in the current directory and subdirectories
for /r . %%i in (.) do echo %%~pni Display 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 file names of mp3 files in d:\mp3 and its subdirectories to d:\mp3.txt
for /l %%i in (2,1,8) do echo %%i Generate a series of numbers 2345678, 2 is the start of the number sequence, 8 is the end, 1 means add 1 each time
for /f %%i in ('set') do echo %%i Loop and call the output result of the set command, one line at a time
for /f "eol=P" %%i in ('set') do echo %%i Take the output result of the set command and ignore the lines starting with P
for /f %%i in (d:\mp3.txt) do echo %%i Display each file name in d:\mp3.txt, one line at a time, does not support names with spaces
for /f "delims=" %%i in (d:\mp3.txt) do echo %%i Display each file name in d:\mp3.txt, one line at a time, supports names with spaces
for /f "skip=5 tokens=4" %%a in ('dir') do echo %%a For the result of the dir command, skip the first 5 lines, and take the 4th column of the remaining lines
The delimiter between columns is the default "space"
You can notice that the first 5 lines of the dir command output have no file names
for /f "tokens=1,2,3 delims=- " %%a in ('date /t') do (
echo %%a
echo %%b
echo %%c
)
For the output result of date /t, take columns 1, 2, 3 of each line
The first column corresponds to the specified %%a, and the subsequent %%b and %%c are derived, corresponding to other columns
The delimiter is specified as - and "space", note that there is a "space" after delims=-
Among them, tokens=1,2,3 can be replaced with tokens=1-3, and the effect is the same
for /f "tokens=2* delims=- " %%a in ('date /t') do echo %%b Take the 2nd column for %%a, and all subsequent columns for %%b
### 32 subst (External Command) Map Disk.
subst z: \\server\d # In this way, entering z: can access \\server\d
subst z: /d # Cancel this mapping
subst # Display all current mappings
### 38 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:\, overwrite existing files
Add /i means if e:\ does not have an mp3 folder, it will be created automatically, otherwise there will be a query
### 39 Some Less Commonly Used Internal Commands
>& Write the output of one handle to the input of another handle
<& Read input from one handle and write it to the output of another handle
shift When there are more than 9 parameters passed to the batch file from the command line, use it to switch parameters
color Set the display color of the cmd window
pormpt Change the command prompt symbol, the default is all drive letter:\path\>, such as c:\>
### 40 format (External Command) Format Hard Disk
format c: /q/u/autotest
/q means quick format, /autotest means automatic format, no need to press Y to confirm
/u means overwrite the hard disk data with F6 per byte, making it unavailable for software recovery
format c: /c Format drive C and detect bad sectors
### 41 fdisk (External Command) Hard Disk Partition
win2000 does not bring this command
fdisk in win98 does not support large hard disks above 80G, fdisk in winme supports
fdisk/mbr Rebuild the hard disk partition table, generally used to clear the boot sector virus and restore the spirit
Note that when using this command, you cannot boot from the hard disk, you must start directly from the floppy disk or CD-ROM
### 42 ping (External Command)
ping -l 65500 -t 192.168.1.200 Continuously send packets with a size of 65500byte to the computer 192.168.1.200
ping -n 10 127.0.0.1>nul Ping yourself 10 times, which can be used for batch processing delay of 10 seconds
### 43 SC (External Command) Service Control Command
sc create aaa displayname= bbb start= auto binpath= "C:\WINDOWS\System32\alg.exe"
Create a service, service name aaa, display name bbb, startup type: automatic
Path of executable file "C:\WINDOWS\System32\alg.exe"
sc description aaa "ccc" Change the description of aaa to ccc
sc config aaa start= disabled binpath= "C:\WINDOWS\System32\svchost.exe -k netsvcs"
Change the startup type of aaa: disabled
Change the path of the executable file of aaa "C:\WINDOWS\System32\svchost.exe -k netsvcs"
sc config aaa start= demand displayname= ddd
Change the startup type of aaa: manual
Change the display name of aaa ddd
sc start aaa Start the aaa service
sc stop aaa Stop the aaa service
sc delete aaa Delete the aaa service
Recent Ratings for This Post
( 4 in total)
Click for details
| Rater | Score | Time |
|---|---|---|
| 523066680 | +1 | 2008-02-16 11:56 |
| xmi | +1 | 2008-02-21 20:41 |
| fengjing001 | +1 | 2008-02-24 23:15 |
| 414893029 | +1 | 2008-03-27 05:12 |
