Windows batch (cmd / bat) commonly used commands to learn

Foreword

Batch file (batch file) contains a series of DOS commands, typically used to automate repetitive tasks. Users simply double-click the batch file can perform tasks without having to repeatedly enter the same command. Write a batch file is very simple, but the difficulty is to make sure everything is executed sequentially. Write rigorous batch file can save time great extent, it is particularly effective when dealing with repetitive work.

In Windows, make good use of the batch can simplify a lot of duplication of work

What is a batch

Batch (Batch), also known as a batch script. As the name suggests, the batch is to batch processing of an object. Extended batch file called bat.

Currently the more common batch contains two types: DOS Batch and PS batch. PS is based on the powerful batch image editing software Photoshop, the script for batch processing images; and DOS batch is used to automatically execute DOS batch commands to implement specific actions based on the DOS command scripts. The real message here is the DOS batch.

Batch is a simplified scripting language used in DOS and Windows systems, it was built by DOS or Windows system command interpreter (usually COMMAND.COM or CMD.EXE) explain to run. Shell script similar to the Unix. Batch file with a .bat or .cmd extension, the simplest example, is written line by line in the command line will be used in various commands. More complex cases, need to use if, for, goto commands control the operation of the program is, like C, Basic and other senior language. If you want a more complex application, it is necessary to use an external program, which includes tools or third-party software and external commands provided by the system itself provides.

Batch file, or batch program is called, is a plain text file consists of a section of the DOS commands which can be directly edited or created with this DOS command with a notebook, you can also use a text editor to edit Edit.exe under DOS . Type the name of the batch file in the "command prompt" under, or double-click the batch file, the system will call Cmd.exe to run the batch program. In general, each command occupies one line; course be multiple commands with a particular symbol (e.g.,: &, &&, |, ||, etc.) after the partition is written in the same row; there is the case where the image if, for and other more advanced commands will have to occupy a few lines or even tens of hundreds of lines of space.

In explaining the system to run a batch program, the first program to scan the entire batch, and then begin stepping down from the first line of code execute all commands until the end of the program or meet the exit command or error quit unexpectedly.

About batch command

echo
rem
pause
call
start
goto
set
http://xstarcd.github.io/wiki/windows/windows_cmd_summary_commands.html

Batch symbol Introduction

@ Echo shield
redirects 1> and >>
redirection 2 <
pipe symbols |
escape ^
logical operator command comprising: &, &&, ||
http://xstarcd.github.io/wiki/windows/windows_cmd_summary_symbols.html

Common DOS command

Folder Management

cd displays the current directory or changes the current directory.
md Creates a directory.
rd delete a directory.
dir displays a list of files and subdirectories in the directory.
Display driver file tree folder structure or path graphically.
path for the executable file shows or sets a search path.
xcopy copy files and directory trees.
Document Management

type display the contents of a text file.
The copy Copies one or more files to another location.
del Deletes one or more files.
move Moves files and renames files and directories. (Windows XP Home Edition does not)
REN to rename the file.
replace replace file.
attrib Displays or changes file attributes.
find the search string.
fc compare two files or two set of files and displays the differences therebetween
network commands

ping network connection test, name resolution
ftp file transfer
net user management and network command set
telnet remote login
ipconfig display, modify, TCP / IP settings
msg send a message to the user
arp display, modify, LAN IP address - physical address mapping list
System Management

at scheduled to run commands and programs to a specific date and time
shutdown immediate or timed shutdown or restart
tskill end of the process
taskkill end of the process (ratio tskill senior, but WinXPHome version without the command)
tasklist to display the process list (Windows XP Home Edition does not)
sc the system service settings and control
reg console registry tool
power settings on powercfg control system
for all commands listed above, enter the command cmd + /? to see help information for the command. Such as find /?

Windows Batch common commands

 

