?
Solved

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

Posted on 2003-11-04
11
Medium Priority
?
2,653 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 30

Accepted Solution

by:
SteveGTR earned 80 total points
ID: 9680399
You can use the caret to escape the double quote:

echo %USERNAME%^" >> mygile.reg

Good Luck,
Steve
0
 
LVL 6

Author Comment

by:Zoplax
ID: 9680441
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
ID: 9687324
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Author Comment

by:Zoplax
ID: 9689109
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
ID: 9689456
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
 
LVL 6

Author Comment

by:Zoplax
ID: 9689552
Thank you!!  :D
0
 
LVL 8

Expert Comment

by:ViRoy
ID: 9750789

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
ID: 9751554
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
ID: 9751868

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
ID: 9751896
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
ID: 9765144
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

765 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