我如何传递一个变量名作为参数传递给一个批处理文件的一部分,然后用它? [英] How can I pass a variable name as part of a parameter to a batch file and then use it?

查看:894
本文介绍了我如何传递一个变量名作为参数传递给一个批处理文件的一部分,然后用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行,它接受一个字符串,它包含一个变量名的批处理文件。该变量随后将在批处理文件中使用。例如...

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%

在批处理文件...

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

编辑:结果
我已经做了研究试图使这项工作相当多。我试过最有前途的想法是一个循环递归评估%1(在本例中)。不幸的是,我不能使它工作。希望这个火花的一些想法给别人。


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.

推荐答案

code:

Code:

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

输出:

Output:

user1

这篇关于我如何传递一个变量名作为参数传递给一个批处理文件的一部分,然后用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