%%~$ Fails in For

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

%%~$ Fails in For

#1 Post by carlos » 11 Dec 2012 22:11

Hello.
Maybe this have mentioned before. But anyways I post this:
In For command if you use the character $, it will fail if you want remove the quotes, using:

Code: Select all

%%~$


Example:

Code: Select all

@Echo Off

Rem Works:
For /F %%$ in ("Hi") do echo(%%$
For %%$ in ("Hi") do echo(%%$

Rem Fails:
For /F %%$ in ("Hi") do echo(%%~$
For %%$ in ("Hi") do echo(%%~$

Pause


Print:

Code: Select all

Hi
"Hi"
%~$
%~$

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: %%~$ Fails in For

#2 Post by Liviu » 12 Dec 2012 00:09

carlos wrote:In For command if you use the character $, it will fail if you want remove the quotes, using %%~$

That's dangerously close to the "%%~$PATH:I" syntax, and the parser mistakes it for such. Try "echo(%%~$:" for verification, which outputs nothing at all.

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

Re: %%~$ Fails in For

#3 Post by dbenham » 12 Dec 2012 07:24

Interesting Carlos. :D

I never thought to test that before, but I suppose it makes some amount of sense. The parser is getting confused by the %~$PATH:I modifier.

FOR /? wrote:

Code: Select all

    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string


I posted an analysis of valid characters for use as FOR variables on StatckOverflow - see the Addendum. I've updated that answer with the info about %%~$. I gave you a credit in a comment after the answer.

Somewhere on DosTips I started a discussion thread about FOR variables, but I can't find it :(


Dave Benham

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: %%~$ Fails in For

#4 Post by Ed Dyreen » 12 Dec 2012 08:32

dbenham wrote:Interesting Carlos. :D
...
I gave you a credit in a comment after the answer.
But this is nothing new, jeb posted about this long time ago, it was explained numerous times.
I took a quick look at your Addendum, the exclamation mark isn't listed. Don't advice people to use it, but for completeness, maybe...

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

Re: %%~$ Fails in For

#5 Post by dbenham » 12 Dec 2012 09:19

Ed Dyreen wrote:I took a quick look at your Addendum, the exclamation mark isn't listed. Don't advice people to use it, but for completeness, maybe...

What is special about %%! :?:

My addendum isn't an attempt to list all possible FOR variable characters. Rather it is an attempt to list characters that are problematic or require special syntax.

Post Reply