Batch file FOR/f expansion

徘徊边缘 提交于 2019-12-11 01:25:07

问题


I have a file (directories.txt) with directory names, each on a single line and I like to expand the line

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

In my script to the real user name running the script. However the echo comes out exactly the same as the line and %USERNAME% does not expand.

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

The echo shows "C:\Documents and Settings\%USERNAME%\My Documents" instead of C:\Documents and Settings\ janco \My Documents

Any ideas?


回答1:


I've managed to do this using variable substitution:

SETLOCAL ENABLEDELAYEDEXPANSION

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


来源:https://stackoverflow.com/questions/1339699/batch-file-for-f-expansion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!