Link to home
Start Free TrialLog in
Avatar of Zoplax
Zoplax

asked on

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):

"USERNAME"

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

"USERNAME
"

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:

  SET TESTVAR=%USERNAME%"

  echo %ALPHA% >> myfile.reg

ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zoplax
Zoplax

ASKER

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
becomes
   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%"
becomes
     >>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.)

Enjoy,
2K
(\o/)
Avatar of Zoplax

ASKER

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,
2K
(\o/)
Avatar of Zoplax

ASKER

Thank you!!  :D

hi guys! was hoping someone could help me, you all seem to be the DOS gurus i need
http://oldlook.experts-exchange.com/questions/20798417/How-to-use-Format-com-in-DOS.html
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,
         ViRoy
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