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

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

Reprinted from: "Fun It" public number

Objective

Batch file contains a series of DOS commands that are typically used to automate repetitive tasks. Users simply double-click a batch file to perform tasks without having to enter the same instruction repeatedly. Writing a batch file is simple, but the challenge is to make sure everything is executed sequentially. Writing rigorous batch files can save a lot of time and is especially effective when dealing with repetitive tasks.

Use batch processing in Windows to simplify a lot of repetitive work

What is batch processing

Batch, also known as a batch script. As the name implies, batch processing is the batch processing of an object. The batch file has a name extension of bat.

At present, the more common batch processing includes two types: DOS batch processing and PS batch processing. PS batch processing is based on the powerful image editing software Photoshop, used to batch pictures of the script, and DOS batch processing is based on DOS commands, used to automate the mass execution of DOS commands to implement a specific operation of the script. This is about DOS batching.

Batch processing is a simplified scripting language that is used in DOS and Windows systems, and is interpreted by a DOS or Windows system-embedded command interpreter (usually Command.com or CMD.EXE). Similar to a shell script in Unix. The simplest example of a batch file with an extension of. bat or. cmd is the various commands that are used to line up the command line. More complex situations require the use of command programs such as If,for,goto to run the process, as in advanced languages such as C,basic. If you need to implement more complex applications, it is necessary to take advantage of external programs, including external commands provided by the system itself and tools or software provided by third parties.

A batch file, or batch program, is a plain text file consisting of a set of DOS commands that can be edited directly in Notepad or created with a DOS command, or edited with a DOS text editor Edit.exe. Under command prompt, type the name of the batch file, or double-click the batch file, and the system calls Cmd.exe to run the batch process. In general, each command occupies one line; Of course, you can use multiple commands with specific symbols (such as:&, &&, |, | | And so on) are separated and written to the same line, and there is the case that higher-level commands like if, for, can occupy a few lines or even a few hundred of space.

When the system interprets running a batch program, it first scans the entire batch program and then executes all commands down from the first line of code until the end of the program or the Exit command is encountered or an error exits unexpectedly.

Introduction to Batch processing commands
    • Echo

    • Rem

    • Pause

    • Pager

    • Start

    • Goto

    • Set

Http://xstarcd.github.io/wiki/windows/windows_cmd_summary_commands.html

Introduction to Batch processing symbols
    • echo Mask @

    • REDIRECT 1 > >>

    • REDIRECT 2 <

    • Pipe Symbols |

    • Escape character ^

    • Logical command characters include:&, &&, | |

Http://xstarcd.github.io/wiki/windows/windows_cmd_summary_symbols.html

Common DOS CommandsFolder 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 @The echo command @ #关闭单行回显echo off #从下一行开始关闭回显 @echo off #从本行开始关闭回显.                 The first line of the general batch is the echo on #从下一行开始打开回显echo #显示当前是 echo off state or echo on status Echo. #输出一个 "Carriage return", Blank line # (with Echo, echo; echo+ echo[Echo] echo/echo)

2 ERRORLEVEL
echo%errorlevel% each command to run the end, you can use this command line format to see the return code default value is 0, General Command execution error will be set ERRORLEVEL 1

3 dir
Display folder Contents dir #显示当前目录中的文件和子目录dir/a #显示当前目录中的文件和子目录, including hidden files and system files dir C:/a:d #显示 C-disk current mesh Dir c:/a:-d #显示 C packing directory file dir c:/b/p #/b only displays the file name,/P page shows the dir *.exe/s #显示当前目录和子目录里所有的. exe file

4 CD
Switch Directory CD #进入根目录cd #显示当前目录cd/d d:sdk #可以同时更改盘符和目录

5 MD
Create directory MD D:ABC #如果 d:a does not exist and will automatically create intermediate directory # If the command extension is disabled, you will need to type mkdir ABC.

6 Rd
Delete Directory RD ABC #删除当前目录里的 ABC subdirectory, require an empty directory rd/s/q d:temp #删除 d:temp folder and its subfolders and files,/q Quiet mode

7 del
Delete Files del d:test.txt #删除指定文件, cannot be hidden, system, read-only files del/q/a/f d:temp*.* delete all files in D:temp folder, including hidden, read-only, system files, not including subdirectories del/q/a/f/s D:te Mp*.* Delete all files in d:temp and subfolders, including hidden, read-only, system files, not including subdirectories

8 Ren
Rename command ren d:temp tmp #支持对文件夹的重命名

9 CLS
Clear Screen

