Go Summary of common commands for Windows batch processing (Cmd/bat)

Source: Internet
Author: User
Tags clear screen echo command file copy ftp file goto ftp file transfer


Common DOS Command folder management
    • The CD displays the current directory name or changes the current directory.
    • MD to create a directory.
    • RD Deletes a directory.
    • DIR displays a list of files and subdirectories in the directory.
    • Tree graphically displays the folder structure of a drive or path.
    • Path displays or sets a search path for the executable file.
    • xcopy copies files and directory trees.
File Management
    • Type Displays the contents of the text file.
    • Copy copies one or more copies of the file to another location.
    • Del deletes one or several files.
    • Move moves the file and renames the file and directory. (Not in Windows XP Home Edition)
    • ren renames the file.
    • Replace replaces the file.
    • attrib Display or change file properties.
    • Find search string.
    • FC compares two files or two sets of files and displays the differences between them
Network commands
    • Ping for network connection testing, name resolution
    • FTP File Transfer
    • NET network command set and user management
    • Telnet remote Login
    • ipconfig displaying and modifying TCP/IP settings
    • MSG sends a message to the user
    • ARP display, modify the IP address of the local area network-Physical Address mapping list
System Management
    • At schedule commands and programs to run on a specific date and time
    • Shutdown immediate or timed shutdown or restart
    • Tskill End Process
    • Taskkill End Process (higher than tskill, but no command in version WinXPHome)
    • Tasklist Show List of processes (not in Windows XP Home Edition)
    • SC system service Setup and control
    • Reg Registry Console Tool
    • POWERCFG power settings on the control system


For all the commands listed above, enter the command +/in CMD to see the Help for this command. Like Find/?


Windows Batch Common Commands
1 echo and @
Echo command
@ # Turn off single line echo
echo off #Echo off from the next line
@echo off #Echo off from this line. Generally the first line of a batch is this
echo on #Echo from the next line
echo #Display the current echo off state or echo on state
echo. # Output a "carriage return", blank line
                         # (Same as echo, echo; echo + echo [echo] echo / echo)

2 errorlevel
echo% errorlevel%
The end of each command run, you can view the return code in this command line format
The default value is 0, and errorlevel is set to 1 for general command execution errors.

3 dir
Show folder contents
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 directories in the current directory of the C drive
dir c: / a: -d #Display files in the root directory of drive C
dir c: / b / p # / b display only the file name, / p display it in pages
dir * .exe / s #Display all .exe files in the current directory and subdirectories

4 cd
Change directory
cd #Enter the root directory
cd #Display the current directory
cd / d d: sdk #Can change drive letter and directory at the same time

5 md
Create a directory
md d: abc #If d: a does not exist, an intermediate directory will be created automatically
#If the command extension is disabled, you need to type mkdir abc.

6 rd
Delete directory
rd abc #Delete the abc subdirectory in the current directory, requiring an empty directory
rd / s / q d: temp #Delete the d: temp folder and its subfolders and files, / q quiet mode

7 del
Delete Files
del d: test.txt #Delete the specified file, it cannot be a hidden, system, read-only file
del / q / a / f d: temp *. *
Delete all files in the d: temp folder, including hidden, read-only, system files, excluding subdirectories
del / q / a / f / s d: temp *. *
Delete all files in d: temp and subfolders, including hidden, read-only, system files, excluding subdirectories

8 ren
Rename command
ren d: temp tmp #Supports renaming of folders

9 cls
Clear screen

10 type
Display file contents
type c: boot.ini #Display the contents of the specified file, the program file will usually display garbled characters
type * .txt #Display the contents of all .txt files in the current directory

11 copy
Copy file
copy c: test.txt d: test.bak
Copy the c: test.txt file to d: and rename it to test.bak
copy con test.txt
Wait for input from the screen, press Ctrl + Z to end the input, and save the input as test.txt
con stands for screen, prn stands for printer, nul stands for empty device
copy 1.txt + 2.txt 3.txt
Combine the contents of 1.txt and 2.txt and save as 3.txt file
If 3.txt is not specified, it is saved to 1.txt
copy test.txt +
Copying the file to yourself is actually modifying 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
Show system version

