"for" in multiple lines

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
Oct 4, 2011
18
0
#1
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
 

rconn

Administrator
Staff member
May 14, 2008
10,100
85
#2
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
 
)
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
 
)
 
Oct 4, 2011
18
0
#3
OK, thanks.

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
 
)
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