有关嵌套变量替换的Batch EnableDelayedExpansion的问题

时间:2019-04-14 22:37:03

标签: batch-file cmd delayedvariableexpansion

替换嵌套的动态变量“ SHADOW_ID_!index!”不起作用。

示例批处理文件:

@echo off
setlocal EnableDelayedExpansion

SET SHADOW_SET_ID={1e3cf732-3c3d-4827-93c5-df7837f78e44}
SET SHADOW_ID_1={884ece35-5320-4e81-ab24-a409ab9de0bf}
SET SHADOW_DEVICE_1=\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy12
SET SHADOW_ID_2={f3ad2b2f-455a-4f6b-b04e-0966ea29882e}
SET SHADOW_DEVICE_2=\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy13

set /a index=0
for %%l in (C D) do (
    set /a index=!index!+1
    set SHADOW_DRIVE_%%l=%%l
    set SHADOW_ID_%%l=!SHADOW_ID_%index%!

    echo !index!: SHADOW_DRIVE_%%l = "!SHADOW_DRIVE_%%l!"
    echo !index!: SHADOW_ID_%%l = "!SHADOW_ID_%%l!"
)

endlocal

我认为我需要在运行时替换展位变量:

set SHADOW_ID_%%l=!SHADOW_ID_!index!!

但这不起作用。 似乎只有外部感叹号和内部百分号有效,但是循环中的%index%并非最新/可用。

Output (see batch file):
1: SHADOW_DRIVE_C = "C"
1: SHADOW_ID_C = ""
2: SHADOW_DRIVE_D = "D"
2: SHADOW_ID_D = ""

Needed Output:
1: SHADOW_DRIVE_C = "C"
1: SHADOW_ID_C = "{884ece35-5320-4e81-ab24-a409ab9de0bf}"
2: SHADOW_DRIVE_D = "D"
2: SHADOW_ID_D = "{f3ad2b2f-455a-4f6b-b04e-0966ea29882e}"

非常感谢您的帮助! / KPW

0 个答案:

没有答案