如何在for循环中回显嵌套变量?

时间:2015-05-20 14:08:34

标签: batch-file

这是一个for循环,用于将文本文件的每一行保存到自己的变量中:

@echo off 
setlocal enableextensions enableDelayedExpansion 

set count=0
for /f "tokens=*" %%a in (file.txt) do (
    set /a count=!count! + 1
    set var_!count!=%%a
)

endlocal

我想将每个变量的内容保存到单独的新文本文件中。我怎样才能做到这一点?我尝试了以下内容。但它不起作用,因为!var_!count !!是变量中的变量。

@echo off 
setlocal enableextensions enableDelayedExpansion 

set count=0
for /f "tokens=*" %%a in (file.txt) do (
    set /a count=!count! + 1
    set var_!count!=%%a
    echo !var_!count!!>file_!count!.txt
)

endlocal

1 个答案:

答案 0 :(得分:2)

@echo off 
setlocal enableextensions enableDelayedExpansion 

set count=0
for /f "tokens=*" %%a in (file.txt) do (
    set /a "count+=1"
    set "var_!count!=%%a"
    for %%b in (!count!) do >file_!count!.txt echo !var_%%b!
)

endlocal