本文介绍了批次:动态变量名称(等效于等效值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含环境变量名称的变量.我想评估这个价值.例如:

I have a variable containing the name of an environnent variable. I would like to eval this value. For example:

:: TOTO_1_2 defined outside of batch file
set varName="TOTO_1_2"
echo %TOTO_1_2% :: Display env var
echo %%varName%% :: Broken

想法是将varName指向的env var的值传递给命令.

The idea is to pass the value of the env var pointed by varName to a command then.

谢谢

推荐答案

如果要评估评估的变量,则必须将其解析两次:

if you want to evaluate a evaluated variable, you have to parse it twice:

有不同的可能性.这是其中的三个:

There are different possibilities to do that. Here are three of them:

@echo off
SET TOTO_1_2=hello
set "varName=TOTO_1_2"
echo 0: %TOTO_1_2%
call echo 1: %%%varName%%%

setlocal enabledelayedexpansion
for %%i in (%varname%) do echo 2: !%%i!
echo 3: !%varName%!

这篇关于批次:动态变量名称(等效于等效值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 17:38