Batch:变量间接:通过动态构造的名称获取变量的值 [英] Batch: variable indirection: getting the value of a variable by dynamically constructed name

查看:50
本文介绍了Batch:变量间接:通过动态构造的名称获取变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以当我运行这段代码时:

So when I run this code:

@echo off
setlocal enabledelayedexpansion

set lstFolders= First Second 

set intCounter=0
for %%i in (!lstFolders!) do (
    set /a intCounter += 1
    set strFlder=%%i
    set strFolder!intCounter!=!strFlder!
    echo %%i
    echo !strFlder!
    echo !strFolder%intCounter%!
    echo !strFolder1!
    echo !strFolder2!
)

:End
pause
endlocal

结果如下:

First
First
ECHO is off.
First
ECHO is off.
Second
Second
ECHO is off.
First
Second

为什么不允许我回显以以下格式创建的变量:!strFolder%intCounter%!?有没有其他方法可以引用此变量并获取其中的数据?

Why doesn't it allow me to echo the variable create with the format : !strFolder%intCounter%!? Is there another way to reference this variable and get the data that is inside of it?

推荐答案

注意:以下代码仅适用于列表值(如%lstFolders%的标记,例如 First ):

Caveat: The code below only works with list values (the tokens of %lstFolders% such as First) that:

  • 都不包含空格
  • 也不包含以下任何字符:&|<>"

需要一种不同的循环方法来处理这种情况.

A different looping approach would be needed to handle such cases.

@echo off
setlocal enabledelayedexpansion

set "lstFolders=First Second" 

set intCounter=0
for %%i in (%lstFolders%) do (
      rem Increment the counter
    set /a intCounter += 1
      rem Echo the loop variable
    echo #!intCounter!=%%i
      rem Set variable strFolder<intCounter> to the loop variable's value
    set "strFolder!intCounter!=%%i"
      rem Echo the variable created using variable indirection with for /f ('...')
    for /f "delims=" %%v in ('echo "%%strFolder!intCounter!%%"') do set "thisFolder=%%~v"
    echo %%thisFolder%% ^(via %%strFolder!intCounter!%%^)=!thisFolder!
)

运行以上结果:

#1=First
%thisFolder% (via %strFolder1%)=First
#2=Second
%thisFolder% (via %strFolder2%)=Second


您要查找的是可变间接定向:

  • 可以间接设置一个变量(通过您根据另一个变量的值动态构造的名称),例如,
    • 设置"strFolder!intCounter!= %% i" ,并且!intCounter!的值为 1 ,可以正确设置变量 strFolder1 设置为 %% i )的值,
    • While you can set a variable indirectly (by a name that you construct dynamically from the value of another variable), e.g.,
      • set "strFolder!intCounter!=%%i", with !intCounter! having a value of 1, correctly sets variable strFolder1 to the value of %%i),

      无法以这种方式获取 的值;您需要一个额外的评估步骤,它可以为/f ...('echo ...')提供.:

      you cannot get a variable's value that way; you need an extra evaluation step, which for /f ... ('echo ...') can provide.:

      • <代码>用于/f"delims =" %% v in('echo"%% strFolder!intCounter!%%"')... ...单引号( echo ... )并将结果整体( delims = )分配给变量 %% v
        ( %%〜v 删除了在 echo 参数周围添加的双引号,以使命令处理诸如& |<<>; 正确).

      • for /f "delims=" %%v in ('echo "%%strFolder!intCounter!%%"') do ... parses the output of the command in single quotes (echo ...) and assigns the result as a whole (delims=) to variable %%v
        (%%~v removes the enclosing double quotes, which were added around the echo argument to make the command handle shell metacharacters such as & | < > correctly).

      %% strFolder!intCounter!%% 立即 strFolder!intCounter!评估为 strFolder1 ,如果!intCounter! 1 ,则由于包含了 doubled 实例,该结果以 literal %strFolder1%,这是 echo 命令在 for 命令运行时看到的内容,导致它评估变量引用并扩展为其值.

      %%strFolder!intCounter!%% immediately evaluates strFolder!intCounter! to strFolder1, if !intCounter! is 1, which, thanks to the enclosing doubled % instances, ends up as literal %strFolder1%, which is what the echo command sees when it is run by the for command, causing it to evaluate the variable reference and expand to its value.

      这篇关于Batch:变量间接:通过动态构造的名称获取变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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