0

I need to have my code come out like this:

if %Psername%==admin ( 
    if %P%==password ( 

But when I put in the code:

echo( if %Psername%==admin ( >>"%HOMEDRIVE%%HOMEPATH%\Orion\Login.bat"
echo(   if %P%==password ( >>"%HOMEDRIVE%%HOMEPATH%\Orion\Login.bat"

it comes out as this in the text file:

if ==admin ( 
if ==password ( 

How to I echo the string %Psername% and %P% literally?

  • Possible duplicate of [Windows Batch Variables Won't Set](https://stackoverflow.com/questions/9681863/windows-batch-variables-wont-set) (assuming, of course, that you've actually set the variables in the first place somewhere higher up in the script that you just aren't showing us.) – SomethingDark Jul 12 '17 at 22:27
  • 2
    To `echo` a literal `%` you need to use `%%` as `%` escapes `%` (ie turns it into an ordinary character). Other characters like `)`, `>`,`<` `=` and `|` require `^` to be prefixed to be escaped. – Magoo Jul 12 '17 at 23:24
  • @SomethingDark I have not set any variables as this code is going to be part of a downloader – TheBatchDude Jul 13 '17 at 20:21
  • You're writing a script to write a script? I wish you had mentioned that; you would have gotten a correct answer sooner. – SomethingDark Jul 13 '17 at 21:20

1 Answers1

0

As @Magoo says, special characters need to be escaped. In this case, the percentage signs(%) require that.

Here is the fixed snippet:

echo( if %%Psername%%==admin ( >>"%HOMEDRIVE%%HOMEPATH%\Orion\Login.bat"
echo(   if %%P%%==password ( >>"%HOMEDRIVE%%HOMEPATH%\Orion\Login.bat"

And please check this page for more information on special characters.

Graham
  • 7,431
  • 18
  • 59
  • 84