Batch

  1. Batch – Batch processing; Batch file — Batch file;

  2. DOS batch processing;

  3. It is interpreted and executed by DOS or Windows built-in command interpreter (for example, cmd.exe).

  4. Similar to Unix shell scripts;

  5. In batch processing, you can not only use the commands provided by the system itself, but also use tools or software provided by the installed third party.

  6. In general, each command consists of one line. Of course can also be multiple orders with a specific symbol (&& and | | |) after separating wrote the same line;

  7. When the system explains how to run a batch program, it scans the entire batch program first, and then executes all commands from the first line of code down to the end of the program or exit command or an error.

Basic batch command

  • echo
  • rem
  • pause
  • call
  • start
  • goto
  • set

Link: Summary of common Batch Commands – An introduction to batch commands

An introduction to common symbols used in batch processing

  • Echo shielding@
  • Redirect 1> 与 >>
  • Redirect 2<
  • Pipe symbol|
  • Escape character^
  • Logical command character& && ||

Link: Summary of common batch commands – An introduction to batch symbols

Basic DOS commands

File system Operations

  • vol
  • lable

Folder management

  • cd
  • md/mkdir
  • rd/rmdir
  • dir
  • tree
  • path
  • xcopy

File management

  • type
  • copy
  • del/erase
  • move
  • ren/rename
  • replace
  • attrib
  • find
  • fc

Network related

  • ping
  • ftp
  • net
  • telnet
  • ipconfig
  • msg
  • arp

System management

  • at
  • shutdown
  • tskill
  • taskkill
  • tasklist
  • sc
  • reg
  • powercfg

Other commands

  • cls
  • assoc
  • ftype

For all of the commands listed above, enter the command +/? You can view the help information about the command. Such as find /?

