Page 1 of 1

Multi-line commands with leading spaces

Posted: 09 Feb 2020 17:02
by Eureka!
These scriptlines:

Code: Select all

for %%x in (*.txt) do ^
    echo %%x
produces an error:
' ' is not recognized as an internal or external command,
operable program or batch file.
Removing the spaces before echo leads to the expected results.


The question:
Was it always like this? I am not entirely sure but I think I used it like this - with the extra spaces - in the past.

(Win10 1909 x64)

Re: Multi-line commands with leading spaces

Posted: 09 Feb 2020 18:52
by penpen
If i remember right and your term "always" means "since XP", then yes, it was always like that.
The only way i remember using extra spaces, is to use round brackets (which makes that escape character pretty useless):

Code: Select all

for %%x in (*.txt) do (^
    echo %%x
)
penpen

Re: Multi-line commands with leading spaces

Posted: 10 Feb 2020 01:51
by Eureka!
Thank you for clearing that up!

(After posting I realized that would make no difference what the answer would be (all in the past), but it was one of those things that kept nagging me ... Thanks for saving me :))

Re: Multi-line commands with leading spaces

Posted: 10 Feb 2020 02:02
by jeb
And it's not special for the FOR command.

Code: Select all

IF 1==1 ^
    echo Hello - Fails
    
^
    echo Simple - Fails too

Re: Multi-line commands with leading spaces

Posted: 11 Feb 2020 07:42
by penpen
If you think about it, one should expect to fail executing the echo command in:

Code: Select all

for %%x in (*.txt) do ^
    echo %%x
The reson is simple:
You don't execute the echo-command but a single space!

Code: Select all

@echo off
>" .bat" echo(@echo(executing "%%~nx0"
for %%x in (1) do ^
    echo %%x
penpen

Re: Multi-line commands with leading spaces

Posted: 11 Feb 2020 11:07
by Eureka!
penpen wrote:
11 Feb 2020 07:42
You don't execute the echo-command but a single space!
You are right (that is what the error message says, after all), but I am obviously missing some in-between steps to make this a logical explanation.
If it is not too much effort/trouble, could you give some more details?

Re: Multi-line commands with leading spaces

Posted: 11 Feb 2020 11:20
by jeb
It's an effect of the multiline caret.

A caret escapes the next character, if this character is a line feed THEN yet another character is fetched (but that's the last one even if it's a line feed, too)
The character will always be escaped ( Here: ALWAYS is a synonym for mostly)

Therefor something like this fails, because the first quote is escaped

Code: Select all

for %%a in (1) do ^
"myBat with space" 
In your case you escape a space character, the parser decides to use this single space as the command token ... and fails

But to force a multiline caret to NOT escape the first character of the next line you can use the obvious redirection technique :idea:

Code: Select all

for %%x in (*.txt) do <nul ^
    echo %%x

Re: Multi-line commands with leading spaces

Posted: 11 Feb 2020 12:42
by Eureka!
Thanks @jeb and @penpen; makes sense now.

(except for the "obvious" :shock: <nul method; will have to chew on that for a while ....)