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

杀马特。学长 韩版系。学妹 提交于 2019-12-10 13:18:19

问题


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.


回答1:


Code:

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

Output:

user1



回答2:


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%



回答3:


Excuse me. I think I don't really understand what you want. However...

If you pass the name of a variable to a Batch file, you may assign a value to that variable directly this way:

set %1=Any value

If you want to get the value of such variable, you need to use Delayed Expansion: insert setlocal EnableDelayedExpansion command at beginning and enclose the variable name (the %1 parameter) in exclamation marks:

setlocal EnableDelayedExpansion
set varValue=!%1!

In your case, I guess you want to achieve something like this:

set num=1
set domainusername=!user%num%!
echo %domainusername%

but this does NOT use the %1 parameter as you said, so I don't know...

I hope it helps...

EDIT: Perhaps this post may be useful to you...

Addendum: After I had realized what the OP wants, this is my solution:

@ECHO off
setlocal EnableDelayedExpansion
SET param=%1
SET num=1
SET x=!param!
ECHO %x%

A delayed expansion execute faster than a CALL...




回答4:


As an afterthought, you might consider testing that variable before you decide it is actually there. Add this to the start of your program:

if %1.==. goto :usage_orwherever


来源:https://stackoverflow.com/questions/10231334/how-can-i-pass-a-variable-name-as-part-of-a-parameter-to-a-batch-file-and-then-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!