Untalented
Untalented

Reputation: 157

Read / Echo Environmental Variable as Literal

Very basic, or so I thought. I've got this string in a command prompt:

C:\>echo "start C:\Users\%USERNAME%\My Documents" >> Test.txt

I've tried %%USERNAME%%, '%USERNAME%', '%'USERNAME'%', and many other ways. The batch output always resolves the environmental variable rather than writing it as a literal string of text. Is it possible to make sure it reads this as a literal string of text and not the environment variable it resolves to?

Upvotes: 1

Views: 1468

Answers (3)

foxidrive
foxidrive

Reputation: 41234

This command works differently at the command line Vs a batch file. In a batch file this works:

@echo off
echo start "" "C:\Users\%%username%%\my documents" >> file.txt

Upvotes: 2

Alcanzar
Alcanzar

Reputation: 17155

No idea why your method doesn't work (I couldn't make it work either), but if you do the echo in two parts like this:

echo.|set /P="start C:\Users\%" >> Test.txt
echo USERNAME%\My Documents >> Test.txt

Then it should work. The first line is something I found here: 'echo' without newline in a shell script

The docs do say that %% should put a literal % in, but apparently the rule to put %USERNAME% has a higher precedence.

Upvotes: 0

cure
cure

Reputation: 2688

echo start ^"^" ^"C:\Users\^%username^%\my documents^" >> test.txt

this should work.

Upvotes: 3

Related Questions