@ 1 echo and 
echo the command 
@ # closed single-line echo 
echo off # the next line begins to close echo 
@echo off # echo from the Bank began to close. The first line of the batch generally 
echo on # Open from the beginning of the next line echo 
echo # Display the current state is still echo off echo on the state of 
echo. # Output a "carriage return", a blank line 
                         # (with echo, echo ; echo + echo [echo] echo / echo) 

2 errorlevel 
echo% errorlevel% 
each operation end command, the command line format can be used to view the return code 
default value is 0, the command execution error will generally errorlevel is provided. 1 

. 3 the dir 
display folders SUMMARY 
dir # display the current directory files and subdirectories 
dir / a # display the current directory files and subdirectories, including hidden and system files 
dir c: / a: d # display C drive current directory in the directory 
dir c: / a: -d # to display the file in the root directory of C
dir c: / b / p # / b to display only the file name, / p pagination 
dir * .exe / s # display the current directory and all subdirectories .exe files 

4 cd 
Change directory 
cd # to enter the root directory 
cd # Display the current directory 
cd / dd: sdk # can change both the letter and directory 

5 md 
create a directory 
md d: abc # If the d: a does not exist, it will automatically create intermediate directories 
# If command extensions are disabled, you need to type mkdir abc . 

6 rd 
delete the directory 
rd abc # delete the current directory in a subdirectory of abc, requires an empty directory 
rd / s / qd: temp # delete d: temp folder and its subfolders and files, / q Quiet mode 

7 del 
delete files 
del d: test.txt # delete a file, can not be hidden, system, read-only file 
del /q/a/fd:temp*.* 
delete d: temp folder all the files inside, including hidden, read-only, system file, not including subdirectories 
del /q/a/f/sd:temp*.* 
delete d: temp files and subfolders inside of all, including hidden, read-only, system file, not including subdirectories 

8 ren 
rename command 
ren d: temp tmp # support for renaming a folder

9 cls 
clear the screen 

10 type 
displays the file content 
type c: boot.ini # display the contents of the specified files, program files typically garbled 
type * .txt # show the contents of the current directory of all .txt files 

11 copy 
copy files 
copy c: test.txt d: test.bak 
copy c: test.txt file to d:, and renamed test.bak 
copy CON test.txt 
wait for input from the screen, press Ctrl + Z end of the input, the input contents as test.txt file 
con behalf screen, prn representing the printer, nul on behalf of the null device 
copy 1.txt + 2.txt 3.txt 
merge 1.txt and 2.txt content, save the file as 3.txt 
If you do not 3.txt specified, save to 1.txt 
copy test.txt + 
copy files to yourself, in fact, changed the file date 

12 title 
set cmd window title 
title can see the new title # cmd window title bar has changed 

13 ver 
display system version 

14 label and Vol  
set the volume label
Vol # display label
tag label # display, prompts for a new label while the 
label c: system # C drive is a volume label System 

15 PAUSE 
Pause command 

16 rem and :: 
annotation command 
comment line operation is not performed 

17 date and time, 
the date and time 
date # It displays the current date, and prompt for a new date, press the "enter" to skip the input 
date / t # only displays the current date, without prompting for a new date 
time # display the current time, and you are prompted to enter a new time, press the "enter" slightly by entering 
time / t # only displays the current time, without prompting for a new time 

18 goto and: 
Jump command 
: label # for the beginning of the line: Indicates that the line is the tag line, tag line does not perform an operation 
goto label # jump to a specified label the line 

19 find (external command) 
Find command 
find "abc" c: test.txt 
in c: Find the line containing the string abc test.txt file 
if found, will be located errorlevel return code of 1  
Find / I "ABC" C: test.txt
find with abc lines, ignoring the case 
find / c "abc" c: test.txt 
the number of lines of rows containing abc

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

21 tree 
displays the directory structure 
tree d: # D displays file directory structure of the disc 

22 & 
execute multiple command sequence , regardless of whether the command is executed successfully 

23 && 
order to perform multiple commands, when met after the command execution error does not execute subsequent commands 
find "ok" c: test.txt && echo successfully 
if found "ok" word, you "success", can not find no display 

