Use batch files (also known as batch programs or scripts) to simplify daily or repetitive tasks. Batch file is a form of text file, which contains one or more commands, its file extension is .bat or .cmd. At the command prompt, the name of the batch file is processed, and CMD.exe will run them one by one in the order of each command in this file.
Any command can be included in the batch file. Some commands, such as for, goto, and if commands, etc. that allow you to process the commands in the batch file. For example, if commands executes commands based on the results of the conditional statement. Other commands allow you to control the input and output and call other batch files.
If there is no error, most applications return standard error code 0; if an error, returns 1 (or greater). Please refer to the application to help documentation to determine the meaning of a particular error code.
For more information on batch file operations, see the following topics:
Use batch parameters
Batch parameters can be used anywhere in the batch file to extract information about environment settings.
CMD.exe provides batch parameters extended variable (% 0 to% 9). When using batch parameters in the batch file,% 0 will be replaced by the batch file name, and% 1 to% 9 will be replaced by the corresponding parameters typed in the command line. To access the parameters exceeded by% 9, you must use the Shift command. For more information on Shift commands, see Shift. % * Batch parameters are wildcard references that passed all parameters (not included% 0) in the batch file.
For example, to copy the contents in Folder1 to Folder2, where% 1 and% 2 will be replaced by values Folder1 and Folder2, type as follows in batch file mybatch.bat:
XCOPY% 1 /*.*% 2
To run this file, type:
Mybatch.bat c: / folder1 d: / folder2
This is equivalent to the following in the batch file is equivalent:
Xcopy C: / Folder1 /*.* D: / folder2
You can also use the editor in the batch parameters. The editor uses the current drive and directory information to extend the batch parameters as part or complete files or directory names. To use an editor, type a percentage (%) character, followed by the waveform symbol (~) character, then type the appropriate editor (ie% ~ modifier).
The following table lists the editors that can be used in the extension.
Editor description
% ~ 1
Expand% 1 and delete any peripheral quotation marks ("").
% ~ F1
Extend% 1 to a fully qualified path name.
% ~ D1
Extend% 1 to the drive letter.
% ~ P1
Expand% 1 to the path.
% ~ N1
Extend% 1 to the file name.
% ~ x1
Extend% 1 to the file extension.
% ~ S1
The extended path only contains short names.
% ~ A1
Extend% 1 to file properties.
% ~ T1
Extend% 1 to file dates / time.
% ~ Z1
Extend% 1 to file size.
% ~ $ PATH: 1
Search the directory listed in the PATH environment variable and extend the% 1 to the full qualified name of the first found directory. If the environment variable name is not defined, or if the file is not found, this editor extends into an empty string.
The following table lists the possible combinations of editors and qualifiers that can be used to obtain composite results:
Editor description
% ~ DP1
Extend% 1 to the drive letter and path.
% ~ NX1
Extend% 1 to the file name and extension.
% ~ DP $ PATH: 1
Search% 1 in the directory listed in the PATH environment variable and extend to the first-found directory drive letter and path.
% ~ ftza1
Extend% 1 to an output line similar to DIR.
note
In the above example, other batch parameter values can be used to replace% 1 and PATH. % * The editor is the only editor that can represent all the parameters passed in the batch file. This editor cannot be used in combination with% to the editor. % ~ Syntax must be terminated by valid parameter values.
Batch parameters cannot be used in the same way as using environment variables. You cannot search and replace the value, or check the sub-string. However, the parameters can be assigned to the environment variable and the environment variable can be operated.
Use of filters
The filter command is a command combined with the re-directional tag (|) command, which is included in the read command, converts the input and writes the output command. The filter command can help you sort, view, and select some command output.
The filter command can divide, rearrange, and extract some information operations. The table below
Windows Server 2003
Filter commands available in the family.
Command Description More Displays the contents of a file or an output of a command in a command prompt window. Find searches for the characters you specify in the file and command output. SORT is arranged in alphabetical order and command output.
To send the input from the file to the filter command, use less than the number (<). If you want to get the filter command from another command, use the duct (|).
Use more command
The more command is displayed in one command prompt window or the output of a command. For example, you want to display a file named list.txt every time a command prompt window, type:
More A command prompt window containing information appears, then appears at the bottom of the command prompt window - More - prompt. To move forward to the next command prompt window, press any key on the keyboard (except for PAUSE). To stop the command and do not view more, press Ctrl C. When using a command to generate multiple output command prompt windows, you can use more commands. For example, assume that you want to see the directory tree on your hard drive. If the "Command Prompt" window does not display all the directories, use the Tree command with the pipe character (|) and more command, as shown in the following example: Tree C: / | More The first output command prompt window generated by the Tree command appears, followed by - more - prompt. The output is paused until the user presses any key on the keyboard (except for PAUSE). Use the find command The Find command searches for the specified string or text in the file. Cmd.exe displays all rows that match the string or text specified in the Command Prompt window. You can use the Find command as the filter command or as a standard command. For more information on how to use Find as a standard command, see Find. To use the FIND command as the filter command, you must contain less than the number (<) and the string or text to search. By default, Find search will be case sensitive. For example, the following command looks for all "Pacific Rim" strings in trade.txt: Find "pacific rim" The output does not include any string of "Pacific RIM". The output includes only the "Pacific RIM" string of all letters. To save the output of the Find command instead of the command prompt window displays the output, type the name of the file greater than the number (>) and the file to store the output content. For example, the following command looks for the "pacific rim" string existing in the file trade.txt, and saves the results in the nwtrade.txt file: Find "pacific rim" Use the sort command The sort command is arranged in alphabetical order or the output of the command. For example, the following command is sorted on the content named list.txt, and the sort result is displayed in the command prompt window: sort In this example, the sort command sorts the row of the list.txt file and displays the result, but does not change the file. To save the output of the sort command instead of the display output, type larger than the number (>) and file name. For example, the following command is sorted in alphabetical order on the line of the list.txt file and store the result in the alphlist.txt file: Sort To sort the command output, type the command, type the pipe character (|), type Sort (ie, command | sort). For example, the following commands are sorted in alphabetical sequences for rows including strings "Jones" (ie, Find command output): Find "Jones" Mailst.txt | Sort Redirect operator using command You can use the redirect operator to redirect the command input and output data from the default location to a different location. The position of the input or output data stream is the handle. The following table will list the available handles. The number code of the handle handle Description stdin0 Keyboard Enter STDOUT1 Output to Command Prompt Window StderR2 Error Output to Command Prompt Window Undefined3-9 These handles are defined by the application, and are specific. Numbers 0 to 9 represent the top 10 handles. You can run the program using the command cmd.exe run and redirects any of the 10 handles before the program. To specify the handle you want to use, you can type the number of the handle in front of the redirect operator. If the handle is not defined, the default To specify a redirection to an existing handle, use the (&) character, back to the hemnock number (eg, & handle #). For example, the following command can redirect the handle 2 (ie, STDERR) to the handle 1 (ie stdout): 2> & 1 The following table lists an operator that can be used to redirect input and output data streams. Redirect Operator Description> Write the command output into a file or device (e.g., a printer) instead of writing in the command prompt window or handle. By default, the command input (ie, the STDIN handle) can be sent from the keyboard to cmd.exe, and then the command output (ie, the Stdout handle) is sent to the command prompt window by cmd.exe. Redirect input (<) To redirect your keyboard to a file or device, use the Sort The content of file.txt will be displayed in the command prompt window in a manner in the alphabetical list. 0 is the default handle of the Redirection output (>) Almost all commands send the output to the command prompt window. Even if you send the output to the drive or printer, the message and prompt will be displayed on the command prompt window. To redirect the output from a command prompt window to a file or device, use> operator. This operator can be used in many commands. For example, to redirect DIR output to dirlist.txt, type: Dir> dirlist.txt If Dirlist.txt does not exist, cmd.exe will create the file. If dirlist.txt exists, cmd.exe will use the output of the DIR command to replace the information in the file. To run the netsh routing dump command, then send the output to ROUTE.CFG, type: Netsh Routing Dump> C: /Route.cfg > Operators can open just write The specified file of the access attribute. So, you cannot read files using this operator. For example, if you use a redirection> & 0 launch program, all the operations that try to write handle 1 will fail because the handle 0 is initially Read-only Access is opened. note 1 is the default handle of the redirection output operator. Copy handle Redirection Operation & You can copy the output or input from a specified handle to another specified handle. For example, to send DIR output to file.txt and send an error output to file.txt, type: Dir> C: /file.txt 2> & 1 When copying the handle, you can copy all the features of the original state. For example, if a handle has just write Access the properties, all copies of the handle have just write Access properties. Can't have one Read-only Handle of access attribute copy to another just write The handle of the access attribute. Use & operators to change input and copy To use the redirected input operator (<) with the replication operator (&), the specified file must already exist. If the input file exists, cmd.exe will Read-only The file is opened, and then the characters contained in the file are sent to this command (as in the keyboard input). If the handle is specified, cmd.exe copies the specified handle into the existing handle. For example, to open File.txt in a manner that reads (ie, stdin) in handle 0, type: To open file.txt, and send the output to the command prompt window (ie, stdout) after the content is sorted, type: Sort To find file.txt, then redirect the handle 1 (ie, stdout) and handle 2 (ie, stderr) to search.txt, type: Findfile file.txt> Search.txt 2 <& 1 To copy the user-defined handle 3 by entering the read (ie, stdin) by handle 0, type: <& 3 Use & operator to redirect output and copy If the output is redirected to the file and the existing file name is specified, cmd.exe will just write Way open files and override the file content. If the handle is specified, cmd.exe copies the file into the existing handle. To copy the user-defined handle 3 to the handle 1, type: > & 3 To redirect all outputs including the handle 2 (ie, stderr) to the handle 1 (ie, stdout), then redirect the output to Output.log, type: ipconfig.exe >> Output.log 2> & 1 Use >> Redirection Operator to add output To add any information that the output is added to the end of the file without losing any information existing in the file, use two consecutive greater than the number (ie >>). For example, the following command can append a list of directories generated by the DIR command to the dirlist.txt file: Dir >> Dirlist.txt To add the output of the NetStat command to the end of TCPinfo.txt, type: NetStat >> TCPINFO.TXT Use a pipe operator (|) The pipe operator (|) can extract an output of a command (by default, stdout), then import it into the input of another command (default is stdin). For example, the following command will be classified: Dir | sort In this example, two commands will be started at the same time, but the Sort command will be suspended until it receives the output of the DIR command. The sort command uses the output of the DIR command as an input, and then sends the output to the handle 1 (ie stdout). Combined commands with redirective operators Custom commands can be created by combining a filter command with other commands and file names. For example, you can use the following command to store file names containing "log" strings: DIR / B | Find "Log"> Loglist.txt The output of the DIR command is sent via the FIND filter command. File names containing string "log" are stored as file name list (for example, Netshconfig.log, logdat.svd, and mylog.bat) in file loglist.txt. To use multiple filters in the same command, use a pipe (|) to separate the filter. For example, the following command will search for each directory on the C drive to find the file name containing the "log" string, and a screen is displayed each time in the command prompt window: DIR C: / / S / B | Find "LOG" | More Use the pipe (|) to direct the CMD.exe to send the DIR command output by the FIND filter command. The Find command selects only the file name containing the string "log". The more command can display the file name selected by the FIND command (a screen is displayed each time in the Command Prompt window). For more information on the filter command, see Using Filters For more information on the commands that can be used in batch files, click the following command: Call Choice Echo endlocal for goto if Pause Rem setlocal shift