1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

"for" in multiple lines

Discussion in 'Support' started by LuP, Oct 25, 2011.

  1. LuP

    LuP

    Joined:
    Oct 4, 2011
    Messages:
    18
    Likes Received:
    0
    Hello,

    I encountered the following difference between TC*.exe and cmd.exe when processing a bat file with a for-loop parameters divided into more lines:

    Code:
    for %%j in (~
                ~Scr) do (
    
      rd /q /s .\%%j
    
    )
    
    The cmd.exe gives:

    Code:
    C:\Lukas\~\Cpp~~>cmd
    Microsoft Windows XP [Verze 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.
    
    C:\Lukas\~\Cpp~~>t.bat
    
    C:\Lukas\~\Cpp~~>for %j in (~ ~Scr) do (rd /q /s .\%j )
    
    C:\Lukas\~\Cpp~~>(rd /q /s .\~ )
    Systém nemůže nalézt uvedený soubor. (= System cannot find the specified file.)
    
    C:\Lukas\~\Cpp~~>(rd /q /s .\~Scr )
    Systém nemůže nalézt uvedený soubor. (= System cannot find the specified file.)
    
    C:\Lukas\~\Cpp~~>
    whilst TC*.exe gives:

    Code:
    C:\Lukas\~\Cpp~~>t.bat
    for %%j in (~
    C:\Lukas\~\Cpp~~\t.bat [1]  Usage : FOR [/A:[[-][+]rhsdaecjot] /D /F ["options"] /H /I"text" /L /Nj /O:[-]adegnrstu /R [path] /T"..." /W] %var IN ([@]set | start, step, end) [DO] command ..
    ~Scr) do (
    TCC: C:\Lukas\~\Cpp~~\t.bat [2]  Unknown command "~Scr)"
    rd /q /s .\%j
    TCC: (Sys) C:\Lukas\~\Cpp~~\t.bat [4]  Neplatný název adresáře. (= Invalid name of a directory.)
     "C:\Lukas\~\Cpp~~\%j"
    )
    TCC: C:\Lukas\~\Cpp~~\t.bat [6]  Unknown command ")"
    
    C:\Lukas\~\Cpp~~>
    This is just a sample - normally I have the line divided into more lines as it uses many parameters:

    Code:
    ...
        for %%j in (~
                    ~Scr Scr~ ~Scr.AxE Scr~.AxE ~Scr.Off Scr~.Off ~Scr.Srf Scr~.Srf
                    DXF~ DXF~.AxE DXF~.Srf DXF~.Off
                    ~Drawings Drawings~ Drawings.~ Drawings.~~
                    ~Test Test.~
                    Funs~ Tables~
                    ~.plt) do (
    
          rd /q /s %%i\%%j
    
        )
    ...
    
    So my question is - how to make TC* treat for-loop parameters divided into more lines the same way as cmd.exe does?

    TIA.

    LuP
     
  2. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,854
    Likes Received:
    83
    There are two documented ways of doing this in CMD and TCC; you're using a third undocumented approach that only works in certain versions of CMD. (And only because CMD has a separate incompatible parser for the FOR command.)

    The preferred ways of doing it are either:

    Code:
    for %%j in (~ ~Scr) do (
     
      rd /q /s .\%%j
     
    )
    
    where the open command group '(' at the end of the first line tells FOR to look for the closing ')'. (IMO this is the much more readable format.)

    Alternatively, you can add an escape at the end of the line, which tells TCC (and CMD) that the command is continued on the next line:

    Code:
    for %%j in (~^
        ~Scr) do (
     
      rd /q /s .\%%j
     
    )
    
     
  3. LuP

    LuP

    Joined:
    Oct 4, 2011
    Messages:
    18
    Likes Received:
    0
    OK, thanks.

    In your proposal the divided text yields "~~Scr" (= just one parameter) whilst

    Code:
    for %%j in (~ ^
        ~Scr) do (
     
      rd /q /s .\%%j
     
    )
    gives "~" and "~Scr" (= two parameters; as wanted originally).

    Thanks again.

    LuP
     

Share This Page