Ten type
Display file contents type C:boot.ini #显示指定文件的内容, program files generally display garbled type *.txt #显示当前目录里所有. txt file contents

Copy
Copy the file copy c:test.txt d:test.bak Copy the C:test.txt file to D:, rename it to test.bakcopy con test.txt wait for input from the screen, press CTRL + Z to end the input, save the input as Test.txt text Piece con represents the screen, PRN represents the printer, NUL represents the empty device copy 1.txt + 2.txt 3.txt merge 1.txt and 2.txt content, save as 3.txt file if 3.txt is not specified, save to 1.txtcopy Test.txt + complex File to yourself, and actually modify the file date

Title
Sets the title of the cmd window titled title new title #可以看到cmd窗口的标题栏变了

verShow System version

Label and volSet Volume Label Vol #显示卷标label #显示卷标 and prompt for new volume label C:system #设置C盘的卷标为 system

PausePause command

REM and::Comment Command Comment Line does not perform action

Date and time
Date and time #显示当前日期, and prompted to enter a new date, press "Enter" to skip the input date/t #只显示当前日期, do not prompt for a new date time #显示当前时间, and prompt for New times, press "enter" Skip input Tim e/t #只显示当前时间, do not prompt to enter a new time

Goto and:
Jump command: Label #行首为: Indicates that the row is a label row, and the label row does not perform an action goto label #跳转到指定的标签那一行

find (External command)
Find a command find the "ABC" C:test.txt in the C:test.txt file search for lines with an ABC string if not found, set the ERRORLEVEL return code to 1find/i "ABC" C:test.txt Look for lines with ABC, ignoring case fin D/C "ABC" C:test.txt shows the number of rows with ABC

More (external command)
Show more c:test.txt #逐屏显示 c:test.txt file content per screen

Tree
Show directory structure Tree D: #显示D盘的文件目录结构

&
Executes multiple commands sequentially, regardless of whether or not the command is executed successfully

&&
Execute multiple commands sequentially, do not execute the following command after executing the wrong command find "OK" c:test.txt && echo Success if the word "OK" is found, it shows "success" and does not appear when it is found.

24 | |
Sequential execution of multiple commands will not execute the following command after executing the correct command find "OK" c:test.txt | | Echo is unsuccessful if the word "OK" is not found, it will show "unsuccessful" and will not show

25 |
Pipe command dir * */s/a | The find/c ". exe" Pipeline command indicates that the dir command is executed before the result of its output executes the command-line result: the number of. exe files in the current folder and all subfolders is output type C:test.txt|more this and more C: The effect of Test.txt is the same.

> >>
Output REDIRECT Command > Clear the contents of the file before writing >> append the content to the end of the file, without clearing the original content output to the specified file if it does not exist, the file is automatically generated if it does not have the type C:test.txt >prn The file content is not displayed on the screen, turn output to the printer echo Hello World>con displays Hello World on the screen, virtually all output is the default >con copy c:test.txt f: >nul copy File And does not display the "File copy succeeded" message, but if the F drive does not exist, an error message is displayed. Copy c:test.txt f: >nul 2>nul does not display "file copy succeeded" message, and F disk does not exist, also does not display error message echo ^^ W ^> ^w>c:test.txt generated file content for ^w > w^ and > are control commands, to output them to a file, you must add a ^ symbol in front

<
Get input from a file instead of a command that waits for input from the screen, such as a date time label @echo Offecho 2005-05-01>temp.txtdate <temp.txtdel Temp.txt This allows you to modify the current date without waiting for input

