Batch file FOR/f expansion
Asked Answered
B

1

2

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?

Blackbeard answered 27/8, 2009 at 8:37 Comment(0)
M
2

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%!
)
Mol answered 27/8, 2009 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.