Bat command Overview

Source: Internet
Author: User
Tags echo command

Bat command Overview
Reading this article requires some basic dos concepts,
Such as: drive letter, file, directory (folder), subdirectory, root directory, current directory
Add /? For more information about the parameters, see the help documentation of Microsoft.
We can see that /? The word "command extension" often appears in help.
The "command extension" is a new function of each command compared to the DOS version of Win98.
In the command test environment, win2000prosp4win98 has too few command functions.

Bytes -------------------------------------------------------------------------------------------------------------------
1 echo and @
Echo control command
@ # Disable single-row echo
Echo off # Close echo from the next line
@ Echo off # disable echo from the beginning of the line. This is the first line of batch processing.
Echo on # enable echo from the next line
Echo # shows whether the current status is echo off or echo on.
Echo. # output a "carriage return line feed", which generally refers to a blank line.
Echo Hello world # output Hello World
"Close echo" means that when you run a batch file, each command in the file is not displayed and only the running result is displayed.
The ECHO is automatically enabled when the batch processing starts and ends.
Bytes -------------------------------------------------------------------------------------------------------------------
2 errorlevel
Program return code
Echo % errorlevel %
After each command is run, you can view the return code in the command line format.
Used to determine whether the command was successfully executed.
The default value is 0. If an error occurs during command execution, the errorlevel is set to 1.
Bytes -------------------------------------------------------------------------------------------------------------------
3 dir
Displays the list of files and subdirectories in a directory.
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 directory in the current directory of drive C
Dir C: // A:-D # display files in the C root directory
Dir D: // MP3/B/P # display files in the D: // MP3 directory on screen. Only the file name is displayed, and the time and size are not displayed.
Dir *. EXE/s
Displays all the. exe files in the current and sub-directories. * is a wildcard,
Represents all the file names. Is there a wildcard? Represents any letter or Chinese Character
For example, C *. * indicates all files starting with C ?. EXE indicates that all files are a local. EXE file.
If the specified directory or file does not exist, the error level is 1.
The dir output of each folder has two subdirectories ..
. Indicates the current directory.. indicates the upper-level directory of the current directory.
Dir. # display files and subdirectories in the current directory
Dir .. # display files and subdirectories in the upper-level directory of the current directory
For other parameters, see Dir /?
Bytes -------------------------------------------------------------------------------------------------------------------
4 CD
Change current directory
CD MP3 # enter the MP3 directory in the current directory
CD .. # enter the parent directory in the current directory
CD // # enter the root directory
CD # display the current directory
CD/d: // MP3 # You can change the drive letter and directory at the same time.
CD // "Documents and Settings" // All Users
The file name contains spaces and can be enclosed in quotation marks.
We recommend that you add quotation marks, because sometimes running errors without quotation marks, such as logon scripts.
If the changed directory does not exist, an error is returned. errorlevel = 1.
Bytes -------------------------------------------------------------------------------------------------------------------
5 MD
Create directory
Md abc # create a subdirectory ABC in the current directory
Md d: // A // B // C # If D: // A does not exist, it will be automatically created.
Bytes -------------------------------------------------------------------------------------------------------------------
6 Rd
Delete directory
Rd ABC # Delete the ABC subdirectory in the current directory, which must be an empty directory
RD/S/q D: // temp # Delete the D: // temp folder and Its subfolders and files. Do not press y to confirm
Bytes -------------------------------------------------------------------------------------------------------------------
7 del
Delete an object
Del D: // test.txt # delete a specified file. It cannot be a hidden, system, or read-only file.
Del *.*
Delete all files in the current directory, excluding hidden, system, and read-only files. Confirm by Y
Del/Q/A/f d: // temp //*.*
Delete all files in the D: // temp folder, including hidden, read-only, and system files, excluding subdirectories.
Del/Q/A/f/s d: // temp //*.*
Delete D: // temp and all files in the subfolders, including hidden, read-only, and system files, excluding subdirectories.
Bytes -------------------------------------------------------------------------------------------------------------------
8 Ren
File rename
Ren 1.txt 2.bak # rename 1.txt to 2.bak
Ren *. txt *. ini changes all the. txt files in the current directory to. ini files.
Ren D: // temp TMP # folder rename is supported
Bytes -------------------------------------------------------------------------------------------------------------------
9 CLS
Clear Screen
Bytes -------------------------------------------------------------------------------------------------------------------
10 type
Show File Content
Type C: // boot. ini # display the content of the specified file. The program file is usually garbled.
Type *. txt contents displays the contents of the TXT file in the current directory.
Bytes -------------------------------------------------------------------------------------------------------------------
11 copy
Copy an object
Copy C: // test.txt D:/copy the C: // test.txt file to D :/
Copy C: // test.txt D: // test. Bak
Copy the C: // test.txt file to D: // and rename it test. Bak.
Copy C ://*.*
Copy C: // all files to the current directory, excluding hidden files and system files
If no target path is specified, the default target path is the current directory.
Copy con test.txt
Wait for the input from the screen, press Ctrl + Z then, and the input content is saved as the test.txt file.
Con stands for screen, PRN stands for printer, NUL stands for empty Device
Copy 1.txt + 2.txt 3.txt
Merge 1.txt and 2.txt contents and save them as 3.txt files.
If 3.txt is not specified, save it to 1.txt.
Copy test.txt +
Copying a file to yourself actually modifies the file date.

