4

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

Community
  • 1
  • 1
Jimm Chen
  • 3,411
  • 3
  • 35
  • 59
  • Instead of trying to get it to work as a doskey alias, just make it a `whichlib.cmd` one-liner batch file: `@for %%i in (%1) do @if NOT "%%~$LIB:i"=="" @echo %%~$LIB:i` – Michael Burr May 14 '13 at 02:03
  • And just for future reference, I think that most people would prefer if copy-paste snippets from console sessions were done as text instead of images. Formatting the text as code usually works pretty well. – Michael Burr May 14 '13 at 02:08
  • Well, I got your idea. I use screen shots to manifest that I'm not asking my questing by just conceiving, and screen shot can avoid accidental copy paste error, finally it is visually attracting. – Jimm Chen May 14 '13 at 02:41
  • I'm not sure why copy/paste from a console window would be any more error prone than pasting a screen shot, text is generally easier to read than screen shots (particularly for readers who might have vision problems), and like I said, sometimes pasting the example into an editor allows for additional analysis or testing. – Michael Burr May 14 '13 at 02:50

1 Answers1

4

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
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Very good answer and explanation, verified! I used to load my doskey macros with CMD command ``doskey /macrofile=F:\ChjConfigs\chjmacro.txt`` , now I write in chjmacro.txt ``whichlib=for %i in ($1) do @echo %~$^LIB:i``, working like a charm. – Jimm Chen May 28 '13 at 06:52
  • Of course, better add a dot after ``echo``, i.e. ``whichlib=for %i in ($1) do @echo.%~$^LIB:i`` , so that when no matching file is found, ``echo`` displays nothing instead of mumbling ``ECHO is on.`` . – Jimm Chen May 28 '13 at 07:02
  • @JimmChen - I know it looks wrong, but ECHO( is the only totally reliable variant. ECHO. can fail in some obscure scenarios. – dbenham May 28 '13 at 12:24