views:

256

answers:

2

Hi,

In my pursuit of a solution to another environment-variable/batch-file related problem, I have once again come across a problem I have visited before (but cannot for the life of me remember how, or even if I solved it).

Say you have two BAT files (or one batch file and the command line). How can one pass an environment variable name to the other so that it can read the variable? The following example does not work:

A.BAT:
  @call b.bat path

B.BAT:
  @echo %%1%

> A.BAT
> %1
> B.BAT path
> %1

It is easy enough to pass the environment variable name, but the callee cannot seem to use it. (I don’t remember if or how I dealt with this the last time it came up, but I suspect it required the less-than-ideal use of redirecting temporary BAT files and calling them and such.)

Any ideas? Thanks.

+1  A: 

B.BAT:

FOR /F "delims=" %%a IN ('echo.%%%1%%') DO set inputvar=%%a
echo %inputvar%

That is one way of doing it.

If all you want to do is echo it, you can do: echo.%%%1%%|more or echo %%%1%%|find /v ""

Anders
I tried that, but all it does is print a double percent: `%%`.
Synetech inc.
What does just print %%? Windows version? FOR /F will only work on NT (2000?)
Anders
`for /f` is available since Windows NT 4. Also, this method fails with spaces in the variable's contents. I took the liberty of editing it to work properly.
Joey
Yup, using the DELIMS flag fixed it. Thanks.
Synetech inc.
+2  A: 

You can use a little trick which unfortunately is nowhere documented:

call echo %%%1%%

Then you can use delayed expansion:

setlocal enabledelayedexpansion
echo !%1!

Delayed expansion helps here mostly because it uses other delimiters for the variable and evaluates them directly prior to running the command, while normally the evaluation might clash with normal parameter expansion.

Another way of overdoing this would be a subroutine:

call :meh "echo %%%1%%"

...

:meh
%~1
goto :eof

All examples, including the other answer, have one thing in common here: They all force cmd to evaluate variables/parameters twice. It won't work otherwise, since the first evaluation must produce %VariableName%, while the second will expand that to the variable's contents.

You can find the code also on my SVN.

Joey
Ah, I had forgotten about enabledelayedexpansion. I was trying to focus more on old school stuff since we don't know the version requirement.
Anders
Nice, both methods work. In fact, you gave me an idea for a way that might hopefully work even in old DOS mode. I’ll test it out right now.
Synetech inc.
@Syn: Do you *need* this in DOS? I didn't have to touch DOS for a few years by now and I doubt that many solutions still need to run there.
Joey
Not for my current use, but I’m fairly sure that the last time I came across this issue was in DOS. Either way, it didn't work. I tried to have the batch file call itself (since DOS cannot call a label), but that just printed the variable with the percents signs around it; it did not expand. Oh well. (Now if I can just remember which project I was working on that needed this…)
Synetech inc.
@Syn: You can do something like `echo echo %%%1%%>temp.bat` and then call `temp.bat` or so. This might work in DOS. I think I need a virtual machine with various versions of DOS and Windows from time to time to test batch files ...
Joey
Right, I’m sure using a temp file would probably do the trick. (Though I try to avoid the use of temp file.) I guess if I find the DOS one that needed this, then I would bite the bullet and use the temp file. Thanks!
Synetech inc.