14 label and vol
Set the volume label
vol #show volume label
label #Show the volume label, and prompt for a new volume label
label c: system #Set the volume label of drive C to system

15 pause
Pause command

16 rem and ::
Comment command
Comment line does nothing

17 date and time
Date and time
date #Display the current date and prompt for a new date, press "Enter" to skip input
date / t #Display only the current date, without prompting for a new date
time #Display the current time, and prompt for a new time, press "Enter" to skip input
time / t #Display only the current time, without prompting for a new time

18 goto and:
Jump command
: label #The beginning of the line is: the line is a label line
goto label #Go to the line with the specified label

19 find (external command)
Find command
find "abc" c: test.txt
Find the line with the abc string in the c: test.txt file
If not found, set errorlevel return code to 1
find / i “abc” c: test.txt
Find lines with abc, ignore case
find / c "abc" c: test.txt
Display the number of rows with abc

20 more (external command)
Display screen by screen
more c: test.txt #Display the content of c: test.txt file by screen

21 tree
Show directory structure
tree d: #Display the file directory structure of drive D

twenty two &
Execute multiple commands in sequence, regardless of whether the command is successfully executed

twenty three &&
Execute multiple commands in sequence. After encountering an execution error command, the subsequent commands will not be executed.
find "ok" c: test.txt && echo successful
If "ok" is found, "success" is displayed, if not, it is not displayed

24 ||
Execute multiple commands in sequence. When the correct command is encountered, subsequent commands will not be executed.
find "ok" c: test.txt || echo is unsuccessful
If the word "ok" is not found, it will show "unsuccessful", if it is not found,

25 |
Pipe command
dir *. * / s / a | find / c ".exe"
The pipe command means that the dir command is executed first, and the subsequent find command is executed on the results output
The command line results: 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 contents of the file before writing
>> 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 generated automatically
type c: test.txt> prn
The file content is not displayed on the screen, and the output is diverted to the printer
echo hello world> con
Show hello world on the screen, in fact all output is the default> con
copy c: test.txt f:> nul
Copy the file without displaying the message "File copied successfully", but if the f drive does not exist, an error message will still be displayed
copy c: test.txt f:> nul 2> nul
The prompt message "File copied successfully" is not displayed, and if the f drive does not exist, an error prompt message is not displayed
echo ^^ W ^> ^ W> c: test.txt
The generated file content is ^ W> W
^ And> are control commands. To output them to a file, you must precede them with a ^ symbol.

27 <
Get input from a file, not 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
This way you can modify the current date without waiting for input

28% 0% 1% 2% 3% 4% 5% 6% 7% 8% 9% *
Parameters passed to the batch from the command line
% 0 batch file itself
% 1 first parameter
% 9 ninth parameter
% * All arguments from the first argument

