[Zope-Coders] silly windows request

Tim Peters tim@zope.com
Wed, 9 Oct 2002 14:13:09 -0400


[Guido]
> ...
> I gave up on the configure.bat file.  I gave all the special commands
> a @ prefix (e.g. echo became @echo), which fixed the "Bad command"
> errors,

I expect you're fooling yourself there.  Repairing the line endings made all
the "Bad command" errors go away for me.  The only effect of an "@" prefix
is to tell the shell *not* to echo the line to stdout before executing it,
assuming echo mode is on (which it is by default, but the initial

    @echo off

line turns it off, and the "@" there is simply to prevent the shell from
displaying

    echo off

before disabling echo mode).


> but I didn't know what to do with the "for" command that also
> mystified Tim.

Doing

    for /?

from a cmd.exe shell gives full details about how cmd.exe's extended "for"
works.  The problem is that command.com has nothing like it -- the cmd.exe
"for" combines all of looping, file input, file parsing, line tokenization,
and backtick-like subcommand output interpolation:

"""
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 variable names are 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 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 ...
"""

If only Python were so simple <wink>.