Copy command skill Integration
At present, a variety of tools and software that can complete the Copy command emerge one after another. Many people are used to replacing the Copy command with tools such as pctools. However, the advantage of the Copy command cannot be replaced by any software. Because it is an internal command, as long as the computer can be started, it can run without any software support. Therefore, mastering the Copy command is a basic skill to use computers more effectively. Copy /? The Copy command format is described in English.
Copy <drive letter> [<path>] <source file name> [/A |/B] [+ <source file name> [/A |/B] [+...] [drive letter] [path>] [target file name> [/A |/B] [/V] [/Y |/-y]
Where/A is ASCII text, that is, when the file is copied to the first CTRL-Z (EOF).
/B is a binary file, that is, when copying an CTRL-Z (EOF) Like processing other characters.
/V is copied and verified at the same time.
/Y can be directly overwritten if a file with the same name is copied.
/-Y
In case of a file with the same name, check whether the file is overwritten.
The selection items of the Copy command comply with the basic rules of the doscommand, cleverly select different parameters and combine the wildcard "*" or "? ", It will make it easier to complete the operations of various copy commands. The following are some basic applications of the Copy command. All command examples are assumed to be in the current path.
1. File Replication is the most common use of the Copy command. If the file name is the same, it is not in the same directory. If the file name is different, you can create a new file in the same directory.
Ii. File merging: copy file name 1 + file name 2 +... new file name
This method is mostly used for file merging in text formats such as text manuals and text source programs. If a new file name exists, merge all the files to be merged to create a new file. If no new file name exists, merge all the files to be merged to create the first file.
For non-text files, the/B parameter must be added after the first file name.
Example: Copy *. WPS/B New. WPS
3. File Screen Display
(1) text file display copy *. txt con
In actual work, it is often necessary to display a certain type of text files. The Type display command can only display a single file. If you want to display multiple similar files consecutively, it is more convenient to use the copy command.
(2) copy *. WPS/B con
If you only want to view the content of the WPS file, you do not need to enter the WPS. You only need to enter the command in the DOS state to find the required file.
4. edit a simple text file: Copy con file name to be edited
It is troublesome to use other editing tools for simple text (such as autoexec. BAT. Use the preceding command to edit the file. After pressing the F6 key, press enter to create the text file on the disk.
5. Act as a typewriter: Copy con prn
In daily life, we often encounter some documents with simple content but urgently need to form written materials. Run the preceding command to edit the document. Press f6 and press enter again. Send the information directly to the printer without leaving any trace on the disk. This method can also be used to test whether the printer is online.
6. Perform keyboard typing exercises: Copy con> con
If you do not want to retain it for the keyboard exercise only, you can write up to 127 characters in the DoS Status prompt. Once you press enter, the bad command or file name information is displayed, which is inconvenient. If you use the above command, you can exercise with no care until press f6 and then press ENTER or press CTRL-C to terminate.
7. Deleting a file makes it unrecoverable: copy the file name to be deleted by NUL
After you delete a file using the del command, use the pctools software or the undelete command to restore the deleted file. After you use the preceding command to delete important files, you cannot use any tool to restore them. This enhances the confidentiality of deleted files.
8. Change the file creation date and time: copy the file name to be rescheduled + ,,
In order to avoid accidental deletion of some application system files, the date of file creation is often set to a unified date. The system date is set to the required date and time by using the date and time commands, and then use the preceding commands, you can change the file creation date and time. This command applies only to a single file, but cannot be modified in batches with the wildcard. Otherwise, the first file in the batch will be deleted.
IX. File printing: copy file name prn
Mostly used for direct printing of text files.
10. Single-drive file replication: If only single-drive files are available, or for some reason, only single-drive file replication is allowed, first in coms, define only the single drive type used (for example, disk A is 5 1/4 1/2 MB or disk 3 MB is none) and then run the copy A: file name B: Command, insert the disk as instructed on the screen to copy files on a single drive.
Bytes -------------------------------------------------------------------------------------------------------------------
12 Title
Set the title of the CMD window
Title New title # The title bar of the CMD window has changed
Bytes -------------------------------------------------------------------------------------------------------------------
13 ver
Show System Version
Bytes -------------------------------------------------------------------------------------------------------------------
14 label and vol
Set the volume label
Vol # display the volume label
Label # displays the volume label and prompts you to enter a new volume label.
Label C: system # Set the volume of drive C to System
Bytes -------------------------------------------------------------------------------------------------------------------
15 pause
Pause command
When you run this command, the following message is displayed:
Press any key to continue...
It is generally used to clearly view the content displayed on the screen.
Bytes -------------------------------------------------------------------------------------------------------------------
16 REM and ::
Comment command
The comment row does not perform the operation.
Bytes -------------------------------------------------------------------------------------------------------------------
17 Date and Time
Date and Time
Date # displays the current date, and prompts you to enter a new date. Press "enter" to skip the input.
Date/T # only display the current date, do not prompt to enter a new date
Time # displays the current time, and prompts you to enter a new time. Press "enter" to skip the input.
Time/T # Only the current time is displayed, and a new time is not prompted
Bytes -------------------------------------------------------------------------------------------------------------------
18 Goto and:
Jump command
: Label # The first line is: indicates that this line is a label line, and the label line does not perform operations
Goto label # Jump to the row of the specified tag
Note:
Microsoft Windows XP [version 5.1.2600]
The Hops of Goto and: are limited by the bat script "statement order.
Goto label statement. The following statement is not executed!
But! Goto to the specified: label,
If the label module does not contain other goto instructions, interruptions, or concluding remarks,
Then, any command lines after the label module, including other labels that are not redirected, will continue to be executed!
This will lead to an endless loop of scripts!
Example 1 ---------------------------
CLS
If exist C:/*. * GOTO 1
If exist D:/*. * goto 2
: 1
Echo condition 1 is set. Execution: 1
: 2
Echo condition 2 Established: 2
: 3
Echo Condition 3 is set and is being executed: 3
Example 2 ------------------------------
CLS
If exist C:/*. * GOTO 1
If exist D:/*. * goto 2
: 2
Echo condition 2 Established: 2
: 1
Echo condition 1 is set. Execution: 1
: 3
Echo Condition 3 is set and is being executed: 3
Example 3 ------------------------------
CLS
If exist C:/*. * GOTO 1
If exist D:/*. * goto 2
: 3
Echo Condition 3 is set and is being executed: 3
: 2
Echo condition 2 Established: 2
: 1
Echo condition 1 is set. Execution: 1
------------------------------
The results of the three examples are different.
4. endless loop example:
------------------------------
CLS
: 1
Echo condition 1 is set. Execution: 1
: 2
Echo condition 2 Established: 2
: 3
Echo Condition 3 is set and is being executed: 3

If exist C:/*. * GOTO 1
If exist D:/*. * goto 2
-------------------------------------
This dead loop script is not highly configured and should not be tested.
Pending)
19 find (external command)
SEARCH Command
Find "ABC" C: // test.txt in C: // test.txt
If the row containing the ABC string cannot be found in the file, set errorlevel to 1.
Find/I "ABC" C: // test.txt
Searches for rows containing ABC, regardless of Case sensitivity.
Find/C "ABC" C: // test.txt
Display the number of rows containing ABC
Bytes -------------------------------------------------------------------------------------------------------------------
20 more (External commands)
Display On Screen
More C: // test.txt # display the content of the C: // test.txt file on screen
Bytes -------------------------------------------------------------------------------------------------------------------
21 tree
Show directory structure
Tree D: // # display the directory structure of the D Drive
Bytes -------------------------------------------------------------------------------------------------------------------
22 &
Execute multiple commands in sequence, regardless of whether the command is successfully executed.
C: & CD // & DIR/W
It is equivalent to writing the following three lines of commands to one line.
C:
CD/DIR/W
Bytes -------------------------------------------------------------------------------------------------------------------
23 &&
Multiple commands are executed sequentially. If an error occurs, the subsequent commands are not executed.
F: & CD // & dir> C: // test.txt
Note that if the drive F does not exist, the subsequent two Commands will not be executed.
Find "OK" C: // test.txt & Echo successful
If "OK" is found, "success" is displayed.
Note:
& If the previous command fails to be executed, only the last "one command" will not be executed.
& Regardless of the previous result, the subsequent command continues to run. It is equivalent to line feed.
If & and & appear in the same command line, & the previous execution fails, only the last one will not be executed, & and then the execution will continue.
Tskill taskmgr & echo article 1 & echo article 2
If "Process Manager" is completed successfully, the following message is displayed:
Article 1
Article 2
If the "Process Manager" fails, the following message is displayed:
Process not found: taskmgr
Article 2
Bytes -------------------------------------------------------------------------------------------------------------------
24 |
Execute multiple commands in sequence. When the correct command is executed, the subsequent commands will not be executed.
F: | E:
If you have a drive, you will not be able to access the drive.
Find "OK" C: // test.txt | echo failed
If "OK" is not found, "unsuccessful" is displayed. If "OK" is found, it is not displayed.
Bytes -------------------------------------------------------------------------------------------------------------------
25 |
Pipeline command
The execution result of the previous command is output to the next command.
Dir *. */S/A | find/C ". EXE"
The pipeline command indicates that the Dir command is executed first, and the find command after the output result is executed
Command Line result: output the number of. EXE files in the folder and all subfolders.
Type C: // test.txt | more
This is the same as more C: // test.txt.
Tips
If some commands do not support the/y parameter, you can enter y manually:
Echo y | XXXX xx
)
Bytes -------------------------------------------------------------------------------------------------------------------
26> and>
Output redirection command
> Clear the original content of the file and write it again.
> Append the content to the end of the file without clearing the original content.
Output the content originally displayed on the screen to a specified file.
If the specified file does not exist, the file is automatically generated.
Echo Hello World> C: // test.txt
Generate the C: // test.txt file with the content Hello World
This format is widely used in batch files and can generate temporary files such as. Reg. bat. vbs.
Type C: // test.txt> prn
If no file content is displayed on the screen, the system redirects to the printer.
Echo Hello World> con
Display Hello World on the screen. In fact, all outputs are by default> con
Copy C: // test.txt F:> NUL
Copy the file, and the message "file copied successfully" is not displayed,
However, if the drive F does not exist, an error message is displayed.
Copy C: // test.txt F:> NUL 2> NUL
The message "file copied successfully" is not displayed,
If the drive F does not exist, no error message is displayed.
Echo ^ w ^> ^ W> C: // test.txt
The generated file content is ^ W> W
Note ^ and> are control commands. to output them to files, you must add a ^ symbol before them.
Bytes -------------------------------------------------------------------------------------------------------------------
27 <
Get input information from the file instead of from the screen
It is generally used for date Time Label and other commands that need to be input
@ Echo off
Echo 2005-05-01> temp.txt
Date <temp.txt
Del temp.txt
In this way, you can directly modify the current date without waiting for the input.
Bytes -------------------------------------------------------------------------------------------------------------------
28% 0% 1% 2% 3% 4% 5% 6% 7% 8% 9% *
Parameters passed by the command line to the batch processing
% 0 batch processing file itself
% 1 first Parameter
% 9 ninth Parameter
% * All parameters starting with the first parameter
Create test. bat in the C root directory. The content is as follows:
@ Echo off
Echo % 0
Echo % 1
Echo % 2
Echo % *
Run CMD and enter C: // test. Bat "/a"/B/C/D.
We can see the meaning of each parameter.
Modify test. BAT as follows:
@ Echo off
Echo % 1
Echo % ~ 1
Echo % 0
Echo % ~ F0
Echo % ~ D0
Echo % ~ P0
Echo % ~ N0
Echo % ~ X0
Echo % ~ S0
Echo % ~ A0
Echo % ~ T0
Echo % ~ Z0
Run CMD and enter C: // test. Bat "/a"/B/C/D.
Result:
"/"
/
C: // test. bat
C:/test. bat
C:
Test
. Bat
C:/test. bat
-- ------

136
Refer to call /? Or /? See the meaning of each parameter
Note that the date and size of the file can be compared here.
Echo load "% 1" "% 2"> C: // test.txt
The generated file content is load "% 1" "% 2"
In the batch file, output the command line parameters to the file in this format
Bytes -------------------------------------------------------------------------------------------------------------------
29 if
Judgment command
If "% 1" = "/a" Echo, the first parameter is/.
If/I "% 1" equ "/a" Echo, the first parameter is/
/I indicates case-insensitive. EQU and = are the same. For other operators, see if /?
If exist C: // test. Bat echo exists in the C: // test. BAT file.

If not exist C: // windows (
ECHO does not exist C: // Windows Folder
REM has multiple commands that can be enclosed in parentheses and are called "Compound statements"
The space in front of the REM row is used to look clear.
)

If exist C: // test. BAT (
ECHO has c: // test. bat
) Else (
ECHO does not exist C: // test. bat
Rem else indicates "otherwise". The subsequent command is executed when the result is false.
)
Bytes -------------------------------------------------------------------------------------------------------------------
30 setlocal and endlocal
Set "command extension" and "Deferred Environment Variable Expansion"
Setlocal enableextensions # enable "command extension"
Setlocal disableextensions # disable "command extension"
Setlocal enabledelayedexpansion # enable "Deferred Environment Variable Expansion"
Setlocal disabledelayedexpansion # disable "Deferred Environment Variable Expansion"
Endlocal # restore to the status before using the setlocal statement
The "command extension" is enabled by default.
"Extended environment variables" is disabled by default.
The default value is automatically restored after the batch processing is completed.
You can modify the Registry to disable the "command extension". For more information, see CMD /? .
Therefore, if you use a program with the "command extension", we recommend that you add
Setlocal enableextensions and endlocal statements,
To ensure that the program runs correctly on other systems
"Deferred Environment Variable Expansion" is mainly used for the IF and for statement,
In the description of set, there are actually examples
Bytes -------------------------------------------------------------------------------------------------------------------
31 set
Set Variables
You can add % before and after the variable name, that is, % variable name %.
Set # display all currently available variables, including system variables and custom Variables
Echo % systemdrive % # displays the system disk drive letter. System variables can be directly referenced.
Set P # Show all variables starting with P. If one variable does not exist, set errorlevel = 1.
Set P = aa1bb1aa2bb2 # Set the Variable P and assign the value = The following string, that is, aa1bb1aa2bb2
Echo % P % # display the string represented by Variable P, that is, aa1bb1aa2bb2
Echo % P :~ 6% # display all the characters after the 6th characters in Variable P, that is, aa2bb2
Echo % P :~ 6, 3% #3 characters after 6th characters are displayed, that is, Aa2
Echo % P :~ 0, 3% # display the first three characters, namely aa1
Echo % P :~ -2% # display the last two characters (B2)
Echo % P :~ 0,-2% # display other characters except the last two characters, that is, aa1bb1aa2b
Echo % P: AA = C % # Replace all AA in Variable P with C, that is, c1bb1c2bb2 is displayed.
Echo % P: AA =%# replace all AA strings in Variable P with null, that is, 1bb12bb2 is displayed.
Echo % P: * BB = C % # The first BB and all its previous characters are replaced with C, that is, c1aa2bb2 is displayed.
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 operators. If there is a decimal number, use the Tail removal method. The value 39/10 = 3.9 indicates 3, P = 3.
Set/a p = P/10 # When the/a parameter is used, the variable after = can be directly referenced without %
Set/a p = "1 & 0" # "and" operations, quotation marks are required. For other supported operators, see set /?
Set P = # cancel the P variable
Set/P = enter
"Enter" is displayed on the screen, and the input string is assigned to the variable p.
Note that this line can be used to replace the choice command
Note that all variables are replaced in the IF and for compound statements at one time, as shown in figure
@ Echo off
Set P = aaa
If % P % = aaa (
Echo % P %
Set P = bbb
Echo % P %
)
The result is displayed.
Aaa
Aaa
Because all % P % has been replaced with AAA when reading the if statement
Here "replace", in /? The help refers to "expansion" and "Environment Variable Expansion"
You can enable "Deferred Environment Variable Expansion! To reference variables, that is! Variable name!
@ Echo off
Setlocal enabledelayedexpansion
Set P = aaa
If % P % = aaa (
Echo % P %
Set P = bbb
Echo! P!
)
Endlocal
The result is displayed.
Aaa
Bbb
There are several dynamic variables that cannot be seen when running set.
% Cd % # represents the string of the current directory
% Date % # current date
% Time % # current time
% Random % # random integer, ranging from 0 ~ 32767
% Errorlevel % # current errorlevel Value
% Cmdextversion % # current command processor extension version number
% Worker line % # Call 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, which can be used when batch processing requires delay.
Bytes -------------------------------------------------------------------------------------------------------------------
32 start
Execute the remaining commands only after the external program is completed.
Start explorer D: // open disk D by calling the graphic interface
@ Echo off
CD/d % ~ Dp0: % ~ Dp0 indicates the identifier and identifier of the current file. In this example, this batch must be in the same folder as dancing .regw.patcher.exe.
Regedit/s audition. Reg
Start patcher.exe
Without the start command, there will be a dark cmd window later when the "audition" is running.
Bytes -------------------------------------------------------------------------------------------------------------------

33. Call another batch processing command in the call batch. Otherwise, the remaining batch processing commands will not be executed. Sometimes some applications use start to call wrong commands -------------------------------------------------------------------------------------------------------------------

34 choice (external command) Select a command to let the user enter a character, and then choose to run different commands, the return code errorlevel is 1234 ...... In Win98, choice.comwin2000prois not found. From win98, choice.exe choice/n/c y/T 5/d y> NUL latency 5 seconds. Below is a choice statement example. @ echo offrem is used in win2000pro, chioce.com file copied from Win98 choice/C: abc aaa, BBB, cccif errorlevel 3 goto cccif % errorlevel % = 2 goto bbbif errorlevel = 1 goto aaarem must first determine the return code with a high value REM. There are three ways to judge the errorlevel value, sometimes some writing method is not easy to use. Another method can be used: REM to directly run chioce, which is equivalent to running choice/C: yn: aaaecho aaago to end: bbbecho bbbgoto end: cccecho cccgoto end: End

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.