变量中的 Windows 批处理变量

Str*_*ing 6 variables batch-file

在 Windows 批处理中,您可以在变量中设置一个变量吗?

解释:

所以 %num% 在变量内。

set num=5
set cnum=test
set h1=%c%num%%
Run Code Online (Sandbox Code Playgroud)

是否可以使百分比像括号一样工作?输出应该是 h1=test

任何帮助,将不胜感激。

dbe*_*ham 6

你问题中的例子很混乱,但我想我明白你在寻找什么:

@echo off
setlocal

set C1=apple
set C2=orange
set C3=banana

set num=2


:: Inefficient way without delayed expansion
:: This will be noticeably slow if used in a tight loop with many iterations
call echo %%C%num%%%

:: The remaining methods require delayed expansion
setlocal enableDelayedExpansion

:: Efficient way 1
echo(
echo !C%num%!

:: Efficient way 2 - useful if inside parenthesized block
:: where %num% will not give current value
echo(
for %%N in (!num!) do echo !C%%N!

:: Showing all values via a loop
echo(
for /l %%N in (1 1 3) do echo !C%%N!
Run Code Online (Sandbox Code Playgroud)