24 || 
execute multiple commands sequence, when met after the correct command execution behind the command does 
find "ok" c: test.txt || echo unsuccessful 
if looking for less than "ok" words, on the show "unsuccessful", found not display 

25 | 
pipe command 
dir * * / s / a | . find / c ".exe" 
pipe command represents the first implementation of the dir command, its output the result of executing the find command later in 
the command result: output current folder and all subfolders inside the .exe file number 
type c: test.txt | more 
of this and more c: test.txt effect is the same 

26> and >> 
output redirection command 
> Clear file and then write any original content 
>> append to the end of the file, but does not clear the contents of the original 
principal will have been content displayed on the screen output to the specified file in 
the specified file if you do not there is, then automatically generate the file 
type c: test.txt> prn 
does not display the contents of a file on the screen, steering output to a printer 
echo hello world> con 
displayed on the screen hello world, virtually all of the output is the default> con's 
copy c : test.txt f:> nul 
copy files, and does not display the message "file copy successful", but if f disk does not exist, or will display an error message 
copy c: test.txt f:> nul 2> nul 
does not show "file copy successful" message, and f disk does not exist, do not display an error message 
echo ^^ W ^> ^ W> c: test.txt 
contents of the file generated for W ^> W 
^ and> control command, take them to a file, must be preceded by a caret 

