1

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?

Untalented
  • 57
  • 1
  • 3
  • 7
  • are you asking how to write the string "start C:\Users\%USERNAME%\My Documents" to a text document "test.txt"? – Jay Oct 07 '13 at 15:37
  • Yes, without it resolving the environmental variable %USERNAME% and writing it exactly how you stated. – Untalented Oct 07 '13 at 15:47
  • From you tag its a batch file correct? "echo "start C:\Users\%USERNAME%\My Documents" >> Test.txt" works both from the command line and as a batch file.What problem are you having?Where do you want the file to be written by the way?in the C: directory? – Jay Oct 07 '13 at 16:09
  • without resolving username to another value. they want to take the literal string %username% and put it in a file. – cure Oct 07 '13 at 17:29

3 Answers3

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

this should work.

cure
  • 2,588
  • 1
  • 17
  • 25
2

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
foxidrive
  • 40,353
  • 10
  • 53
  • 68
0

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.

Community
  • 1
  • 1
Alcanzar
  • 16,985
  • 6
  • 42
  • 59
  • 1
    Consider expanding your answer to explain to the asker why this achieves the desired result, possibly linking to documentation. As is, this is only marginally useful. – Joshua Dwire Oct 07 '13 at 16:34