Multi-line commands with leading spaces

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Eureka!
Posts: 111
Joined: 25 Jul 2019 18:25

Multi-line commands with leading spaces

#1 Post by Eureka! » 09 Feb 2020 17:02

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)

penpen
Expert
Posts: 1872
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Multi-line commands with leading spaces

#2 Post by penpen » 09 Feb 2020 18:52

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

Eureka!
Posts: 111
Joined: 25 Jul 2019 18:25

Re: Multi-line commands with leading spaces

#3 Post by Eureka! » 10 Feb 2020 01:51

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

jeb
Expert
Posts: 972
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Multi-line commands with leading spaces

#4 Post by jeb » 10 Feb 2020 02:02

And it's not special for the FOR command.

Code: Select all

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

penpen
Expert
Posts: 1872
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Multi-line commands with leading spaces

#5 Post by penpen » 11 Feb 2020 07:42

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

Eureka!
Posts: 111
Joined: 25 Jul 2019 18:25

Re: Multi-line commands with leading spaces

#6 Post by Eureka! » 11 Feb 2020 11:07

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?

jeb
Expert
Posts: 972
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Multi-line commands with leading spaces

#7 Post by jeb » 11 Feb 2020 11:20

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

Eureka!
Posts: 111
Joined: 25 Jul 2019 18:25

Re: Multi-line commands with leading spaces

#8 Post by Eureka! » 11 Feb 2020 12:42

Thanks @jeb and @penpen; makes sense now.

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

Post Reply