Windows批处理编程:间接/嵌套变量评估 [英] Windows batch programming: Indirect/nested variable evaluation
本文介绍了Windows批处理编程:间接/嵌套变量评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们有一个列出一系列路径的文本文件,以及从此文件中读取行的批处理文件。
例如,TargetFolders.txt可能包含行:
%ProgramFiles%\Acme\FooBar%VersionNumber%
自然地,当我们从文本文件中读取这行(使用FOR命令)时,变量%%接收到实际的行文本,其中%符号而不是替换变量值。
SET VersionNumber = 7.0
FOR / Feol =; delims =%% I IN(TargetFolders .txt)DO(
echo文件夹:%% I
)
文件夹:%ProgramFiles%\Acme\FooBar%VersionNumber%
如何替换实际的变量值,以便打印
文件夹:C:\Program Files\Acme\FooBar 7.0
?
解决方案
SET VersionNumber = 7.0
FOR / Feol =; delims =%% I IN(TargetFolders.txt)DO(
for / Fusebackq delims =%% J in(`echo %% I`)do echo文件夹:%% J
)
你去。 (这是你想要的,对吧?)
We have a text file that lists a bunch of paths, and a batch file that reads the lines from this file.
For instance, TargetFolders.txt might contain the line:
%ProgramFiles%\Acme\FooBar %VersionNumber%
Naturally, when we read this line from the text file (using a FOR command), the variable %%I receives the actual line text, with the % signs rather than replacing the variable values. So,
SET VersionNumber=7.0 FOR /F "eol=; delims=" %%I IN (TargetFolders.txt) DO ( echo Folder: %%I )
Prints
Folder: %ProgramFiles%\Acme\FooBar %VersionNumber%
How does one make it replace the actual variable values, so that it prints
Folder: C:\Program Files\Acme\FooBar 7.0
?
解决方案SET VersionNumber=7.0 FOR /F "eol=; delims=" %%I IN (TargetFolders.txt) DO ( for /F "usebackq delims=" %%J in (`echo %%I`) do echo Folder: %%J )
There you go. (This is what you wanted, right?)
这篇关于Windows批处理编程:间接/嵌套变量评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文