System command

for command

Repeat a command over files, parsed text, directories or a numeric range.

Availability: Microsoft Learn command reference spanning Windows client and Windows Server releases; some entries are legacy, role-specific, optional-feature or interactive subcommands.中文

Documented syntax

for {%% | %}<variable> in (<set>) do <command> [<commandlineoptions>]

The linked Microsoft Learn article is the upstream authority. Availability may depend on Windows release, role, optional feature or interactive tool context.

Examples

Example usage for for.

for %F in (*.log) do @echo %F

Parameters, options and clauses

Option / clauseDescription
%variable / %%variableUse one percent sign at an interactive prompt and two in batch files.
/dIterate over matching directories.
/r [<path>]Walk a directory tree recursively.
/lIterate over a numeric range in the form (start,step,end).
/f [<options>]Parse files, command output or strings into tokens.
in (<set>) / do <command>Provide the item set and the command to run for each item.
{%% \Microsoft documents this argument or subcommand; check the linked reference for its required format and context.
<commandlineoptions>Microsoft documents this argument or subcommand; check the linked reference for its required format and context.
/?Display command help or the supported command-line usage.
<variable>Microsoft documents this argument or subcommand; check the linked reference for its required format and context.
Safety note

Review a command and its target before running it. Commands that alter files, services, accounts, disks, permissions or databases may be difficult to reverse. Test in a safe environment and keep backups.

Official reference ↗Back to directory