动态分配批处理中的变量

问题描述 投票:0回答:1

我正试图从动态给定的结果中分配新的变量,目前脚本正在从 "wmic logicaldisk get name value "给定的结果中动态地计算和呼应驱动器字母。脚本目前从 "wmic logicaldisk get name value "给定的结果中动态地计算和呼应驱动器字母,它成功地呼应了驱动器字母,我想将其分配为新的变量,例如:C应该分配为%C%变量或%DRIVE-C%变量。谢谢你,这里是脚本。

setlocal EnableDelayedExpansion
set "output_cnt=0"
for /f "tokens=2 delims=:=" %%f in ('wmic logicaldisk get name /value') do (
    set /a output_cnt+=1
    set "DRIVE[!output_cnt!]=%%f"
)

for /L %%n in (1 1 !output_cnt!) DO echo Partition !DRIVE[%%n]!````
loops batch-file
1个回答
0
投票

我相信你问的是如何使用驱动器字母作为索引将值分配给一个数组,对吗?

@Echo off & Setlocal EnableDelayedExpansion

Set AvDrives=
For /f "tokens=2 delims=:=" %%f in ('wmic logicaldisk get name /value') do (
::: - Build a reference string containing the drives returned to use as the Array index.
    Set AvDrives=!AvDrives! "%%~f"
::: - Assign a value to the Array index - optional
    Set "DRIVE[%%~f]=%%~f"
)

::: - Iterate over / perform actions on the array indices
For %%D in (!AvDrives!) Do Echo 'DRIVE[%%~D]=%%~D'

pause

输出。

'DRIVE[C]=C'
'DRIVE[D]=D'
Press any key to continue . . . 
© www.soinside.com 2019 - 2024. All rights reserved.