Index | Syntax | Notes Batch



FOR--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 would 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:

    Capitalize capitalize Capitalized.
    Please beware of the two lines below, which got wrapped in transmission.
    Benny Pedersen, PS.
    The below batch file is tested in both DOS 6.22 and Win 98.

    @echo off
    set Word=capitalize
    echo %Word%. | choice /s/cabcdefghijklmnopqrstuvwxyz. > nul
    for %%H in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do if e
rrorlevel H%%H set H=%%H
    for %%X in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do if e
rrorlevel x%%X set X=%%X
    echo %X%. | choice /s/c%Word%. > nul
    if not errorlevel 2 for %%%X% in (%H%) do set Word=%%%Word%
    echo.%Word%
    for %%v in (Word H X) do set %%v=


Using round brackets/parentheses in a loop.
Put the brackets to the right of the FOR loop:

  @ECHO OFF
  IF (%1)==() FOR %%v in (GOTO:END ECHO.(%%1):(%1)) do %%v
  ECHO Got a value
  :END
  ECHO The end

If %1 is blank, then the user
will see the following:

       (%1):()
       The end

Note:  ...)NO SPACE HERE) do %%v


-Top- | Syntax | Notes Batch