Solved

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

Posted on 2003-11-04
11
2,648 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 20 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 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