Can the ECHO command pipe double quotes (") to a text file?

I'm trying to write a batch file which ECHOs certain output to a text file.

At the very end of the text file a set of double quotes (") need to appear.

The batch file is creating a .REG file to be imported into Windows.  The first part of a file is simply part of a registry file, followed by the contents of an environment variable, %USERNAME%.

After %USERNAME%, there needs to be a set of double quotes (") at the end so that Windows recognizes this as a registry entry.  

However, when I try to append this to the end of the batch file, it ends up with the " on the next line, for example:

(what I want):


(what I don't want, which is happening now):


Can someone help me figure out how to make it so that the " is appended to the end of the line, with no carriage return after the USERNAME?

I've tried the following, which actually works when I type it from the command prompt, but fails from within a batch file:


  echo %ALPHA% >> myfile.reg

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can use the caret to escape the double quote:

echo %USERNAME%^" >> mygile.reg

Good Luck,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZoplaxAuthor Commented:
Holy crap, that worked; I thought I'd tried the caret but guess not!

Thanks much, Steve.  :-)
That works fine when you are hard coding the quote.

Other similar or more drastic problems can occur when redirecting echo of a variable to a file in that manor.  The reason is the command parser expands the variables before doing redirection, then does not know where the data ends and the redirect begins.  It sees redirect and looks at the last thing before the redirect to guess if it should affect the redirect.
   echo %USERNAME%">>myfile.reg
   echo kujawskir">>myfile.reg
parser thinks " is there to quote a possibly long file name, so echo never sees the ".

By placing the redirection first on the line, the parser will never have something before the redirect to confuse it.  It has no problem finding the space after the file name and before the start of the command, so it sends the rest of the line to echo.  Echo knows what to do with it so it always looks as desired.
     >>myfile.reg echo.%USERNAME%"
     >>myfile.reg echo kujawskir"
parser sets up redirect and send echo the whole thing.

Other problems fixed by placing redirect first include but may not be limited to:
     lines ending in 2 are deleted or not included in output file if not separated by space.
     unwanted space at the end of lines  (This is so harmless most of the time, you don't even notice it.  That makes it a BEAR to figure out where the space is from that one time in 20 that you MUST get rid of that space.)

JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

ZoplaxAuthor Commented:
Thanks K_2K, this is also very useful, once I have more points (I'm currently out, seriously) I will create a question and direct you to it for the points.
Thanks anyway, but no need, just glad you got what you wanted.  You might rather want to use those points to ask a new question sometime later.

I found out the "lines ending in 2 are deleted" problem no longer exists in NT/2K/XP, and I always knew the space problem is our fault for putting one before the redirect to make the line look pretty.  
I'm still kinda partial to having the redirects first tho, even if only to fix stray quotes and make the program line up pretty.

Have a good day,
ZoplaxAuthor Commented:
Thank you!!  :D

hi guys! was hoping someone could help me, you all seem to be the DOS gurus i need
ViRoy, the experts have answered your initial question, please award points and close the topic.

If you want have other questions, then please open a new topic. What you are doing is piggybacking on your initial question. It's against EE rules.

actually, i was trying to redirect attention of certain experts to my query....
i understand this CAN be regarded as piggybacking however this PAQ is completed and i am NOT interfearing.

my question has NOT been answered as 'initial' is not a option. this question right here is dealing with creating dynamic batch files to import into the windows registry... i need a simple command function, the experts commenting in this question are obviously very knowledgable in the area of my question, as it is a VERY specialized question that a very small percentage of EE experts can answer... take a look at my history and youll see i that i do have a history of asking hard to answer questions.
this one in particular is of extreme urgency... not that i think i can bend rules or anything... but honestly, this question is completely done, there are 0 inquiries left and has been formally closed with 'thank you's'

either way, i have been a EE expert for 4 years and understand. please do not worry :)
thanks for your concern,
The experts have given you information to which you state that you'll look at it on Monday (probably). Then you add this question:

"oh yea, would anyone happen to have a detailed list on usage of | < > and any others? "

That's what I was referring to... Maybe piggybacking wasn't a good description. It's asking another question within the original question.
ohh that was a bonus... as i so delicatley stated "happen to have.."
and was not intended to be part of my decisioning process.
i follow ya now on what you mean.
that question is closed now
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.