Usage of environment variable in a batch file as command line argument

  • Thread starter Thread starter kk
  • Start date Start date
K

kk

Hi,

in a batch file I can write, e.g.
if %PATH% ...

but when I want to use also other variables insted of PATH,
and I want to define that as a command line argument, e.g.
if %%1% ...
That does not work.

Is there any solution?

Regards
Konrad Kullig
 
Hi,

in a batch file I can write, e.g.
if %PATH% ...

but when I want to use also other variables insted of PATH,
and I want to define that as a command line argument, e.g.
if %%1% ...
That does not work.

Is there any solution?

I assume you mean you want to expand an environment
variable whose name is passed as a command-line argument.

Try a construct such as %%%1%% with an additional CALL to
expand the result.

Lines that aren't prefixed with two spaces have wrapped in transmission
====Begin cut-and-paste (omit this line)
@ECHO OFF
CALL ECHO %%%1%%

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

Screen capture with above pasted as DEMO.BAT:

============Screen capture Windows 2000 simulated in Win95
C:\WORK>SET VAR=String of text

C:\WORK>demo var
String of text

C:\WORK>
============End screen capture

Alternatively, you could use !%1! (without a CALL) if you have
Delayed Variable Expansion enabled.
 
kk said:
Hi,

in a batch file I can write, e.g.
if %PATH% ...

but when I want to use also other variables insted of PATH,
and I want to define that as a command line argument, e.g.
if %%1% ...
That does not work.

Is there any solution?

Regards
Konrad Kullig

%environmentvariablename%

to get a command-line argument

%1
%2
%3
etc to
%9

If you need to strip quotes surrounding a command-line argument,

%~1

had you given us an in-context example, we could be more helpful. As it is,

IF %1==...

should, primitively do what you seem to want to do.

IF "%1"=="something"...

might be better (saves against missing argument)

or

SET yDV=%1
IF NOT DEFINED yDV ECHO argument expected&GOTO :EOF
IF %yDV%==...one way...
IF %1==...another way...

where :EOF is defined by CMD.EXE as "end of file" (no need to declare it,
but the ":" is REQUIRED)
* yDV is a Dummy Variable. I prefix all my environemnt variables with "y".
yDV is not a "Magic value"

Perhaps you could read alt.msdos.batch.nt for nt-batch discussion.

HTH

....Bill
 
Back
Top