27 < 
take input from a file, rather than from the screen 
is generally a command and the like to wait date time label input 
@echo OFF 
echo 2005-05- 01> temp.txt 
DATE <temp.txt 
de l temp.

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

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

     % ~ 1 - Remove the quotation mark ( "), the expansion of% 1 
     % ~ f1 - expands% 1 to a fully qualified path name 
     % ~ d1 - only expands% 1 to a drive letter 
     % ~ p1 - only expands% 1 to a path 
     % ~ n1 - only expands% 1 to a file name 
     % ~ x1 - only expands% 1 to a file extension 
     % ~ s1 - expanded path contains short names 
     % ~ a1 - expands% 1 to file attributes 
     % ~ t1 - expands% 1 to date / time of the file 
     % ~ z1 - expands% 1 to size of file 
     % ~ $ PATH: 1 - searches the directories listed in the PATH environment variable, and 1% 
                   expansion to the first fully qualified name found. If the environment 
                   variable name is not defined or the file is not found, this key combination will
                   Expanded to an empty string 

modifiers can be combined to get compound results: 

    % ~ DP1 - only% 1 extended to a drive letter and path 
    % ~ nx1 - only% one extension to the file name and extension 
    % ~ dp $ PATH: 1 - find% 1 in the directory listed in the pATH environment variable in, 
                   and expands to the drive letter and path of the first file found. 
    % ~ ftza1 - 1% will extend the DIR like output line. 
Can refer to call / or for / seen meaning of each parameter?? 
Echo Load "%%. 1" "%% 2"> C: test.txt 
content file is generated load "% 1" "% 2 " 
Batch file with this format command line parameters to the output file 

29 if 
determination command 
if "% 1" == "/ a" echo is the first parameter / a 
IF / I ". 1%" EQU "/ a" of echo is a parameter / a 
/ I represents a case insensitive, and EQU == is the same, other operators see IF /? 
IF exist c: test.bat echo exists c: test.bat file 
if not exist c: windows ( 
     echo c does not exist:
     echo exists c: test.bat
     ) The else ( 
     echo c does not exist: test.bat 
     ) 

30 setlocal and endlocal 
Settings "command extension" and "delayed environment variable expansion" 
SETLOCAL ENABLEEXTENSIONS # enable "command extension" 
SETLOCAL DISABLEEXTENSIONS # disable "command extension" 
SETLOCAL ENABLEDELAYEDEXPANSION # enable the "delayed environment variable expansion" 
SETLOCAL DISABLEDELAYEDEXPANSION # disable "delayed environment variable expansion" 
ENDLOCAL # restored to its previous state using SETLOCAL statement 
"command extensions" enabled by default 
"delayed environment variable expansion" is disabled by default 
batch end The system will automatically restore the default values 
can modify the registry to disable "command extension", see cmd /?. So use "command extension" of the process 
sequence, the proposed beginning and ending with the SETLOCAL ENABLEEXTENSIONS and ENDLOCAL statements, to ensure the 
security procedures can run correctly on another system 
"delayed environment variable expansion"

set # Display all currently available variables, including system variables and custom variables 
echo% SystemDrive% # display system disk drive. System variables can be referenced directly 
set p # show all variables beginning with p, and if a = no. 1 is located ERRORLEVEL 
SET # P = aa1bb1aa2bb2 set variables p, and assigned = string behind, i.e. aa1bb1aa2bb2 
echo% P% # p represents a string variable display, i.e. aa1bb1aa2bb2 
echo% p: ~ #. 6% of all characters variable p after the first six characters, i.e. aa2bb2 
echo% p: ~ # 6,3% after the first six display character 3 characters, i.e. AA2 
echo% P: ~ # 0,3% 3 front display characters, i.e., AA1 
echo% P: -2% ~ # appearing at the end of two characters, i.e., B2 
echo% P: ~ 0, -2% # display other characters except the last two characters, i.e. aa1bb1aa2b 
echo% p:% AA c = c # is replaced with all of the variables p AA, i.e. displaying c1bb1c2bb2 
echo% p: aa = variable p #% All aa replacement string is empty, is displayed 1bb12bb2
echo% p: * bb = c% # of all the characters and a bb before being replaced is c, is displayed c1aa2bb2 
SET P =% p: * = c% # bb set variables p, assigned% p: * bb = c%, i.e. c1aa2bb2 
sET / # AP = 39 to set a numeric variable p, the value 39 
sET / AP = 39/10 # support operator, tail method rinsed with decimals, 39/10 = 3.9, to tail too. 3, P =. 3 
SET / AP = P / 10 # a / a parameter, at = after the variable may be used without% direct reference 
set / ap = "& 0 1 " # " and" operation, in quotes. Other supported operators see the SET /? 
The SET p = p # cancel variable 
set / pp = Please input 
screen is displayed on "enter", and will enter the string assigned to the variable p 
Note that this command can be used in place of choice 
Note that the variable and for the composite's statement if is replaced all at once, as 
@echo OFF 
SET P = aaa 
if aaa ==% P% ( 
     echo% P% 
     SET = P BBB 
     echo% P% 
     ) 
results are displayed 
aaa 
aaa 
as it has been all replaced aaa% p% when reading the if statement 
here "replace" in the /? help in means "expansion", "environment variable expansion" 
enable "delayed environment variable expansion" with! to refer to variables that! variable names! 
@echo OFF 
SETLOCAL ENABLEDELAYEDEXPANSION 
the SET the p- = aaa
the p-aaa%% == IF ( 
     echo the p-%% 
     set = bbb the p- 
     echo! the p-! 
     ) 
ENDLOCAL 
results will show 
aaa 
bbb 
there are several dynamic variables, run the set can not see the 
string% CD% # represents the current directory 
% current date dATE% # 
% tIME% # current time 
% rANDOM% # random integer, between 32767 ~ 0 
% #% ERRORLEVEL current value of ERRORLEVEL 
% CMDEXTVERSION% # current command processor extension version number 
% CMDCMDLINE% # call command processor original command line 
may be used to view the echo command value of each variable, such as% time% echo 
note% time% millisecond precision, when the batch process may require a delay used 

32 start 
command to call an external program batch, or and other external program after the completion of the instruction to continue the rest of the 

33 call 
to call another batch of the batch command, otherwise the rest of the batch command will not be executed 
sometimes a call to start the application with an error, you can also call call

34 choice (external command) 
to select commands 
allow the user to enter a character, so choose a different running order, errorlevel return code ...... for the 1234 
win98 where is choice.com 
win2000pro there are no, you can copy from win98 came in 
win2003 in a choice.exe 
choice / N / C y / T 5 / D y> nul 
5 seconds delay 

35 assoc ftype and 
association file 
assoc set an association 'file extension', associated to the 'file type' 
associated disposed ftype 'file type' associated ' the procedures and parameters' 
when you double-click a .txt file, windows are not based on direct .txt judgment notepad.exe open 
but the first judgment belongs txtfile .txt 'file type' 
command line and then call txtfile associated txtfile =% SystemRoot % system32NOTEPAD.EXE% 1 
in the "folder options" → "file types" in the modification of these two related 
assoc # display all 'file extension' association 
Assoc .exe # display represented .exe 'file type', the results show.= exefile EXE 
FTYPE # Show All "file types" associated 
assoc .txt # show 'file type' .txt representatives, the results showed .txt = txtfile
assoc .doc # Display 'file type' .doc representative showed the Word.Document.8 = .doc 
FTYPE exefile # exefile type display associated command line showed = exefile "%. 1"% * 
Assoc .txt = Word .Document.8 
set to word .txt types of documents, you can see the icon .txt files have changed 
assoc .txt = txtfile 
restore the right of association .txt 
ftype exefile = "% 1"% * 
correctly associated exefile of recovery 
if this association has been corrupted, run COMMAND.COM, and then enter this command 

36 pushd popd and 
switch the current directory 
@echo OFF 
C: MD & CD & # mp3 in C: establishing mp3 folder 
md d: mp4 # in D: establish mp4 folder 
cd / dd: mp4 # change the current directory to d: mp4 
pushd c: MP3 # save the current directory, and switch the current directory is c: MP3 
the popd # restore the current directory is just saved d: mp4 

37 for 
loop command 
this is more complicated, please control for /? view 
for %% i in (c: d: E: f :) do echo %% i 
in turn each call parentheses string, the command execution behind do 
note %% i, statement invokes parameters 2% in the batch for 
the default string delimiter is "spacebar", "the Tab key","enter" 
for %% i in (* .txt) do the Find "abc" %% i 
all txt in the current directory file execute the find command 
for / r. %% i in ( * .txt) do find "abc" %% i 
in the current directory and all subdirectories in a .txt file that contains the search string abc line 
for / r. %% (.) i in do echo %% ~ pni 
display the current directory and all subdirectories name, including the path, not including the drive letter 
for / rd: mp3 %% i in (* .mp3) do echo %% i >> d: mp3.txt 
the d: mp3 files and subdirectories in the name of mp3 files are saved to d: mp3.txt go 
for / l %% i in (2,1,8 ) do echo %% i 
generation of 2345678 a string of numbers, the sequence number 2 is the beginning, the end 8, each represents a plus 1 
for / F %% I in ( 'set') do echo %% I 
output of the set command cycle is called per line 
for / f "eol = P" %% i in ( 'set') Do echo %% i 
takes the output of the command set, ignoring the beginning few lines P
) Do echo %% b take the first 2 to %% a, gave the subsequent column B %% 
38 is SUBST (external command) 
mapping the disk. 
subst z: serverd # this input z: you can access the serverd 
for / f "skip = 5 tokens = 4" %% a in ( 'dir') Do echo %% a 
result of the dir command, skip ahead five rows, each of the remaining four take the first row

subst z: / d # unmap 
subst # shows the current when all enantiomers 

39 xcopy (external command) 
file copy 
xcopy d: mp3 e: mp3 / s / e / i / y 
copy d: mp3 folder, all subfolders folders and files to e:, overwrite the existing file
Plus / i said that if e: mp3 does not automatically create a new folder, or there will be asked

 

---------------------
Disclaimer: This article is CSDN blogger "CIAS" of the original article, follow the CC 4.0 by-sa copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/hanzheng260561728/article/details/51558990

Guess you like

Origin www.cnblogs.com/tangsong41/p/11320658.html