How to echo the string "command" into a file?

  • Thread starter Aidan Whitehall
  • Start date
A

Aidan Whitehall

This is for a batch file that runs on XP... apologies if this is
strictly a Win2000-only group, but couldn't find a "scripting'" group
under microsoft.public.windowsxp...


I'm using a batch file to echo multiple lines into a file, like so:

echo blah blah >"C:\somefile.txt"
echo blah blah >>"C:\somefile.txt"
echo blah blah >>"C:\somefile.txt"

but the line

echo command blah blah >>"C:\somefile.txt"

isn't being added to somefile.txt. I've tried

echo ^command blah blah >>"C:\somefile.txt"

but still no joy.

Does anyone know how to escape the string "command" such that the line
will be appended to the text file?

Thanks!
 
P

Pegasus \(MVP\)

Aidan Whitehall said:
This is for a batch file that runs on XP... apologies if this is
strictly a Win2000-only group, but couldn't find a "scripting'" group
under microsoft.public.windowsxp...


I'm using a batch file to echo multiple lines into a file, like so:

echo blah blah >"C:\somefile.txt"
echo blah blah >>"C:\somefile.txt"
echo blah blah >>"C:\somefile.txt"

but the line

echo command blah blah >>"C:\somefile.txt"

isn't being added to somefile.txt. I've tried

echo ^command blah blah >>"C:\somefile.txt"

but still no joy.

Does anyone know how to escape the string "command" such that the line
will be appended to the text file?

Thanks!

There is nothing special about the word "command". If it does not work for
you then some non-Windows program is intercepting the command. Try it in
Safe Mode!
 
F

foxidrive

On Fri, 9 Jan 2009 07:43:51 -0800 (PST), Aidan Whitehall

This group is fine for XP etc. It is also on topic in alt.msdos.batch.nt
I'm using a batch file to echo multiple lines into a file, like so:

echo blah blah >"C:\somefile.txt"
echo blah blah >>"C:\somefile.txt"
echo blah blah >>"C:\somefile.txt"

but the line

echo command blah blah >>"C:\somefile.txt"

isn't being added to somefile.txt. I've tried

echo ^command blah blah >>"C:\somefile.txt"

but still no joy.

Does anyone know how to escape the string "command" such that the line
will be appended to the text file?

try this. The leading redirection stops trailing spaces from being added
in the file also, unless you want them.



The / is one of many characters that can be used and is the most
compatible, though not always perfect.

Characters like <>|& still need to be escaped with ^ and % is best entered
as %%
 
A

Aidan Whitehall

try this.  The leading redirection stops trailing spaces from being added
in the file also, unless you want them.


The / is one of many characters that can be used and is the most
compatible, though not always perfect.

Characters like <>|& still need to be escaped with ^ and % is best entered
as %%

Thanks all for the info.

Turns out I had (stupidly) missed the % further along the line...
escaping that by doubling it up fixed the problem.

Thanks again for the help... appreciated!
 
P

Pegasus \(MVP\)

try this. The leading redirection stops trailing spaces from being added
in the file also, unless you want them.


The / is one of many characters that can be used and is the most
compatible, though not always perfect.

Characters like <>|& still need to be escaped with ^ and % is best entered
as %%

Thanks all for the info.

Turns out I had (stupidly) missed the % further along the line...
escaping that by doubling it up fixed the problem.

Thanks again for the help... appreciated!
=======

Strange - I can't see any %, missing or otherwise, in the example you gave
us . . .

echo command blah blah >>"C:\somefile.txt"
 
B

billious

Aidan said:
Thanks all for the info.

Turns out I had (stupidly) missed the % further along the line...
escaping that by doubling it up fixed the problem.

Thanks again for the help... appreciated!

....and perhaps you should try alt.msdos.batch.nt for batch questions and
tips - it probably has twenty times the traffic you'll find here!

(but please - the code-frag that you're using, OK?)
 
A

Aidan Whitehall

Strange - I can't see any %, missing or otherwise, in the example you gave
us . . .

echo command blah blah         >>"C:\somefile.txt"

Yeah, I usually tidy up problematic code when posting as I've found
that great swathes of code can both obfuscate the cause of the problem
and deter others from assisting.

In this instance I also managed to tidy up the cause of the problem...
my bad!
 
A

Aidan Whitehall

...and perhaps you should try alt.msdos.batch.nt for batch questions and
tips - it probably has twenty times the traffic you'll find here!

I'll bear that group in mind... thanks.

(but please - the code-frag that you're using, OK?)

Does that mean prefix lines of code with dashes?
 
B

billious

Aidan said:
Does that mean prefix lines of code with dashes?

No - minimum editing; disguise credit-card numbers, MAC addresses, real
names etc., but otherwise cut-and-paste exactly the code you are using - a
sufficiently large codefragment to show the problem without swamping - for
the reasons you mentioned.

Indicating the start of lines is useful. I use a (modified form of)

FIND /N /V "" <batchfile.bat >batchfile.txt
NOTEPAD batchfile.txt

which numbers each of the batchfile lines in [brackets], creates a textfile
and opens it in notepad ready to cut-and-paste - but whatever come out clear
as batch syntax can sometimes be persnicketty, as you'd know.
 
A

Al Dunbar

foxidrive said:
On Fri, 9 Jan 2009 07:43:51 -0800 (PST), Aidan Whitehall

This group is fine for XP etc. It is also on topic in alt.msdos.batch.nt


try this. The leading redirection stops trailing spaces from being added
in the file also, unless you want them.

Another advantage of the leading redirection is that it is easier to verufy
the correctness of the redirection syntax, as this:

echo/command1 blah blah blah blah >"C:\somefile.txt"
echo/command2 >>"C:\somefile.txt"
echo/command3 blah blahblah blahblah blahblah blah >>"C:\somefile.txt"
echo/command4 blah >>"C:\somefile.txt"

becomes this:
"C:\somefile.txt" echo/command1 blah blah blah blah

However, there is an even simpler way to accomplish this:

(
echo/command1 blah blah blah blah
echo/command2
echo/command3 blah blahblah blahblah blahblah blah
echo/command4 blah
)>"C:\somefile.txt"

The advantages are that the individual command lines do not include the
redirection, so are even easier to read, and that the output file is not
written and appended to multiple times.

One final point: if it is critcal that trailing blanks either be suppressed
or preserved, this can be made more obvious with parentheses:

(
(echo/no trailing blank)
(echo/trailing blanks )
)>"C:\somefile.txt"
The / is one of many characters that can be used and is the most
compatible, though not always perfect.
exactly.

Characters like <>|& still need to be escaped with ^ and % is best entered
as %%

.... and all are best handled by avoiding their use (whenever possible).

/Al
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top