Jimm Chen
Jimm Chen

Reputation: 3777

How to workaround doskey's special character, like $L

I got a useful tip from this post: https://stackoverflow.com/a/374363/151453 , but plagued by doskey's special characters.

(env: Windows 7 and Windows XP)

Using Visual C++ command line, we have env-vars INCLUDE and LIB. So with this doskey macro,

doskey whichinclude=for %i in ($1) do @echo.%~$INCLUDE:i

we can easily findout which .h is found first in which INCLUDE directory, really convenient.

enter image description here

However, this trick fails for LIB. I just CANNOT simply code a macro like:

doskey whichlib=for %i in ($1) do @echo.%~$LIB:i

Call whichlib winsock32.lib, it spouts The system cannot find the file specified.

enter image description here

I launch Procmon to know what happens, it reveals:

enter image description here

So I realize $L has special meaning for doskey, it is replaced with current drive letter when run.

Try double dollar( @echo.%~$$LIB:i ), still not working, Procmon report CMD accessing C:\echo .

Counld someone kindly help me out?

My doskey book mark: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/doskey.mspx?mfr=true

Upvotes: 4

Views: 1755

Answers (1)

dbenham
dbenham

Reputation: 130819

I agree with Michael Burr's comment - you may be better off with a batch file. I generally do not use DOSKEY macros because they do not work within batch files, so it seems kind of pointless. In my mind, if a command works on the command line, it should also work within a batch file.

But... it is possible to do what you want :)

The $ only has special meaning if it is followed by a character that has special meaning to DOSKEY. The $L is interpreted as the < character (input redirection). The MS documentation implies that $$L should give a $L literal, but the documentation is not correct, as you have discovered.

The DOSKEY $ substitution happens before the normal command line parsing. The trick to embed a literal $L in your macro definition is to put an intervening character between $ and L that is not treated as special by DOSKEY, but that disappears during the normal command line parsing - The ^ works perfectly. $^ has no special meaning to DOSKEY, and ^L simply becomes L during command line parsing.

You can list the definition of your DOSKEY macros by using DOSKEY /M.

The full definition you require is whichlib=for %i in ($1) do @echo(%~$^LIB:i.

The ^ must be escaped when you define the macro. So the complete line to define the macro becomes:

doskey whichlib=for %i in ($1) do @echo(%~$^^LIB:i

Upvotes: 4

Related Questions