批处理文件FOR / f扩展

时间:2009-08-27 08:37:03

标签: batch-file for-loop variable-expansion

我有一个带有目录名的文件(directories.txt),每个文件都在一行上,我想扩展一行

C:\Documents and Settings\%USERNAME%\My Documents

在我的脚本中运行脚本的真实用户名。然而,回声与行完全相同,而%USERNAME%不会扩展。

FOR /f "tokens=*" %%X IN (directories.txt) DO (
    ECHO %%X
)

回显显示“C:\ Documents and Settings \%USERNAME%\ My Documents”而不是C:\ Documents and Settings \ janco \ My Documents

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

我设法使用变量替换:

SETLOCAL ENABLEDELAYEDEXPANSION

FOR /f "tokens=*" %%X IN (directories.txt) DO (
    SET DIR=%%X
    ECHO !DIR:%%USERNAME%%=%USERNAME%!
)