Draft for Information Only
Content MS DOS Command: FOR
MS DOS Command: FORReference from Microsoft WinXP cmd help DescriptionFor each file in a set of files DO/run a specified command when the specified parameter is met. SyntaxFOR %variable IN (set) DO command [command-parameters] Parameters
RemarksSpecified %%variable instead of %variable when using in a batch program. Variable names are case sensitive. Command ExtensionsSyntaxFOR /D %variable IN (set) DO command [command-parameters] RemarksIf set contains wildcards, then specifies to match against directory name instead of file names. SyntaxFOR /R [[drive:]path] %variable IN (set) DO command [command-parameters] RemarksWalks the directory tree rooted at [drive:]path, executing the FOR statement in each directory of the tree. If no directory specification is specified after /R then the current directory is assumed. If set is just a single period (.) character then it will just enumerate the directory tree. SyntaxFOR /L %variable IN (start,step,end) DO command [command-parameters] RemarksThe set (start,step,end) is a sequence of numbers from start to end by step. Negative step is allowed. Imply set (1,2,5) equals to set (1,3,5) and set (5,2,1) equals to set (5,3,1). SyntaxFOR /F ["options"] %variable IN (file-set) DO command [command-parameters] FOR /F ["options"] %variable IN ("string") DO command [command-parameters] FOR /F ["options"] %variable IN ('command') DO command [command-parameters] Or, if usebackq option present: FOR /F ["options"] %variable IN (file-set) DO command [command-parameters] FOR /F ["options"] %variable IN ('string') DO command [command-parameters] FOR /F ["options"] %variable IN (`command`) DO command [command-parameters] RemarksFilenameset is one or more file names. Each file is opened, read and processed before going on to the next file in filenameset. Processing consists of reading in the file, breaking it up into individual lines of text and then parsing each line into zero or more tokens. The body of the for loop is then called with the variable value(s) set to the found token string(s). By default, /F passes the first blank separated token from each line of each file. Blank lines are skipped. The default parsing behavior can be override by specifying the optional "options" parameter. This is a quoted string which contains one or more keywords to specify different parsing options. The keywords are:
Example:FOR /F "eol=; tokens2,3* delims=, " %i IN (myfile.txt) DO @echo %i %j %k Do: Command would parse each line in myfile.txt, ignoring lines that begin with a semicolon, passing the 2nd and 3rd token from each line to the for body, with tokens delimited by commas and/or spaces. Notice the for body statements reference %i to get the 2nd token, %j to get the 3rd token, and %k to get all remaining tokens after the 3rd. For file names that contain spaces, you need to quote the filenames with double quotes. In order to use double quotes in this manner, you also need to use the usebackq option, otherwise the double quotes will be interpreted as defining a literal string to parse. %i is explicitly declared in the for statement and the %j and %k are implicitly declared via the tokens= option. You can specify up to 26 token via the tokens= line, provided it does not cause an attempt to declare a variable higher than the letter 'z' or 'Z'. Remember, FOR variables are single-letter, case sensitive, global, and you cannot have more than 52 total active at any one time. You can also use the FOR /F parsing logic on an immediate string, by making the filenameset between the parenthesis a quoted string, using single quote characters. It will be treated as a single line of input from a file and parsed. Finally, you can use the FOR /F command to parse the output of a command. You do this by making the filenameset between the parenthesis a back quoted string. It will be treated as a command line, which is passed to a child CMD.exe and the output is captured into memory and parsed as if it was a file. Example:FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i Do: Command would enumerate the environment variable names in the current environment. In addition, substitution of FOR variable references has been enhanced with the following optional syntax:
The modifiers can be combined to get compound results:
In the above examples %I and PATH can be replaced by other valid values. The %~ syntax is terminated by a valid FOR variable name. Picking upper case variable names like %I makes it more readable and avoids confusion with the modifiers, which are not case sensitive. ©sideway ID: 110700018 Last Updated: 7/13/2011 Revision: 0 Latest Updated Links
![]() ![]() ![]() ![]() ![]() |
![]() Home 5 Business Management HBR 3 Information Recreation Hobbies 8 Culture Chinese 1097 English 339 Travel 18 Reference 79 Computer Hardware 254 Software Application 213 Digitization 37 Latex 52 Manim 205 KB 1 Numeric 19 Programming Web 289 Unicode 504 HTML 66 CSS 65 SVG 46 ASP.NET 270 OS 431 DeskTop 7 Python 72 Knowledge Mathematics Formulas 8 Set 1 Logic 1 Algebra 84 Number Theory 206 Trigonometry 31 Geometry 34 Calculus 67 Engineering Tables 8 Mechanical Rigid Bodies Statics 92 Dynamics 37 Fluid 5 Control Acoustics 19 Natural Sciences Matter 1 Electric 27 Biology 1 |
Copyright © 2000-2025 Sideway . All rights reserved Disclaimers last modified on 06 September 2019