Windows Batch Common commands and examples

  1. 1 the echo and @

  2. The echo command

  3. # Disable single-line echo

  4. Echo off # Turn off the echo from the next line

  5. Echo off # Turn off the echo from this line. This is the first row in a batch

  6. Echo on # opens the echo from the next line

  7. Echo # Displays whether the current state is Echo OFF or Echo on

  8. Echo. # prints a "enter newline", a blank line

  9. # (with echo, echo; echo+ echo[ echo] echo/ echo)

  10. 2 errorlevel

  11. echo %errorlevel%

  12. At the end of each command, you can use this command line format to view the return code

  13. The default value is 0. Errorlevel is set to 1 when the command execution fails

  14. 3 dir

  15. Show folder contents

  16. Dir # displays files and subdirectories in the current directory

  17. Dir /a # Displays files and subdirectories in the current directory, including hidden files and system files

  18. Dir c: /a:d # Displays the directories in the current directory of drive C

  19. Dir c: /a:-d # Displays the files in the root directory of drive C

  20. Dir c: /b/p #/b

  21. Dir *.exe /s # displays all. Exe files in the current directory and subdirectories

  22. 4 cd

  23. Switch directory

  24. CD # Go to root directory

  25. CD # displays the current directory

  26. CD /d d: SDK # Can change drive letter and directory at the same time

  27. 5 md

  28. Create a directory

  29. Md d: ABC # If d:a does not exist, the intermediate directory will be created automatically

  30. If the command extension is disabled, type mkdir ABC.

  31. 6 rd

  32. Delete the directory

  33. Rd ABC # delete the ABC subdirectory in the current directory

  34. Rd /s/q d:temp # Delete d:temp folder and its subfolders and files, /q quiet mode

  35. 7 del

  36. Delete the file

  37. Del d:test.txt # Delete the specified file

  38. del /q/a/f d:temp*.*

  39. Delete all files in the d:temp folder, including hidden, read-only, and system files, excluding subdirectories

  40. del /q/a/f/s d:temp*.*

  41. Delete all files in d:temp and subfolders, including hidden, read-only, and system files, excluding subdirectories

  42. 8 ren

  43. Rename command

  44. Ren D: Temp TMP # Supports folder renaming

  45. 9 cls

  46. Clear the screen

  47. 10 type

  48. Display file contents

  49. Type c:boot.ini # Displays the contents of the specified file

  50. Type *.txt # Displays the contents of all.txt files in the current directory

  51. 11 copy

  52. Copy files

  53. copy c:test.txt d:test.bak

  54. Copy the c:test. TXT file to d: and rename it test.bak

  55. copy con test.txt

  56. Wait for input from the screen, press Ctrl+Z to finish typing, and save the input as test.txt

  57. Con for screen, PRN for printer, nuL for empty device

  58. copy 1.txt + 2.txt 3.txt

  59. TXT and 2. TXT and save them as 3. TXT files

  60. TXT is saved to 1.txt if 3. TXT is not specified

  61. copy test.txt +

  62. Copying a file to yourself actually changes the date of the file

  63. 12 title

  64. Sets the title of the CMD window

  65. You can see that the title bar of the CMD window has changed

  66. 13 ver

  67. Display system Version

  68. 14 label and vol

  69. Set the volume label

  70. Vol # Displays volume label

  71. Label # displays the volume label and prompts for a new volume label

  72. Label C :system # Set the volume label of drive C to system

  73. 15 pause

  74. Suspension order

  75. 16 and rem...

  76. Annotation commands

  77. Comment lines do not perform operations

  78. The date and time

  79. Date and time

  80. Date # displays the current date and prompts for a new date. Press enter to skip typing

  81. Date /t # Displays only the current date and does not prompt for a new date

  82. Time # Displays the current time and prompts you to enter a new time. Press enter to skip input

  83. Time /t # Displays only the current time and does not prompt you to enter a new time

  84. 18 goto and:

  85. A jump command

  86. :label # Indicates that the line is a label line and no operation is performed on the label line

  87. Goto label # Jumps to the specified label line

  88. 19 find (External command)

  89. To find the command

  90. find "abc" c:test.txt

  91. Look for the ABC string in c:test. TXT

  92. If the errorLevel return code is not found, the errorLevel return code is set to 1

  93. The find/I "ABC" c: test. TXT

  94. Find the line containing ABC, ignoring case

  95. find /c "abc" c:test.txt

  96. Displays the number of rows containing ABC

  97. 20 More (External Command)

  98. Each screen

  99. More c:test. TXT # display the contents of c:test. TXT file screen by screen

  100. 21 tree

  101. Display directory structure

  102. Tree D: # Displays the file directory structure of drive D

  103. 22 &

  104. Execute multiple commands sequentially, regardless of whether the commands are successfully executed

  105. 23 &&

  106. Run multiple commands in sequence. If a command fails to be executed, subsequent commands are not executed

  107. Find "ok" c:test.txt && echo successful

  108. If the word "OK" is found, "success" is displayed, if not, it is not displayed

  109. 24 | |

  110. Run multiple commands in sequence. If a correct command is executed, subsequent commands are not executed

  111. Find "ok" c: test. TXT | | echo is not successful

  112. If the word "OK" is not found, it is displayed as "unsuccessful", and if it is found, it is not displayed

  113. 25 |

  114. piping

  115. dir *.* /s/a | find /c ".exe"

  116. The pipe command indicates that the dir command is executed and the find command is executed on the output

  117. The command line result: output the number of. Exe files in the current folder and all subfolders

  118. type c:test.txt|more

  119. This has the same effect as more C :test.txt

  120. 26 > and > >

  121. Output the redirection command

  122. > Clear the original contents of the file before writing

  123. >> Appends content to the end of the file without erasing the original content

  124. Outputs the content that would have been displayed on the screen to a specified file

  125. Specifies that the file is automatically generated if it does not exist

  126. type c:test.txt >prn

  127. Instead of displaying the contents of the file on the screen, switch to output to the printer

  128. echo hello world>con

  129. Display Hello World on the screen, with virtually all output defaulting to >con

  130. copy c:test.txt f: >nul

  131. Copy the file, and do not display "file copy success" message, but if disk F does not exist, will still display an error message

  132. copy c:test.txt f: >nul 2>nul

  133. The file replication success message is not displayed. If disk F does not exist, no error message is displayed

  134. echo ^^W ^> ^W>c:test.txt

  135. The generated file content is ^W > W

  136. ^ and > are control commands that must be preceded by a ^ sign to output them to a file

  137. 27"

  138. Get input from a file, not from the screen

  139. This command is used to wait for input, such as date time label

  140. @echo off

  141. echo 2005-05-01>temp.txt

  142. date <temp.txt

  143. del temp.txt

  144. This allows you to change the current date without waiting for input

  145. 28 %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 %*

  146. Arguments passed from the command line to the batch

  147. %0 Batch the file itself

  148. %1 the first argument

  149. %9 The ninth parameter

  150. %* All arguments starting with the first argument

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

  152. %~1 - Delete quotes (") to expand %1

  153. %~f1 - Expands %1 to a fully qualified pathname

  154. %~d1 - Just expand %1 to a drive letter

  155. %~p1 - expand %1 to a path only

  156. %~n1 - Just expand %1 to a filename

  157. %~x1 - Just expand %1 to a file extension

  158. %~s1 - The expanded path contains a short name

  159. %~a1 - Extends %1 to file attributes

  160. %~t1 - expands %1 to the date/time of the file

  161. %~z1 - Expands %1 to the size of the file

  162. %~$PATH: 1 - Find the column in the directory of the PATH environment variable and place %1

  163. Expand to the first fully qualified name found. If the environment

  164. The variable name is not defined, or the file is not found

  165. Expand to an empty string

  166. Multiple results can be obtained by combining modifiers:

  167. %~dp1 - extends %1 to drive letters and paths only

  168. %~nx1 - only extend %1 to filename and extension

  169. %~dp$PATH:1 - Find %1 in the directory listed in the PATH environment variable,

  170. And extends to the drive letter and path of the first file found.

  171. %~ftza1 - extends %1 to a DIR like output line.

  172. See call/? Or for /? See what each parameter means

  173. echo load "%%1" "%%2">c:test.txt

  174. Load "%1" "%2"

  175. In a batch file, use this format to print command line arguments to the file

  176. 29 if

  177. Judge ordered

  178. If "%1"=="/a" echo

  179. If/I "%1" equ "/a" echo

  180. / I is case insensitive, and equ is the same as ==. For other operators, see if/?

  181. If exist C :test.bat echo The c:test.bat file exists

  182. if not exist c:windows (

  183. The c: Windows folder does not exist

  184. )

  185. if exist c:test.bat (

  186. Echo is c: test. Bat

  187. ) else (

  188. Echo c:test.bat does not exist

  189. )

  190. Setlocal and endlocal 30

  191. Set command extension and Delay Environment Variable Expansion

  192. SETLOCAL ENABLEEXTENSIONS # enable command extensions

  193. SETLOCAL DISABLEEXTENSIONS # disable command extensions

  194. SETLOCAL ENABLEDELAYEDEXPANSION

  195. SETLOCAL DISABLEDELAYEDEXPANSION # disable "delay environment variable expansion"

  196. ENDLOCAL # restore the state before using the SETLOCAL statement

  197. Command Extension is enabled by default

  198. Delay environment variable scaling is disabled by default

  199. The system automatically restores the default value after the batch processing ends

  200. You can modify the registry to disable "command extensions". See CMD /? . So the program that uses the "command extension"

  201. It is recommended to add SETLOCAL ENABLEEXTENSIONS and ENDLOCAL statements at the beginning and end to ensure that

  202. Ensure that the program runs correctly on other systems

  203. "Delay environment variable expansion" is mainly used for if and for conformance statements. There are actual use routines in the description of set

  204. 31 set

  205. Set a variable

  206. Reference variables can be preceded by %, that is, % variable name %

  207. Set # Displays all currently available variables, including system variables and custom variables

  208. Echo %SystemDrive% # displays the SystemDrive letter. System variables can be referenced directly

  209. Set p # display all variables starting with p, if none, set errorLevel =1

  210. Set p= aa1bb1AA2bb2 # Set the variable p and assign it to the string following =, that is, aa1bb1AA2bb2

  211. Echo %p% # displays the string represented by the variable p, aa1bb1AA2bb2

  212. Echo %p:~6% # displays all characters after the 6th character in the variable p, that is, aa2bb2

  213. Echo %p:~6,3% # displays three characters after the sixth character, that is, aa2

  214. Echo %p:~0,3% # Displays the first three characters, that is, aa1

  215. Echo %p:~-2% # Displays the last two characters, b2

  216. Echo %p:~0,-2% # displays all but the last two characters, that is, aa1bb1AA2b

  217. Echo %p:aa=c% # echo %p:aa=c% #

  218. Echo %p:aa=% # Replace all aa strings in p with null, that is, display 1bb12bb2

  219. Echo %p:*bb=c% # echo %p:*bb=c% #

  220. Set p=%p:*bb=c% # set p=%p:*bb=c%, c1aa2bb2

  221. Set p=39 # set p to 39

  222. Set /a p=39/10, p=3

  223. Set /a p=p/10 # set /a p=p/10 #

  224. Set /a p= "1&0" # "and". For other supported operators, see set/?

  225. Set p= # cancel the p variable

  226. Set /p p= Enter a value

  227. The screen displays "Please enter" and assigns the input string to the variable P

  228. Note that this can be used instead of the choice command

  229. Note that variables are replaced all at once in the if and for compound statements, as in

  230. @echo off

  231. set p=aaa

  232. if %p%==aaa (

  233. echo %p%

  234. set p=bbb

  235. echo %p%

  236. )

  237. The result will show

  238. aaa

  239. aaa

  240. Because all %p% is replaced with AAA when the if statement is read

  241. The "replace" here, in /? Help refers to "expand", "environment variable expansion"

  242. You can enable "Delay environment variable scaling" with! To reference the variable, that is! The variable name!

  243. @echo off

  244. SETLOCAL ENABLEDELAYEDEXPANSION

  245. set p=aaa

  246. if %p%==aaa (

  247. echo %p%

  248. set p=bbb

  249. echo ! p!

  250. )

  251. ENDLOCAL

  252. The result will show

  253. aaa

  254. bbb

  255. There are also several dynamic variables that you can't see when you run set

  256. %CD% # is a string representing the current directory

  257. %DATE% # Current DATE

  258. %TIME% # Current TIME

  259. %RANDOM% # The value is a RANDOM integer between 0 and 32767

  260. %ERRORLEVEL% # The current ERRORLEVEL value

  261. %CMDEXTVERSION% # Specifies the version of the current command processor extension

  262. %CMDCMDLINE% # Invokes the original command line of the command processor

  263. You can view the value of each variable with the echo command, such as echo %time%

  264. Note that %time% is accurate to the millisecond and can be used when batch processing needs to be delayed

  265. 32 start

  266. A command that invokes an external program in batch processing, or waits for the external program to complete before continuing to execute the rest of the instructions

  267. 33 call

  268. A command that calls another batch in a batch, otherwise the remaining batch instructions will not be executed

  269. Sometimes an application fails with a start call

  270. 34 Choice (External command)

  271. Select the command

  272. Let the user enter a character to choose to run a different command with the return code errorLevel 1234...

  273. In the win98 is choice.com

  274. Win2000pro not, you can copy from Win98

  275. Is choice in win2003. Exe

  276. choice /N /C y /T 5 /D y>nul

  277. Delay for 5 seconds

  278. Ftype and 35 assoc

  279. File associations

  280. Assoc sets' file extension 'association to' file type '

  281. Ftype sets' file type 'association to' executor and parameter '

  282. When you double-click on a.txt file, Windows doesn't just open it with Notepad.exe from.txt

  283. I'm going to say.txt is txtfile 'file type'

  284. Txtfile =%SystemRoot% system32notepad.exe %1

  285. You can change these associations in folder options → File Type

  286. Assoc # Displays all 'file extension' associations

  287. Assoc. TXT # displays the 'file type' represented by.txt, and the result shows.txt=txtfile

  288. Assoc.doc # displays the 'file type' represented by.doc, and the result shows.doc= word.document.8

  289. Exe represents the 'file type' and the result shows.exe=exefile

  290. Ftype # Displays all 'file type' associations

  291. Ftype exefile # Displays the command line associated with the exefile type. The result shows exefile="%1" %*

  292. assoc .txt=Word.Document.8

  293. TXT as word document, you can see that the ICONS of the. TXT file have changed

  294. assoc .txt=txtfile

  295. Restore the correct association of.txt

  296. ftype exefile="%1" %*

  297. Restore the correct association of exefile

  298. If the association has been broken, run command.com and enter this command

  299. 36 the pushd and popd

  300. Switch current directory

  301. @echo off

  302. C: & CD & md mp3 # Create an MP3 folder in C:

  303. Md D: mp4 # Create mp4 folder in D: mp4

  304. CD /d d:mp4 # Change current directory to D :mp4

  305. Pushd C :mp3 # Save the current directory and switch the current directory to C :mp3

  306. Popd # restore the current directory to d:mp4

  307. 37 for

  308. Loop command

  309. This is more complicated, please compare for/? Come to see

  310. for %%i in (c: d: e: f:) do echo %%i

  311. Invoke each string in parentheses in turn, executing the commands following do

  312. Note that %% I is used in the batch for call argument with 2 %

  313. The default string delimiters are space, Tab, enter

  314. for %%i in (*.txt) do find "abc" %%i

  315. Execute find on all TXT files in the current directory

  316. for /r . %%i in (*.txt) do find "abc" %%i

  317. Search all.txt files in the current directory and subdirectories for lines containing the ABC string

  318. for /r . %%i in (.) do echo %%~pni

  319. Displays the current directory name and all subdirectory names, including paths but not drive letters

  320. for /r d:mp3 %%i in (*.mp3) do echo %%i>>d:mp3.txt

  321. Save the names of mp3 files in d:mp3 and their subdirectories to d:mp3.txt

  322. For /l %% I in (2,1,8) do echo %% I

  323. Generate a sequence of numbers 2345678, where 2 is the beginning of the sequence, 8 is the end, and 1 means increment by 1 each time

  324. for /f %%i in ('set') do echo %%i

  325. Loop over the output of the set command, one line per line

  326. for /f "eol=P" %%i in ('set') do echo %%i

  327. Take the output of the set command, ignoring the lines starting with P

  328. for /f %%i in (d:mp3.txt) do echo %%i

  329. Display each file name in d:mp3. TXT, one for each line, no names with Spaces supported

  330. for /f "delims=" %%i in (d:mp3.txt) do echo %%i

  331. Display each file name in d:mp3. TXT, one for each line, supporting names with Spaces

  332. for /f "skip=5 tokens=4" %%a in ('dir') do echo %%a

  333. For the result of the dir command, skip the first five lines and take the fourth column for each remaining line

  334. The separator between each column is the default "space"

  335. Notice that the first five lines of the dir command output do not have a filename

  336. For /f "tokens=1,2,3 delims= "%%a in ('date /t') do (

  337. echo %%a

  338. echo %%b

  339. echo %%c

  340. )

  341. The output result of date /t is 1, 2, 3 columns per row

  342. The first column corresponds to the specified %%a, followed by %%b and %%c, which are derived for the other columns

  343. The delimiters are specified as - and "space", note that delims=- is followed by a "space"

  344. Tokens =1,2,3 Tokens =1-3

  345. for /f "tokens=2* delims=- " %%a in ('date /t') do echo %%b

  346. Take the second column to %% A and all subsequent columns to %% B

  347. 38 Subst (External Command)

  348. Mapping a disk.

  349. Subst z: serverd # To access serverd, type z

  350. Subst z: /d # Cancel the mapping

  351. Subst # displays all current times

  352. 39 xcopy (External command)

  353. File copy

  354. xcopy d:mp3 e:mp3 /s/e/i/y

  355. Copy the D :mp3 folder, all subfolders and files to e: and overwrite the existing files

  356. If e: does not have an mp3 folder, it will automatically create a new one. Otherwise, there will be an inquiry link