28%0%1%2%3%4%5%6%7%8%9%*
The command line passed to the batch parameter%0 Batch file itself%1 first parameter%9 nineth parameter%* the substitution of all parameter batch parameters (%n) starting with the first parameter has been enhanced. You can use the following syntax:%~1-Remove quotation marks ("), expand%1%~f1-expands%1 to a fully qualified pathname%~D1-expands%1 to only one drive letter% ~P1-expands%1 to only one path%~n1-expands%1 to a filename only%~x1-expands%1 to a file name extension%~s1-Expanded path Refers to a short name%~A1-expands%1 to file attribute%~t1-date/time of extension%1 to file%~z1-expands%1 to file size%~ $PATH: 1 -Find the directory that is listed in the PATH environment variable and expand%1 to the first fully qualified name found. If the environment variable name is not defined, or if the file is not found, the key combination expands to an empty string that can be combined with a modifier to achieve multiple results:%~DP1-only extend%1 to drive letter and path %~NX1-only extend%1 to file name and extension%~dp$path:1-look for% 1 in the directory listed in the PATH environment variable, and expand to the drive letter and path of the first file found. %~FTZA1-Extended%1 to an output line similar to DIR. Can you refer to call/? or for/? See the meaning of each parameter echo load "%%1" "%%2" >c:test.txt generated file content for the load "%1" "%2" batch file, use this format to output command line arguments to the file

if
Judgment command If "%1" = = "/A" echo the first parameter is/aif/i "%1" equ "/A" echo the first parameter is/a/i is case insensitive, equ and = = are the same, other operators see if/?if exist C:test.bat echo exists C:test.bat file if not exist C:windows (Echo does not exist C:windows folder) if exist C:test.bat (Echo exists c:test.bat Else (Echo does not exist C:test.bat)

setlocal and Endlocal
Set command extension and defer environment variable expansion SETLOCAL enableextensions #启用 command extension SETLOCAL disableextensions #停用 command extension setloc AL enabledelayedexpansion #启用 "Delaying environment variable expansion" SETLOCAL disabledelayedexpansion #停用 "delaying the expansion of environmental variables" endlocal #恢复到使用SETLOCAL语句以前的状态 "command extension" defaults to enable "Defer environment variable expansion" By default to deactivate batch end system automatically restores default can modify the registry to disable "command extension", see CMD/?. Therefore, the "command extension" program, 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 "delay environment variable expansion" is mainly used for if and for the compliance statement, in the description of the set There is actually a use case process

Set
The variable reference variable can be added before and after the variable name, that is, the% variable name%set #显示目前所有可用的变量, including the system variable and the custom variable echo%systemdrive% #显示系统盘盘符. The system variable can directly refer to the set P #显示所有以p开头的变量, if one does not have the Errorlevel=1set p=aa1bb1aa2bb2 #设置变量p, and assigns the value = the following string, namely AA1BB1AA 2bb2echo%p% #显示变量p代表的字符串, namely Aa1bb1aa2bb2echo%p:~6% #显示变量p中第6个字符以后的所有字符, Aa2bb2echo%p:~6,3% The #显示第6个字符以后的3个字符, namely the Aa2echo%p:~0,3% #显示前3个字符, namely the Aa1echo%p:~-2% #显示最后面的2个字符, namely B2echo%p:~0,-2% #显示除了最后2个字符以外的其它字符, that is, the Aa1bb1aa2becho%p:aa=c% #用c替换变量p中所有的aa, which shows C1bb1c2bb2echo%p:aa=% #将变量p中的所有aa The string substitution is empty, which displays the 1bb12bb2echo%p:*bb=c% #第一个bb及其之前的所有字符被替换为c, which displays the C1aa2bb2set p=%p:*bb=c% #设置变量p, which is assigned%p:*bb=c%, which is c1aa2bb2set/a p=39 #设置p为数值型变量, value is 39set/a P=39/10 #支持运算符, with a decimal when using the tail method, 39/10=3.9, go to the tail 3,p=3set/a P=P/10 #用 the/A parameter, the variable after = can be quoted without a% direct reference set/a p= "1&0″#" and "operation, to be quoted." Other supported operators see Set/?set p= #取消p变量set/P p= Please enter the screen with "Please enter" and assign the input string to the variable pNote that this can be used instead of the choice command to note that variables are all replaced at once in the compound statement of if and for, such as @echo offset P=aaaif%p%==aaa (echo%p% set p=bbb echo%p %) The result will show aaaaaa because all%p% have been replaced with AAA here when reading the IF statement, in the/? Help means "expansion", "Environment variable expansion" can be enabled "delay environment variable expansion", with! To reference a variable, that is! variable name! @echo offsetlocal enabledelayedexpansionset p=aaaif%p%==aaa (echo%p% set p=bbb echo!p! Endlocal results will show aaabbb there are several dynamic variables, run set cannot see%cd% #代表当前目录的字符串%date% #当前日期%time% #当前时间%random% #随机整数, 0~32767%errorlevel% #当前 ERRORLEVEL value%cmdextversion% #当前命令处理器扩展名版本号% Cmdcmdline% #调用命令处理器的原始命令行可以用echo命令查看每个变量值, such as Echo%time% Note that%time% is accurate to milliseconds and can be used when batch processing requires delay processing

start
commands from external programs are called in batches, otherwise the remaining instructions are not executed until the external program finishes

Call
Another batch command is called in the batch, otherwise the remaining batch instructions will not be executed sometimes the application uses the start call error or call

Choice (external command)
Select the command to let the user enter a character, thus choosing to run different commands, return code ERRORLEVEL is 1234 ... Win98 in the Choice.comwin2000pro is not, can be copied from the Win98 win2003 is choice.exechoice/n/C y/t 5/d y>nul delay 5 seconds

Assoc and Ftype
File Association Assoc Settings ' file Extension ' Association, association to ' file type ' ftype setting ' File Type ' association, associated to ' executing programs and Parameters ' when you double-click a. txt file, Windows is not directly judged by the. txt notepad.exe Open rather first determine that the. txt belongs to txtfile ' file type ' and then call txtfile associated command line Txtfile=%systemroot%system32notepad.exe%1 can be in "folder Options" → "File type" Modify these 2 Associations Assoc #显示所有 ' file extension ' associated Assoc. txt #显示 the ' file type ' represented by the. Txt=txtfileassoc. doc #显示. doc for ' file type ' ', the results show. Doc=word.document.8assoc. exe #显示. exe represents the ' file type ', and the results are displayed. Exe=exefileftype #显示所有 ' file Type ' association ftype exefile #显示exefile类型关联的命令行, the results show exefile= "%1"%*assoc. Txt=word.document.8 setting the. txt to a Word type of document, you can see that the icon for the. txt file has changed assoc. txt= Txtfile Restore the correct association of. TXT ftype exefile= "%1"%* Correct association of Exefile if the association has been corrupted, you can run Command.com and enter this command

pushd and POPD
Toggle current directory @echo OFFC: & CD & MD MP3 #在 C: Establish MP3 folder MD d:mp4 #在 D: Create MP4 folder cd/d D:mp4 #更改当前目录为 d:mp4pushd C:mp3 #保存当前目录 and switch the current directory to C:MP3POPD #恢复当前目录为刚才保存的 D:mp4

PNS for
Circular command This is more complicated, please control for/? Let's see for%%i in (c:d: e:f:) do echo%%i each string in parentheses, execute the command following the do, note%%i, in batch the for statement call parameter with 2% default String delimiter is "Space key", "Tab key", "return" for %%i in (*.txt) do find "ABC"%%i executes the Find command for/r for all txt files in the current directory. %%i in (*.txt) do find "ABC"%%i searches all the. txt files in the current directory and subdirectories for the line for/r containing the ABC string. %%i in (.) Does echo%%~PNI displays the current directory name and all subdirectory names, including the path, not including the drive letter for/r D:mp3%%i in (*.mp3) do echo%%i>>d:mp3.txt put the D:mp3 and its subdirectories in M The file names of the P3 files are stored in the D:mp3.txt for/l%%i in (2,1,8) do echo%%i generates a string of 2345678 numbers, 2 is the beginning of the sequence of numbers, 8 is the end, and 1 means that each add 1for/f%%i in (' Set ') does Ech o%%i the output of the set command loop, one for/f "Eol=p"%%i in (' Set ') does echo%%i takes the output of the set command, ignoring those lines that begin with P for/f%%i in (d:mp3.txt) do Echo%%i displays each file name in D:mp3.txt, one per line, with a space name for/f "delims="%%i in (d:mp3.txt) do echo%%i shows each file name in D:mp3.txt, one per line, support with spaces The name for/f "Skip=5 tokens=4"%%a in (' dir ') does echo%%a the result of the dir command, skipping the first 5 rows, the remaining row 4th column each column between the delimiter as the default "space" can be noted that the dir command output of the top 5 lines is no file name for/f "tokens=1,2,3 delims=-"%%a in (' date/t ') do (echo%%a echo%%b echo%%c) on date The output of/T, each row takes 1, 2, 3 columns of the first column corresponding to the specified%%a, the following%%b and%%c are derived, corresponding to the other column delimiter is specified as-and "space", note that there is a "space" behind the delims=-, where tokens=1,2,3 with tokens=1- 3 Replace, the effect is the same for/f "tokens=2* delims=-"%%a in (' date/t ') does echo%%b take the 2nd column to%%a, followed by the column to%%b

subst (external command)
Mapped disks. Subst Z:serverd #这样输入z: You can access serverd subst Z:/d #取消该映射subst #显示目前所有的映时

A. xcopy (external command)File copy xcopy D:mp3 e:mp3/s/e/i/y copy D:mp3 folder, all subfolders and files to E:, overwrite the existing file plus/I indicates if e: no mp3 folder will be automatically created, otherwise there will be a query

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

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.