Substitution of batch parameters (% n) has been enhanced. You can use the following syntax:

     % ~ 1-Remove quotes (") and expand% 1
     % ~ f1-expand% 1 to a fully qualified path name
     % ~ d1-Expand% 1 to only one drive letter
     % ~ p1-expand% 1 to only one path
     % ~ n1-expand% 1 to a file name only
     % ~ x1-only expand% 1 to a file extension
     % ~ s1-extended path means short name
     % ~ a1-expand% 1 to file attributes
     % ~ t1-expands% 1 to the date / time of the file
     % ~ z1-Expand% 1 to the size of the file
     % ~ $ PATH: 1-finds the directory listed in the PATH environment variable and sets% 1
                   Expand to the first fully qualified name found. If the environment
                   The variable name is not defined, or no file is found, this key combination will
                   Expand to empty string

You can combine modifiers to get multiple results:

    % ~ dp1-only expand% 1 to drive letter and path
    % ~ nx1-only expand% 1 to filename and extension
    % ~ dp $ PATH: 1-finds% 1 in the directories listed in the PATH environment variable,
                   And expand to the drive letter and path of the first file found.
    % ~ ftza1-Expand% 1 to a DIR-like output line.
You can refer to call /? Or for /? To see the meaning of each parameter
echo load "%% 1" "%% 2"> c: test.txt
Generated file content is load "% 1" "% 2"
In a batch file, use this format to output command line parameters to a 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 and == are the same, for other operators see if /?
if exist c: test.bat echo c: test.bat file
if not exist c: windows (
     echo does not exist in the c: windows folder
     )
if exist c: test.bat (
     echo exists in c: test.bat
     ) else (
     echo does not exist c: test.bat
     )

30 setlocal and endlocal
Set "command extension" and "stay environment variable expansion"
SETLOCAL ENABLEEXTENSIONS #Enable "command extensions"
SETLOCAL DISABLEEXTENSIONS #Disable "command extensions"
SETLOCAL ENABLEDELAYEDEXPANSION #Enable "Delay environment variable expansion"
SETLOCAL DISABLEDELAYEDEXPANSION #Disable "Delay environment variable expansion"
ENDLOCAL #Restore to the state before using the SETLOCAL statement
Command Extension is enabled by default
"Delayed environment variable expansion" is disabled by default
The system will automatically restore the default value after the batch processing
You can modify the registry to disable "command extensions", see cmd /? For details. So the procedure using "command extension"
Order, it is recommended to add SETLOCAL ENABLEEXTENSIONS and ENDLOCAL statements at the beginning and end to ensure
Guarantee Runs correctly on other systems
"Delayed environment variable expansion" is mainly used in the if and for conformance statements, and has its practical routines in the description of set

31 set
Set variable
Reference variables can be preceded by%, that is,% variable name%
set #Display all currently available variables, including system variables and custom variables
echo% SystemDrive% #Display the system drive letter. System variables can be directly referenced
set p #Display all variables starting with p, if there is none, set errorlevel = 1
set p = aa1bb1aa2bb2 #Set the variable p and assign the value to the string after =, ie aa1bb1aa2bb2
echo% p% #Display the string represented by the variable p, that is, aa1bb1aa2bb2
echo% p: ~ 6% #Display all characters after the 6th character in the variable p, that is, aa2bb2
echo% p: ~ 6,3% #Display the 3 characters after the 6th character, that is, aa2
echo% p: ~ 0,3% #Display the first 3 characters, namely aa1
echo% p: ~ -2% #Display the last 2 characters, which is b2
echo% p: ~ 0, -2% #Display characters other than the last 2 characters, namely aa1bb1aa2b
echo% p: aa = c% #Replace all aa in the variable p with c, which shows c1bb1c2bb2
echo% p: aa =% #Replace all aa strings in the variable p with empty, that is, 1bb12bb2
echo% p: * bb = c% #the first bb and all characters before it are replaced with c, which shows c1aa2bb2
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 the operator, use the tail removal method when there is a decimal, 39/10 = 3.9, the tail is 3, p = 3
set / a p = p / 10 #When using the / a parameter, the variables after = can be directly referenced without adding%
set / a p = ”1 & 0 ″ #” AND ”operation, quotes. For other supported operators, see set /?
set p = #Cancel p variable
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 instead of the choice command
Note that variables are replaced all at once in if and for compound statements, such as
@echo off
set p = aaa
if% p% == aaa (
     echo% p%
     set p = bbb
     echo% p%
     )
The results will be displayed
aaa
aaa
Because all% p% have been replaced with aaa when reading the if statement
"Replace" here means "expand" and "environment variable expansion" in /? Help
You can enable "slow environment variable expansion" and use! To refer to variables, that is,! Variable names!
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set p = aaa
if% p% == aaa (
     echo% p%
     set p = bbb
     echo! p!
     )
ENDLOCAL
The results will be displayed
aaa
bbb
There are also several dynamic variables that you can't see when running set
% CD% #A 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% #Invoke 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 and can be used when batch processing requires delayed processing

32 start
Commands that call external programs in batches, or wait for external programs to complete before executing the remaining instructions

33 call
Invoking another batch command in the batch, otherwise the remaining batch instructions will not be executed
Sometimes some applications use start call to make a mistake.

34 choice (external command)
Select command
Let the user enter a character and choose to run different commands with a return code of error level 1234 ...
win98 is choice.com
No in win2000pro, you can copy it from win98
win2003 is choice.exe
choice / N / C y / T 5 / D y> nul
5 seconds delay

35 assoc and ftype
File association
assoc set ‘file extension’ association to ‘file type’
ftype sets the ‘file type’ association to the ‘executor and parameters’
When you double-click a .txt file, windows is not directly judged by .txt and opened with notepad.exe
But first determine that .txt belongs to txtfile ‘file type’
Then call the command line associated with txtfile txtfile =% SystemRoot% system32NOTEPAD.EXE% 1
You can modify these two associations in "Folder Options" → "File Type"
assoc #Show all ‘file extension’ associations
assoc .txt #Display the ‘file type’ represented by .txt, the result is .txt = txtfile
assoc .doc #Show the ‘file type’ represented by .doc, the result is .doc = Word.Document.8
assoc .exe #Display the ‘file type’ that .exe represents, and the result shows .exe = exefile
ftype #Show all ‘file type’ associations
ftype exefile #Display the command line associated with exefile type, the result shows exefile = "% 1"% *
assoc .txt = Word.Document.8
Set .txt to a word document, 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

36 pushd and popd
Change the current directory
@echo off
c: & cd & md mp3 #in C: create mp3 folder
md d: mp4 #in D: create mp4 folder
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 d: mp4 just saved

37 for
Loop command
This is more complicated, please look at for /?
for %% i in (c: d: e: f :) do echo %% i
Call each string in parentheses one after the other and execute the command after do
Note the %% i, 2% for the for statement call parameter in the batch
The default string separators are "spacebar", "Tab", "Enter"
for %% i in (* .txt) do find "abc" %% i
Find command on all txt files in the current directory
for / r. %% i in (* .txt) do find "abc" %% i
Search all .txt files in the current directory and subdirectories for lines containing the abc string
for / r. %% i in (.) do echo %% ~ pni
Display the current directory name and all subdirectory names, including the path, excluding drive letters
for / r d: mp3 %% i in (* .mp3) do echo %% i >> d: mp3.txt
Save the file names of d: mp3 and mp3 files in its subdirectories to d: mp3.txt
for / l %% i in (2,1,8) do echo %% i
Generate a string of numbers 2345678, 2 is the beginning of the sequence of numbers, 8 is the end, 1 means add 1 each time
for / f %% i in (‘set’) do echo %% i
Call the output of the set command cyclically, one per line
for / f "eol = P" %% i in (‘set’) do echo %% i
Take the output of the set command and ignore the lines that start with P
for / f %% i in (d: mp3.txt) do echo %% i
Display each file name in d: mp3.txt, one per line, names with spaces are not supported
for / f "delims =" %% i in (d: mp3.txt) do echo %% i
Display each file name in d: mp3.txt, one per line, 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 fourth column for each remaining line
The separator between each column is the default "space"
Notice that the first 5 lines of the dir command have no filename.
for / f "tokens = 1,2,3 delims =-" %% a in (‘date / t’) do (
     echo %% a
     echo %% b
     echo %% c
     )
For date / t output, take 1, 2, and 3 columns per line
The first column corresponds to the specified %% a, the following %% b and %% c are derived and correspond to the other columns
The delimiter is specified as-and "space", note that there is a "space" after delims =-
Where tokens = 1,2,3 if replaced with tokens = 1-3, the effect is the same
for / f "tokens = 2 * delims =-" %% a in (‘date / t’) do echo %% b
Take the second column to %% a and all subsequent columns to %% b

38 subst (external command)
Map the disk.
subst z: serverd #So enter z: to access serverd
subst z: / d #Cancel the mapping
subst #Show all current time

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:, overwriting existing files
Adding / i means that if e: does not have an mp3 folder, a new one will be created automatically, otherwise you will be asked


[]windows Batch processing (cmd/bat) Common Command summary


Related Article

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.