Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ for(CMD) — OpenDesktop 3.0.0

Media Vault

Software Library

Restoration Projects

Artifacts Sought


 for(CMD)                        19 June 1992                        for(CMD)


 Name

    for - run a specified command for each file in a set of files

 Syntax

    To use for in a batch program, use the following syntax:

    for %%variable in (set) do command [command-parameters]

    To use for from the command prompt, use the following syntax:

    for %variable in (set) do command [command-parameters]


 Description

    You can use the for command within a batch program or directly from the
    command prompt.

 Parameters


    %%variable or %variable
              Represents a replaceable variable.  The for command replaces
              %%variable (or %variable) with each text string in the speci-
              fied set until the command (specified in the command parameter)
              processes all the files.  Use %%variable to carry out the for
              command within a batch program. Use %variable to carry out for
              from the command prompt.

    (set)     Specifies one or more files or text strings that you want to
              process with the specified command.  The parentheses are
              required.

    command   Specifies the command that you want to carry out on each file
              included in the specified set.

    command-parameters
              Specifies any parameters or switches that you want to use with
              the specified command (if the specified command uses any param-
              eters or switches).


 Notes


    Using the in and do keywords

    in and do are not parameters, but they are required in the for command.
    If you omit either of these keywords, MS-DOS displays an error message.

    Using the replaceable variable

    To avoid confusion with the batch parameters %0 through %9, you can use
    any character for variable except the numerals 0 through 9.  For simple
    batch programs, a single character such as %%f may be all that is neces-
    sary.

    You can use multiple values for variable in complex batch programs to
    distinguish different replaceable variables.  However, you cannot nest
    (add) multiple for commands on the same command line.

    Specifying a group of files

    The set parameter can represent a single group of files or several groups
    of files.  You can use wildcards (* and ?)  to specify a file set.  The
    following are valid file sets:

       (*.doc)

       (*.doc *.txt *.me)

       (jan*.doc jan*.rpt feb*.doc feb*.rpt)

       (ar??1991.* ap??1991.*)


    When you use the for command, the first value in set replaces %%variable
    (or %variable) and MS-DOS carries out the specified command in order to
    process this value; this continues until MS-DOS has processed all the
    files (or groups of files) that correspond to the value (or values) in
    set.

 Examples

    Suppose you want to use the type command to display the contents of all
    the files in the current directory that have the extension .DOC or .TXT.
    To do this and to use the replaceable variable %f, type the following
    command at the command prompt:

       for %f in (*.doc *.txt) do type %f


    In this example, each file that has the .DOC or .TXT extension in the
    current directory is substituted for the %f variable until the contents
    of every file are displayed.  To use this command in a batch file, you
    simply replace every occurrence of %f with %%f.  Otherwise, MS-DOS
    ignores the variable and displays an error message.

    MS-DOS supports command switches, pipes, and redirection that you may
    want to use with the specified command.  For example, to redirect the
    output of the previous example to PRN (the default printer port), you
    would type the following command:

       for %f in (*.doc *.txt) do type %f > prn:


Typewritten Software • bear@typewritten.org • Edmonds, WA 98026