Solved

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

Posted on 2003-11-04
11
2,641 Views
Last Modified: 2013-11-18
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

0
Comment
Question by:Zoplax
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 30

Accepted Solution

by:
SteveGTR earned 20 total points
Comment Utility
You can use the caret to escape the double quote:

echo %USERNAME%^" >> mygile.reg

Good Luck,
Steve
0
 
LVL 6

Author Comment

by:Zoplax
Comment Utility
Holy crap, that worked; I thought I'd tried the caret but guess not!

Thanks much, Steve.  :-)
0
 
LVL 8

Expert Comment

by:K_2K
Comment Utility
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/)
0
 
LVL 6

Author Comment

by:Zoplax
Comment Utility
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.
0
 
LVL 8

Expert Comment

by:K_2K
Comment Utility
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/)
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 6

Author Comment

by:Zoplax
Comment Utility
Thank you!!  :D
0
 
LVL 8

Expert Comment

by:ViRoy
Comment Utility

hi guys! was hoping someone could help me, you all seem to be the DOS gurus i need
http://oldlook.experts-exchange.com/Operating_Systems/Q_20798417.html
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
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.
0
 
LVL 8

Expert Comment

by:ViRoy
Comment Utility

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
0
 
LVL 30

Expert Comment

by:SteveGTR
Comment Utility
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.
0
 
LVL 8

Expert Comment

by:ViRoy
Comment Utility
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
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now