SoFunction
Updated on 2025-04-14

It’s easy to learn batch processing! Another good insight page 3/3


for /L [%% | %]variable in (start,step,end) do command [command-parameters]
A set is a series of numbers divided by step size, from beginning to end. In this way, (1,1,5) will generate sequence 1 2 3 4 5, and (5,-1,1) will generate sequence  (5 4 3 2 1).
File parsing
for /F ["options"] [%% | %]variable in (filenameset) do command [command-parameters]
for /F ["options"] [%% | %]variable in ("literal string") do command[command-parameters]
for /F ["options"] [%% | %]variable in ('command') do command [command-parameters]
Or, if the usebackq option appears:
for /F ["options"] [%% | %]variable in (filenameset) do command [command-parameters]
for /F ["options"] [%% | %]variable in ('literal string') do command [command-parameters]
for /F ["options"] [%% | %]variable in (`command`) docommand [command-parameters]
The filenameset parameter specifies one or more file names. Each file is opened, read, and processed before continuing to the next file in filenameset.
The process consists of reading a file, dividing it into separate lines of text, and then parsing each line into zero or more tokens. Then call the for loop body using the set of variable values ​​(or values) set to find one or more token strings. By default, /F passes the first blank separator for each line of each file.
Skip the empty line. The default parsing behavior can be overridden by specifying the optional "options" parameter. This is a reference string that contains one or more keywords to specify different parsing options. The keywords are:
Keyword Description
eol=c Specify the end-of-line comment character (only one character)
skip=n Specifies the number of lines skipped at the beginning of the file.
delims=xxx Specifies the delimiter set. This replaces the default separator set for spaces and tabs.
tokens=x,y,m-n Specifies to pass the token from each line to each repeated body. This will cause other variable names to be assigned. The m-n format is a range that specifies the token from mth to nth. If the last character in the token = string is an asterisk, the additional variable will be assigned and the remaining text will be received on the line after parsing the last token.
usebackq Specifies that the right quote string is executed as a command. The single quote string is a literal string command. You can use double quotes to include the filename in filenameset.
Variable replacement
In addition, the replacement modification program for variable reference has been enhanced. The following optional syntax is now available (for any variable I):
Variable (using the modified program) Description
%~I Expand %I deleted any quotation marks around  (")
%~fI Expand %I to a fully qualified pathname
%~dI Only expand %I to drive letter
%~pI only expand %I to the path
%~nI only expand %I to file name
%~xI only expand %I to file extension
%~sI Expand the path to contain only short names
%~aI Expand %I to file properties of the file
%~tI Expand %I to the date/time of the file
%~zI Expand %I to file size
%~$PATH:I Search for the directory listed in the PATH environment variable, and expand %I to the first to find all qualified names for the result. If the environment variable name is not defined, or the file is not found after searching, the modifying program will be extended to an empty string.
Modifying programs can be merged to obtain complex results:
Variables (using merged modification program) Description
%~dpI only expand %I to drive letter and path
%~nxI only expand %I to file name and extension
%~fsI Expand %I to the full pathname that contains only short names
%~dp$PATH:I Search for %I in the directory listed in the PATH environment variable, and expand to the first drive letter and path to find the result.
%~ftzaI Extend %I to output lines similar to dir
Notice
In the above example, %I  and PATH can be replaced by other valid values. Terminate %~ syntax with valid for variable names.
Using uppercase variable names (such as %I) can make the code more readable and avoid confusion with case-insensitive modification programs.
Shift
Change the location of replaceable parameters in the batch file.
shift 
After enabling the command extension (the default setting in Windows 2000), the shift command supports the /n switch, which notifies the command that it starts to change at the nth parameter, n can be any value from 0 to 8. For example,
SHIFT /2
Change %3 to %2, change %4 to %3, etc., while %0 and %1 remain unchanged.
Filter command
Filter commands can help you sort, view, and select some command output results.
Pass information through filter command
Filter commands can divide, rearrange and extract some information operations through. Windows 2000 has three filter commands:
The more command displays one screen of file content or command output at a time.
The find command searches for specified characters in the file and command output.
The sort command arranges the file and command output alphabetically.
To send input from a file to a filter command, use the less than symbol (<). If you want the filter command to get input from other commands, use the pipeline (|).
Use the more command to control the screen display
The more command displays the content of the file or the command output one screen at a time. For example, the following more command displays the content of the file one screen at a time:
more <  
After the information is displayed on one screen, the word "More" will appear. To continue to display the next screen, press any key on the keyboard. To stop the command and not view the details, press the CTRL+C key.
If you use the command that generates multi-screen output, more will be very useful. For example, suppose you want to view the directory tree of the hard disk. If Windows 2000 cannot display all directories in one screen, please use the tree command with the pipeline number (|) and more commands, as shown in the following example:
tree c:\ | more 
The first screen output of the tree command is displayed, followed by the word "More". Windows 2000 pauses until the user presses any key on the keyboard (except the PAUSE key).
Search text using the find command
The find command searches for the specified text in one or more files. Windows 2000 displays each line containing the text. The find command can be used as a filter command or as a standard Windows 2000 command. For information on using find as a standard Windows 2000 command, click find in the Related Topics list.
To use find as a filter command, include the file name smaller than the symbol (<) and searched. When entering a file name, remember to search to be case sensitive. For example, the following command looks for all "Pacific Rim" strings in the file:
find "Pacific Rim" <  
To save the output of the find command instead of displaying the output, use the greater than (>) and the file name to store the output. For example, the following command looks for all "Pacific Rim" strings in the file and save the result in the file:
find "Pacific Rim" <  >  
Sort text files
The sort command arranges the output of text files or commands alphabetically. For example, you can use the following command to sort the contents of a file and display the results on the screen:
sort <  
In this example, the sort command sorts the lines of the file and displays the results, but does not change the file. To save the output of the sort command instead of displaying the output, include the greater than (>) and file name in the command. For example, you can use the following command to sort the lines of a file alphabetically and save the results to the file:
sort <  >  
To sort the output of a command, type a command followed by the pipeline (|) and sort commands. For example, the following command sorts the output results of the find command:
find "Jones"  | sort 
When typing this command, Windows 2000 lists the lines in which "Jones" appear alphabetically.
Merge command with redirector
Filter commands, other commands, and file names can be merged to generate custom commands. For example, you can use the following command to store the file name containing the "LOG" string:
dir /b | find "LOG" >  
Windows 2000 sends the output of the dir command through the find filter command and stores the file name containing the string "Log" in the file. Store the results as a list of file names (e.g., , and ).
To use multiple filters in the same command, use a pipeline (|) to separate the filters. For example, the following command searches each directory on the C disk to find the file name containing the "Log" string, and displays one screen at a time:
dir c:\ /s /b | find "LOG" | more 
Because of using pipeline (|), Windows 2000 sends the output result of the dir command through the find command. The find command only selects the file name containing the string "Log". The more command displays the file name selected by the find command on a screen each time.
More
One output screen is displayed at a time. This command is usually used to view long files. This command can be used alone, or it can be used to control the output of other commands, such as the type command. A more prompt appears when the display fills the available viewing area, and the user can enter many commands to control how the rest of the file is viewed.
command name | more [/c] [/p] [/s] [/tn] [+n]
more [[/c] [/p] [/s] [/tn] [+n]] < [drive:][path] filename
more [/c] [/p] [/s] [/tn] [+n] [files]
parameter
[drive:][path] filename
Specifies the file to display.
command name
Specifies the command to display its output.
/c
Clear the screen before displaying the page.
/p
Extended page breaks.
/s
Change multiple blank lines to one blank line.
/tn
Change tabs to n spaces
+n
Displays the first file starting with the line specified by n.
files
Specifies the list of files to display. Separate file names with spaces.
More Subcommand
The following command is accepted under the more prompt (-- More --).
Keyword Operation
space Show the next page.
ENTER Displays the next line.
F Show the next file.
q Exit.
? Show available commands.
= Show line number.
P n Show the following n lines.
S n Skip the line below n .
Find
Search for a specified text string in one or more files.
When the specified file is searched, find will display all lines containing the specified string.
find [/v] [/c] [/n] "string" [[drive:][path]filename[...]] 
Parameters
/v 
Displays all lines that do not contain the specified string.
/c 
Shows only the number of lines containing the specified string.
/n 
Place the file line number at the beginning of each line.
/I 
Specifies that searches are case-insensitive.
"string" 
Specifies the character group to search for. The text of string must be included in quotes.
[drive:][path] filename 
Specifies the location and name of the file in which you want to search for the specified string.
Sort
Read input, sort data, and write results to screens, files, and other devices.
sort [/r] [/+n] [/m kilobytes] [/l locale] [/rec characters] [[drive1:][path1]filename1] [/t [drive2:][path2]] [/o [drive3:][path3]filename3]
[command |] sort [/r] [/+n] [/m kilobytes] [/l locale] [/rec characters] [[drive1:][path1]filename1] [/t [drive2:][path2]] [/o [drive3:][path3]filename3]
Parameters
/r
Reverse the sorting order, that is, sort from Z to A, and then sort from 9 to 0.
/+n
Specify the character position number n, sort starts each comparison here. For example, /+3 means that each comparison begins at the third character of each line. Lines with less than n characters are sorted before other lines. By default, comparison begins at the first character of each line.
/m kilobytes
Specifies the amount of main memory used for sorting, in kilobytes (KB). The minimum memory value used is always 160 KB. If the memory size is specified, the exact amount of memory specified (but at least 160 KB) will be used for sorting regardless of how much main memory is available.
If the input and output are all files, when there is no specified size, the default maximum memory size is 90% of the available main memory, otherwise it is 45% of the main memory. The default settings usually yield the best performance.
/l locale
Instead of the character sorting order defined by the system's default locale settings; that is, the language and "country (region)" selected when installing Windows 2000. Currently, the only alternative option for the default locale is the "C" locale, which is faster than natural language sorting and sorts characters according to binary encoding.
/rec characters
Specifies the maximum number of characters in the line of the record or input file (the default is 4096, and the maximum is 65535).
[drive1:][path1]filename1
Specifies the file to sort. If no file name is specified, the standard input is sorted. Specifying an input file is faster than redirecting the same file as standard input.
/t [drive2:][path2]
Specify the directory path to retain the sort command working storage to prevent data from being loaded into main memory. The default is to use the system temporary directory.
/o [drive3:][path3]filename3
Specifies the file to store sorted input. If not specified, the data is written to standard output. Specifying an output file is faster than redirecting the same file as standard output!
Previous page123Read the full text