for
- help for
Output:
指定されたコマンドをファイル セットの各ファイルに対して実行します。
FOR %変数 IN (セット) DO コマンド [コマンドパラメーター]
%変数 単一文字の置き換え可能なパラメーターを指定します。
(セット) ファイル セットを指定します。ワイルドカードを使用できます。
コマンド 各ファイルごとに実行するコマンドを指定します。
コマンドパラメーター
指定されたコマンドのパラメーターまたはスイッチを指定します。
バッチ プログラムで FOR コマンドを使用するときは、%変数の代わりに、
%%変数を使用してください。変数名では大文字と小文字が区別されるため、
%i と %I は異なります。
コマンド拡張機能を有効にすると、次の FOR コマンドの追加形式
がサポートされるようになります:
FOR /D %変数 IN (セット) DO コマンド [コマンド パラメーター]
セットがワイルドカードを含む場合は、ファイル名ではなくディレクトリ名
の一致を指定します。
FOR /R [[ドライブ:]パス] %変数 IN (セット) DO コマンド [コマンド パラメーター]
[ドライブ:]パスから始めて、ツリーの各ディレクトリで FOR 文を実行し
ます。/R の後にディレクトリが指定されていない場合は、現在の
ディレクトリが使用されます。セットが単一のピリオド (.) である場合は、
ディレクトリ ツリーの列挙だけを行います。
FOR /L %変数 IN (開始,ステップ,終了) DO コマンド [コマンド パラメーター]
セットは、ステップの量ごとに変化する開始から終了までの数列です。
たとえば、(1,1,5) は 1 2 3 4 5、(5,-1,1) は (5 4 3 2 1) という数列に
なります。
FOR /F ["オプション"] %変数 IN (ファイル セット) DO コマンド
[コマンド パラメーター]
FOR /F ["オプション"] %変数 IN ("文字列") DO コマンド [コマンド パラメーター]
FOR /F ["オプション"] %変数 IN ('コマンド') DO コマンド [コマンド パラメーター]
または usebackq オプションの場合:
FOR /F ["オプション"] %変数 IN (ファイル セット) DO コマンド
[コマンド パラメーター]
FOR /F ["オプション"] %変数 IN ('文字列') DO コマンド [コマンド パラメーター]
FOR /F ["オプション"] %変数 IN (`コマンド`) DO コマンド [コマンド パラメーター]
ファイル セットは、1 つ以上のファイル名です。各ファイルが開かれ、
読み取られ、処理されてから、ファイル セットの次のファイルに進みます。
処理では、ファイルの読み取り、個々のテキスト行への分割と、0 個以上の
トークンへの解析が行われます。その後、見つかったトークン文字列を変数値に
設定して for ループの本体が呼び出されます。既定では、/F は、各ファイルの
各行から、空白で区切られた最初のトークンを取得して渡します。空白行は
スキップされます。既定の解析動作を変更するには、省略可能な "オプション"
パラメーターを指定します。これは、異なる解析オプションを指定する 1 つ以上の
キーワードを含む、引用符で囲まれた文字列です。キーワードは、次のとおりです:
eol=c - 行末のコメント文字を指定します (1 文字)。
skip=n - ファイルの先頭でスキップする行数を指定します。
delims=xxx - 区切り文字のセットを指定します。
これは、既定の区切り文字であるスペースとタブを
置き換えます。
tokens=x,y,m-n - 各繰り返しに対して、各行から for 本体に渡す
トークンを指定します。これにより、追加の変数名が
割り当てられます。
m-n の形式は範囲で、m 番目から n 番目の
トークンを指定します。
tokens= 文字列の最後の文字がアスタリスクである場合は、
追加の変数が割り当てられ、最後のトークンが解析された
後、行に含まれている残りのテキストを受け取ります。
usebackq - 次の新しい表示形式を指定します。
逆引用符で囲まれた文字列がコマンドとして実行され、
一重引用符で囲まれた文字列がリテラル文字列コマンドに
なり、ファイル セットのファイル名を二重引用符で
囲めるようになります。
例を参考にしてください:
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k
この例は、myfile.txt の各行を解析します。セミコロンで始まる行を無視し、
各行の 2 番目と 3 番目のトークンを for 本体に渡します。
トークンは、コンマまたはスペースで区切られています。
for 本体の文が %i で 2 番目のトークンを、%j で 3 番目のトークンを取得し、
%k で 3 番目以降のすべてのトークンを取得していることに
注意してください。
スペースを含むファイル名に対しては、二重引用符でファイル名を引用する
必要があります。
この方法で二重引用符を使うためには、usebackq オプションも
使わなければなりません。
使わなければ、二重引用符はリテラル文字列の定義として
解釈され、解析されます。
%i は for 文で明示的に宣言され、%j と %k は tokens= オプションで暗黙的に
宣言されています。
tokens= 行を使って 26 個までのトークンを指定できますが、
文字 'z' または 'Z' よりも高い変数を宣言することはできません。FOR 変数名は
単一の文字で、大文字と小文字を区別し、グローバルなものであり、一度に
アクティブにできるのは合計 52 個までです。
また、かっこで囲んだファイル セットを一重引用符で囲み、文字列にすることに
より、即時の文字列に対する FOR /F 解析ロジックを使うこともできます。
これは、ファイルからの単一入力行として処理されます。
最後に、FOR /F コマンドを使って、コマンド出力を解析することができます。
かっこの中のファイル セットを逆引用符で囲みます。この文字列は、コマンド
ラインとして子 CMD.EXE に渡されます。出力はメモリにキャプチャされ、
ファイルのように解析されます。
例:
FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i
この例は、現在の環境の環境変数名を列挙します。
また、FOR 変数参照の置換も拡張されました。
次のオプション構文を使うことができます:
%~I - すべての引用句 (") を削除して、%I を展開します。
%~fI - %I を完全修飾パス名に展開します。
%~dI - %I をドライブ文字だけに展開します。
%~pI - %I をパス名だけに展開します。
%~nI - %I をファイル名だけに展開します。
%~xI - %I をファイル拡張子だけに展開します。
%~sI - 展開されたパスは短い名前だけを含みます。
%~aI - %I をファイルの属性に展開します。
%~tI - %I ファイルの日付/時刻に展開します。
%~zI - %I ファイルのサイズに展開します。
%~$PATH:I - PATH 環境変数に指定されているディレクトリを
検索し、最初に見つかった完全修飾名に %I を
展開します。
環境変数名が定義されていない場合、または検索
してもファイルが見つからなかった場合は、この
修飾子を指定すると空の文字列に展開されます。
修飾子を組み合わせて、複合結果を得ることもできます:
%~dpI - %I をドライブ文字とパスだけに展開します。
%~nxI - %I をファイル名と拡張子だけに展開します。
%~fsI - %I を完全なパスと短い名前だけに展開します。
%~dp$PATH:I - PATH 環境変数に指定されているディレクトリを
検索して %I を探し、最初に見つかったファイル
のドライブ文字とパスだけに展開します。
%~ftzaI - %I を DIR コマンドの出力行のように展開します。
上の例の %I と PATH は、他の有効な値で置き換えることができます。
%~ 構文は、有効な FOR 変数名によって区切られます。%I のような大
文字の変数を使うと読み取りやすく、大文字と小文字を区別しない修飾子
との混乱を避けることができます。
Runs a specified command for each file in a set of files.
FOR %variable IN (set) DO command [command-parameters]
%variable Specifies a single letter replaceable parameter.
(set) Specifies a set of one or more files. Wildcards may be used.
command Specifies the command to carry out for each file.
command-parameters
Specifies parameters or switches for the specified command.
To use the FOR command in a batch program, specify %%variable instead
of %variable. Variable names are case sensitive, so %i is different
from %I.
If Command Extensions are enabled, the following additional
forms of the FOR command are supported:
FOR /D %variable IN (set) DO command [command-parameters]
If set contains wildcards, then specifies to match against directory
names instead of file names.
FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]
Walks 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.
FOR /L %variable IN (start,step,end) DO command [command-parameters]
The set is a sequence of numbers from start to end, by step amount.
So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) would
generate the sequence (5 4 3 2 1)
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]
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]
file-set is one or more file names. Each file is opened, read
and processed before going on to the next file in file-set.
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. You can override the default parsing
behavior 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:
eol=c - specifies an end of line comment character
(just one)
skip=n - specifies the number of lines to skip at the
beginning of the file.
delims=xxx - specifies a delimiter set. This replaces the
default delimiter set of space and tab.
tokens=x,y,m-n - specifies which tokens from each line are to
be passed to the for body for each iteration.
This will cause additional variable names to
be allocated. The m-n form is a range,
specifying the mth through the nth tokens. If
the last character in the tokens= string is an
asterisk, then an additional variable is
allocated and receives the remaining text on
the line after the last token parsed.
usebackq - specifies that the new semantics are in force,
where a back quoted string is executed as a
command and a single quoted string is a
literal string command and allows the use of
double quotes to quote file names in
file-set.
Some examples might help:
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k
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 tokens 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 can't 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 file-set 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 file-set 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. So the following
example:
FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i
would enumerate the environment variable names in the current
environment.
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string
The modifiers can be combined to get compound results:
%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line
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.
Return Code: 1