Maylow wrote: ↑28 Sep 2022 07:50
Is there information about why FOR treats space characters as commands instead of ignoring them? It deviates from default behavior imo.
It is the default behaviour:
If you escape a character (using the circumflex-character '^'), or add characters when the interpreter expects text starting with escaped characters (using for-variables in the way you did), then it has no special function for the tokenizer that splits the command line string into meaningfull pieces (=tokens). Therefore that escaped character will end up as (a part of) a token.
The interpreter assumes that the first token is the command to execute, which in your above example is a token containing the space-character only.
Maylow wrote: ↑28 Sep 2022 07:50
And might there also be a way to have FOR treat space characters NOT as commands? I suppose not.
Depending on what you mean. In general use escaped whitespace-characters only where you need them and don't use them if you don't want them.
Maylow wrote: ↑28 Sep 2022 07:50
I've tried several things, like escape whitespace, replace with ASCII values (tab, backspace, others), etc, they don't work.
Other workarounds I don't like, for example redirecting and thus ignoring the error message, using & to have the second command executed successively.
I would rather have the possibility for FOR to ignore whitespace characters, just like cmd would do under 'normal' circumstances.
Using an escaped whitespace-character is what lead you into that situation.
You want the opposit, which is an unescaped whitespace-character, because you want the lexer to recognize that character to have the special function of beeing 'whitespace'.
Windows batch script doesn't support unescaping characters, so you only have the option to avoid that situation.
I don't know your intended program logic, so there might (or might not) be multiple ways (with different sideeffects) how to help you; for example:
Code: Select all
for /f "tokens=1,* delims=," %%b in (",dir") do %%~b %%~c
for /f "tokens=1,* delims=," %%b in (""",dir") do %%~b %%~c
penpen