Possible characters for FOR-Parameters

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jeb
Expert
Posts: 1061
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Possible characters for FOR-Parameters

#1 Post by jeb » 18 Dec 2011 17:26

Hi,

this should be a discussion or search about the possible and allowed characters in a for-loop.

As Dave showed at DOS batch script to parse CSV file and output a text file.

The current list seems to exclude only the batch delimiters.

dbenham wrote:The following is a summary of characters that have restrictions or require special syntax.
Note that text within angle brackets like <space> represents a single character.

Code: Select all

Dec  Hex   Character   Define     Access
  0  0x00  <nul>         No       No
 09  0x09  <tab>         No       %%^<tab>  or  "%%<tab>"
 10  0x0A  <LF>          No       %%^<CR><LF><CR><LF>  or  %%^<LF><LF>
 11  0x0B  <VT>          No       %%<VT>
 12  0x0C  <FF>          No       %%<FF>
 13  0x0D  <CR>          No       No
 32  0x20  <space>       No       %%^<space>  or  "%%<space>"
 34  0x22  "             %%^"     %%"  or  %%^"
 37  0x25  %             %%%%     No
 38  0x26  &             %%^&     %%^&  or  "%%&"
 41  0x29  )             %%^)     %%^)  or  "%%)"
 44  0x2C  ,             No       %%^,  or  "%%,"
 59  0x3B  ;             No       %%^;  or  "%%;"
 60  0x3C  <             %%^<     %%^<  or  "%%<"
 61  0x3D  =             No       %%^=  or  "%%="
 62  0x3E  >             %%^>     %%^>  or  "%%>"
 94  0x5E  ^             %%^^     %%^^  or  "%%^"
124  0x7C  |             %%^|     %%^|  or  "%%|"
126  0x7E  ~             No       %%~~  (%%~ may work, or may crash COMMAND.EXE)
255  0xFF  <NB space>    No       No

dbenham wrote:Special characters like ^ < > | & must be either escaped or quoted. For example, the following works:

for /f %%^< in ("OK") do echo "%%<" %%^<

Some characters cannot be used to define a FOR variable. For example, the following gives a syntax error:

for /f %%^= in ("No can do") do echo anything

But %%= can be implicitly defined by using the TOKENS option, and the value accessed in the DO clause like so:

for /f "tokens=1-3" %%^< in ("A B C") do echo %%^< %%^= %%^>

The % is very odd - You can define a FOR variable using %%%%. The value cannot be accessed, yet the subsequent tokens work fine.

for /f "tokens=1,2" %%%% in ("A B") do echo %%%% %%^&

The above yields %% B

The ~ is a dangerous FOR variable. It can only be defined indirectly. But you also have to be careful how you access it. If you attempt to access the variable using %%~, you may get the correct result, you may get the wrong result, or you may crash COMMAND.EXE! The only reliable way to access it is using %%~~, but it strips any enclosing quotes as would be expected.

for /f "usebackq tokens=1,2" %%} in ('"A" "B"') do echo %%} %%~~

The above yields "A" B


My own results are:
The ~ CAN be defined by

Code: Select all

for %%~ in ("Hello") do echo %%~!


%%~ is dangerous only at the end of a line, as it can produce buffer overruns.
But the same could be useful for viewing the internal token buffer.
Once I build a nice trick with this.
Currently I'm searching for the example! :roll:

The %%%% can be accessed with a simple trick, you need the ~

Code: Select all

for %%%% in ("Hello") do echo %%~%%


In my tests you could also use as the define parameter.
10 0x0A <LF>
11 0x0B <VT>
12 0x0C <??>
But also here I can't remember how I tested it :(

jeb

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Possible characters for FOR-Parameters

#2 Post by dbenham » 18 Dec 2011 21:12

Thanks jeb.

I corrected the SO info regarding %%~ and %%~%%

I still can't access %%<0x255>

I'm curious how you managed to define %%<0x0A>, %%<0x0B>, %%<0x0C> in the past. :?


Dave Benham

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

Re: Possible characters for FOR-Parameters

#3 Post by jeb » 19 Dec 2011 10:53

dbenham wrote:I still can't access %%<0x255>

I'm curious how you managed to define %%<0x0A>, %%<0x0B>, %%<0x0C> in the past.

I'm too :)
I suppose, I'm only confused, and I haven't found any solution for these characters. :(

jeb

Post Reply