Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass a variable name as part of a parameter to a batch file and then use it?

I'm trying to run a batch file that accepts a string which includes a variable name. The variable will then be used in the batch file. For example...

C:\>test.bat user%num%

In the batch file...

set num=1
(This is where I need help... Somehow set domainusername=user1)
echo %domainusername%

set num=2
...

The idea is to allow the batch call to work in the same way regardless of where the variable name is. Examples..

C:\>test.bat %num%user
or
C:\>test.bat us%num%er

Edit:
I've already done a fair amount of research trying to make this work. The most promising idea that I tried was a for loop to recursively evaluate %1 (in the example). Unfortunately, I couldn't make it work. Hopefully this sparks some ideas for others.

like image 941
Lodra Avatar asked Apr 19 '12 15:04

Lodra


People also ask

How do I pass parameters to a batch file?

You simply substitute the parameter for 1 (e.g., %~f2 for the second parameter's fully qualified path name). The %0 parameter in a batch file holds information about the file when it runs and indicates which command extensions you can use with the file (e.g., %~dp0 gives the batch file's drive and path).

Can you use variables in batch files?

When creating batch files, you can use set to create variables, and then use them in the same way that you would use the numbered variables %0 through %9. You can also use the variables %0 through %9 as input for set. If you call a variable value from a batch file, enclose the value with percent signs (%).

How do you call a variable in a batch file?

Within the batch file, set the scope of the variable by specifying the variable-value pair in the scope-specific file, as follows: To set local variables, whose values are not available outside the scope of this flow (or subflow), from a file, use the JS_FLOW_VARIABLE_FILE environment variable to access the file.


2 Answers

As I understand the OP, the answers of Aacini and stackoverflow are wrong.

The delayed expansion solution (of Aacini) can only work if num is defined before starting the batch, else %mnm% will never expand.

C:\>set "num="

C:\>myBatch.bat user%num%

results to the output of
user%num%

The other solution (of stackoverflow) works a bit better, but fails when num is defined

I added a second set num=2 to demonstrate it

@ECHO off
SET param=%1
SET num=1
CALL SET x=%param%
ECHO %x%

set num=2
CALL SET x=%param%
ECHO %x%

Calling it two times shows the problem

C:\>myBatch user%num%
user1
user2

C:\>myBatch user%num%
user2
user2

In the second run you got 2 two times, as the result is fixed at the startup of the batch.

It's a good idea to echo the content of %1 to see if the format-string is really there or if the %num% is already expanded.

As I understand the OP (It's possible that I don't understand it!), the question is about using a placeholder,
but this is tricky with percents, as it only works at the command line (not from another batch) and it only works if the variable isn't defined in that moment.
Because, if the variable is defined, then the %num% will expand immediatly and the content of %1 is user2 and not user%num% (assuming num=2).

That it sometimes works is only a side effect of the cmd-line parser, as it does not remove an undefined variable (as inside a batch file), instead an undefined variable expansion will not changed in any way.

echo "%thisIsUndefined%"  - from cmd-line this outputs `"%thisIsUndefined%"`
echo "%thisIsUndefined%"  - from a batch file this outputs `""`

But as side effect, there doesn't exists a way to escape a percent sign in cmd-line context.
There exists only a workaround for a pseudo escape.
mybatch user%num^%
It doesn't really escapes the percent, but mostly there will not exists a variable named num^.

Then the solution of stackoverlow would work, with:

myBatch user%num^%

But I would prefere the delayed expansion, mentioned by Aacini.
You would call the batch then with exclamation marks, instead of percents, normally this works good, as delayed expansion is per default disabled.

myBatch user!num!

The batch itself would look like this

@echo off
set "param=%1"
Setlocal EnableDelayedExpansion
set num=1
echo %param%
set num=2
echo %param%
like image 32
jeb Avatar answered Sep 22 '22 11:09

jeb


Code:

@ECHO off
SET param=%1
SET num=1
CALL SET x=%param%
ECHO %x%

Output:

user1
like image 191
Ωmega Avatar answered Sep 23 '22 11:09

Ωmega