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

I am trying to run a batch file that accepts a string that includes the variable name. Then the variable will 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 let the batch call work the same, no matter where the variable name is. Examples ..

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

Edit:
I have already done a lot of research trying to do this job. The most promising idea I tried was a for loop to recursively evaluate% 1 (in the example). Unfortunately, I could not get this to work. Hopefully these are sparks of some ideas for others.

+7
source share
4 answers

The code:

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

Output:

 user1 
+5
source

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

A deferred expansion solution (Aacini) can only work if num defined before the batch starts, otherwise %mnm% will never expand.

 C:\>set "num=" C:\>myBatch.bat user%num% 

user%num% output user%num%

Another solution (stackoverflow) works a little better, but fails when num 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% 

A call 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, since the result is fixed when the package starts.

A good idea is to echo the %1 content to see if the format string is actually present or if %num% already expanded.

As I understand OP (maybe I don’t understand this!), The question is to use placeholder,
but this is difficult with percentages, since it only works on the command line (not from another batch), and only works if the variable is not defined at that moment.
Since, if a variable is defined, then %num% will be expanded immediately, and the contents of %1 is user2 , not user%num% (provided that num = 2).

The fact that it sometimes works is only a side effect of the cmd-string analyzer, because it does not delete the undefined variable (as inside a batch file), and the extension of the undefined variable will not change in any way.

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

But as a side effect, there is no way to avoid the percent sign in the context of the cmd line.
There is only a workaround for pseudo security.
mybatch user%num^%
This does not really speed up the percentage, but basically there will be no variable named num^ .

Then the stackoverlow solution will work with:

 myBatch user%num^% 

But I would prefer the delayed extension mentioned by Aachini.
You would call a package and then exclamation points rather than percentages, as a rule, this works well, since delayed extension is disabled by default.

 myBatch user!num! 

The party itself will look like this:

 @echo off set "param=%1" Setlocal EnableDelayedExpansion set num=1 echo %param% set num=2 echo %param% 
+3
source

Sorry. I think I don’t quite understand what you want. But...

If you pass the variable name to a batch file, you can directly assign a value to this variable:

 set %1=Any value 

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

 setlocal EnableDelayedExpansion set varValue=!%1! 

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

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

but this DOES NOT use parameter% 1, as you said, so I don’t know ...

Hope this helps ...

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

Addendum : after I figured out what the OP wants, this is my solution:

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

Deferred expansion is faster than CALL ...

+1
source

As an afterthought, you might consider testing this variable before deciding that it actually exists. Add this to the top of your program:

 if %1.==. goto :usage_orwherever 
0
source

All Articles