Batch-processing Bat command

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

1, batch processing commonly used symbols:

-Echo turns on echo or turn off the request Echo feature, or displays a message. If there are no parameters, the echo command displays the current echo setting syntax: @echo [{on|off}] echo{"Show Info"}

-REM Annotation commands, just act as a comment, easy for others to read and modify yourself later

-Pause pauses and the following message will be displayed: Press any key to continue ...

-Call calls another batch program from one batch and does not terminate the parent batch program syntax: [Drive:][path] FileName [Batchparameters] [: Label [arguments]

-Start invokes external programs, all DOS commands and command-line programs can be called by the start command

-goto Specifies to jump to the label, after the label is found, the program will process the command starting from the next line

-Set value, Syntax: Set [variable=[string]] set/p variable=[promptstring] set/a expression

2. Common DOS commands

    • Folder Management

-the CD displays the current directory name or changes the current directory.

-MD Create directory.

-Rd Deletes a directory.

-dir displays a list of files and subdirectories in the directory.

-Tree graphically displays the folder structure of the drive or path.

-Path displays or sets a search path for the executable file.

-xcopy to copy 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 files and rename files and directories

-Ren renames the file.

-Replace replaces file.

-attrib Display or change file properties.

-Find search string.

-FC compares two files or two sets of files and shows the difference 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 display, modify TCP/IP settings

-MSG sends a message to the user

-ARP display, modify IP address of LAN-Physical Address map 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

-tasklist Show List of processes

-SC system service Setup and control

-Reg Registry Console tool

-Power settings on the PowerCfg control system

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

3. Windows Batch Common Commands

1 echo and @ echo commands

@ #关闭单行回显

echo off #从下一行开始关闭回显

@echo off #从本行开始关闭回显. General batch The first line is this.

echo on #从下一行开始打开回显

Echo #显示当前是 echo off state or echo on state

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 directory in the current directory

Dir c:/a:-d #显示 C files in the packing directory

Dir c:/b/p #/b display only the file name,/p pagination display

Dir *.exe/s #显示当前目录和子目录里所有的. exe files

4 CD Switch Directory

CD d:/#进入d盘根目录

CD #显示当前目录

CD/D D:SDK #可以同时更改盘符和目录

5 MD Create Directory

MD d:abc #如果 d:a not present, intermediate directories will be created automatically

6 Rd Delete Directory

RD ABC #删除当前目录里的 ABC subdirectory, requires an empty directory

rd/s/q d:temp #删除 d:temp folder and its subfolders and files,/q Quiet mode

7 del Delete file

Del D:test.txt #删除指定文件, cannot be a hidden, system, read-only file

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

D:temp all files in the folder, including hidden, read-only, system files, not including subdirectories

8 ren Rename command

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

9 CLS Clear Screen

Type Display file contents

Type C:boot.ini #显示指定文件的内容, program files will generally show garbled

Type *.txt #显示当前目录里所有. txt file contents

Copy files by copy

Copy c:test.txt d:test.bak copying c:test.txt file to D:, and rename to Test.bak

Copy con test.txt from the screen waiting for input, press CTRL + Z to end the input, 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 1.txt and 2.txt content, save as 3.txt file if 3.txt is not specified, save to 1.txt

Copy test.txt + copying files to yourself, actually modifying the file date

Title of Set cmd window

Title New Headline #可以看到cmd窗口的标题栏变了

Ver Display system version

Label and Vol set volume label

Vol. #显示卷标

Label #显示卷标 and prompt for new volume label

Label C:system #设置C盘的卷标为 System

Pause command

REM and:: Comment command, comment Line does not perform action

Date and time dates and times

Date #显示当前日期 and prompts for a new date, press ENTER to skip the input

date/t #只显示当前日期, do not prompt to enter a new date

Time #显示当前时间, and prompt for New times, press "enter" to skip the input

time/t #只显示当前时间, do not prompt to enter a new time

Goto and: Jump commands

: Label #行首为: Indicates that the row is a label row, and the label row does not perform an action

Goto Label #跳转到指定的标签那一行

Find command (External command)

Find "ABC" C:test.txt a line in the C:test.txt file with an ABC string if not found, the ERRORLEVEL return code will be set to 1

find/i "abc" C:test.txt finds rows with ABC, ignores case find/c "ABC" C:test.txt shows rows with ABC

+ More (external command) display by screen

More c:test.txt #逐屏显示 c:test.txt file contents

Tree Display directory Structure

Tree d: #显示D盘的文件目录结构

& Sequential execution of multiple commands, regardless of whether or not the command was executed successfully

Multiple commands are executed in && order, and no subsequent command is executed when a command that executes the error is encountered

Find "OK" c:test.txt && echo Success if the word "OK" is found, the "success" is displayed, not found

24 | | Executes multiple commands sequentially and does not execute subsequent commands when the correct command is run

Find "OK" c:test.txt | | Echo is unsuccessful if the word "OK" is not found, it will show "unsuccessful" and will not show

25 | Pipeline command

Dir * */s/a | The find/c ". exe" Pipeline command indicates that the DIR command is executed first and the results of its output are executed after 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:test The effect of. txt is the same

> and >> Output redirect command

> Erase the contents of the file before writing

>> append 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

Type C:test.txt >prn screen does not display file contents, turn output to 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 the file and does not display the "File copy succeeded" message, but if the F drive does not exist, an error message will be displayed

Copy c:test.txt F: >nul 2>nul does not display "file copy succeeded" message, and F disk does not exist, also do not display error message

Echo ^^ w ^> ^w>c:test.txt generated file contents of ^w > W ^ and > are control commands, to output them to a file, you must add a ^ symbol in front

< get input from the file, not from the screen

Commands that are typically used to wait for input such as date time label @echo off echo 2016-10-30>temp.txt date <temp.txt del temp.txt so that you can modify the current date without waiting for input

28%0%1%2%3%4%5%6%7%8%9%* command-line parameters passed to batch processing

%0 The batch file itself

The first parameter of%1

%9 nineth parameter

%* all parameters starting from the first argument

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

%~1-Remove quotation marks ("), expand%1

%~F1-Expanded%1 to a fully qualified pathname

%~D1-expand%1 to only one drive letter

%~P1-expand%1 to only one path

%~N1-expands%1 to a file name only

%~X1-Expanded%1 to only one file name extension

%~S1-extended path refers to a short name

%~A1-Extend%1 to file properties

%~T1-date/time when%1 was expanded to a file

%~Z1-Expansion of%1 to file size

%~ $PATH: 1-finds the directory that is listed in the PATH environment variable and expands%1 to the first fully qualified name found. If the environment variable name is not defined or the file is not found, the key combination is expanded to an empty string

Modifier can be combined to achieve multiple results:

%~DP1-Extend%1 to drive letter and path only

%~NX1-Extension%1 to file name and extension only

%~dp$path:1-Finds the% 1 in the directory listed in the PATH environment variable and expands 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 parameters to the file

If Judgment command

If "%1" = = "/A" echo the first parameter is/a

if/i "%1" equ "/A" echo the first parameter is/a,/i for 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 the command extension and delay environment variable expansion

SETLOCAL enableextensions #启用 "command extension"

SETLOCAL disableextensions #停用 "command extension"

SETLOCAL enabledelayedexpansion #启用 "delaying the expansion of environmental variables"

SETLOCAL disabledelayedexpansion #停用 "delaying the expansion of environmental variables"
endlocal #恢复到使用SETLOCAL语句以前的状态 "command extension" by default to enable "Defer environment variable expansion" By default to deactivate batch end system will automatically restore default values 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, the set of the The description is actually a use case process.

Set variable, reference variable can be added before and after the variable name, i.e.% variable name%

Set #显示目前所有可用的变量, including system variables and custom variables

Echo%systemdrive% #显示系统盘盘符. System variables can be referenced directly

Set P #显示所有以p开头的变量, if not a errorlevel=1

Set P=aa1bb1aa2bb2 #设置变量p, and assigns a value = the string after it, i.e. AA1BB1AA2BB2

echo%p% #显示变量p代表的字符串, i.e. AA1BB1AA2BB2

echo%p:~6% #显示变量p中第6个字符以后的所有字符, i.e. AA2BB2

echo%p:~6,3% #显示第6个字符以后的3个字符, i.e. AA2

echo%p:~0,3% #显示前3个字符, i.e. Aa1

echo%p:~-2% #显示最后面的2个字符, i.e. B2

echo%p:~0,-2% #显示除了最后2个字符以外的其它字符, i.e. aa1bb1aa2b

Echo%p:aa=c% #用c替换变量p中所有的aa, which shows C1BB1C2BB2

Echo%p:aa=% #将变量p中的所有aa字符串置换为空, which shows 1BB12BB2

Echo%p:*bb=c% #第一个bb及其之前的所有字符被替换为c, which shows C1AA2BB2

Set p=%p:*bb=c% #设置变量p, assigned to%p:*bb=c%, i.e. C1AA2BB2

set/a p=39 #设置p为数值型变量 with a value of 39

set/a P=39/10 #支持运算符, there are decimals with the de-tail method, 39/10=3.9, go to the tail 3,p=3 set/a P=P/10 #用/A parameter, the variable after = can not be added to the direct reference

set/a p= "1&0″#" and "operation, add quotation marks." Other supported operators see set/?

Set p= #取消p变量

set/p p= Please enter the screen to display "Please enter", and will assign the input string to the variable p note that this can be used to replace the choice command note that the variable in the if and for compound statement is a one-time replacement, such as @echo off

Set P=AAA

If%P%==AAA (

Echo%p%

Set P=BBB

Echo%p%)

The results will show AAA AAA 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 off

SETLOCAL enabledelayedexpansion

Set P=AAA

If%P%==AAA (

Echo%p%

Set P=BBB

Echo!p! ) endlocal

The result will show AAA BBB with several dynamic variables, run set cannot see%cd% #代表当前目录的字符串%date% #当前日期%time% #当前时间%random% #随机整数, between 0~32767%errorlevel% #当前 E The Rrorlevel value%cmdextversion% #当前命令处理器扩展名版本号%cmdcmdline% #调用命令处理器的原始命令行 can view each variable value with the Echo command, such as Echo%time% note%time% accurate to milliseconds, at the batch place Time-lapse processing can be used

The command of the external program is called in the start batch, or the remaining instructions are not executed until the external program is completed.

Another batch command is called in the call batch, otherwise the remaining batch instructions will not be executed. Sometimes applications with the start call error, or call calls

Assoc and Ftype File association

Assoc set the ' File Extension ' Association, associated to ' file type ' ftype set ' file Type ' association, associated to ' Execute program and Parameters ' when you double-click a. txt file, Windows does not open with the Notepad.exe directly judging by the. txt Instead, start by determining that the. txt belongs to the txtfile ' file type ' and then calls the txtfile associated command line Txtfile=%systemroot%system32notepad.exe%1 to modify these 2 associations in Folder Options → file types

Assoc #显示所有 ' File Extension ' Association

Assoc. txt #显示. txt represents the ' file type ' and results are displayed. Txt=txtfile

Assoc. Doc #显示. doc represents the ' file type ', and the results are displayed. Doc=word.document.8

Assoc. exe #显示. exe represents the ' file type ' and results are displayed. Exe=exefile

Ftype #显示所有 ' File Type ' association

Ftype Exefile #显示exefile类型关联的命令行, the results show exefile= "%1"%*

Assoc. Txt=word.document.8 set the. txt as a Word type of document, you can see that the. txt file icon has changed

Assoc. Txt=txtfile Restore Correct Association of. TXT ftype exefile= "%1"%* Correct association of Exefile Recovery

PUSHD and POPD switch the current directory

@echo off C: & CD & MD mp4 #在 C: Create MP4 folder

MD d:mp4 #在 D: Create MP4 folder

cd/d D:mp4 #更改当前目录为 D:mp4

pushd c:mp4 #保存当前目录, and switch the current directory to C:mp4

POPD #恢复当前目录为刚才保存的 D:mp4

For loop command, please control for/? View

For%%i in (c:d: e:f:) do echo%%i sequentially calls each string in the parentheses, performing the command following the do and note%%i, in the batch for statement call parameter with 2%

The default string delimiter is "SPACEBAR", "Tab key", "enter"

For%%i in (*.txt) do find "abc"%%i execute find command for all txt files in the current directory

For/r. %%i in (*.txt) do find "ABC"%%i searches all the. txt files in the current directory and subdirectories for lines containing the ABC string

For/r. %%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 Save the file names of D:mp3 files in mp3 and their subdirectories in D:mp3.txt

FOR/L%%i in (2,1,8) does echo%%i generates a string of 2345678 numbers, 2 is the beginning of a sequence of numbers, 8 is the end, and 1 means 1 per add

The for/f%%i in (' Set ') do echo%%i the output of the set command in a cyclic call, one per line

for/f "Eol=p"%%i in (' Set ') does echo%%i takes the output of the set command, ignoring the 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 name that does not support spaces

for/f "delims="%%i in (d:mp3.txt) do echo%%i displays each file name in D:mp3.txt, one per line, with a name that supports spaces

for/f "Skip=5 tokens=4"%%a in (' dir ') does echo%%a the result of the dir command, skipping the first 5 lines, the remaining row takes the 4th column between each column of the delimiter as the default "space" can be noted in the dir command output of the top 5 lines is no text Name of

for/f "tokens=1,2,3 delims=-"%%a in (' date/t ') do (

Echo%%a

Echo%%b

Echo%%c)

For the output of date/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 specified as-and "space", notice that there is a "space" behind delims=-, where tokens=1,2,3 is used 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, then the columns are given%%b

The PNS subst (external command) maps the disk.

Subst Z:serverd #这样输入z: You'll have access to serverd.

Subst Z:/d #取消该映射 subst #显示目前所有的映时

Archive xcopy (external command) file copy

xcopy d:bin\debug e:bin\debug/s/e/i/y copy d:bin\debug folder, all subfolders and files to E:bin\debug, overwrite existing files

Plus/I indicates that if E: no bin\debug folder is created automatically, there will be a query

Batch-processing Bat command

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.