Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

112 views
Skip to first unread message

Aidan Whitehall

unread,
Jan 9, 2009, 10:43:51 AM1/9/09
to
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!

Pegasus (MVP)

unread,
Jan 9, 2009, 11:07:17 AM1/9/09
to

"Aidan Whitehall" <aidan.w...@gmail.com> wrote in message
news:1e51442a-6de4-4664...@p36g2000prp.googlegroups.com...

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!


foxidrive

unread,
Jan 9, 2009, 11:15:53 AM1/9/09
to
On Fri, 9 Jan 2009 07:43:51 -0800 (PST), Aidan Whitehall
<aidan.w...@gmail.com> wrote:

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.


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


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 %%


Aidan Whitehall

unread,
Jan 9, 2009, 12:46:59 PM1/9/09
to
On Jan 9, 4:15 pm, foxidrive <got...@woohoo.invalid> wrote:

> try this.  The leading redirection stops trailing spaces from being added
> in the file also, unless you want them.
>
> >>"C:\somefile.txt" echo/command blah blah
>
> 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!

Pegasus (MVP)

unread,
Jan 9, 2009, 2:09:47 PM1/9/09
to

"Aidan Whitehall" <aidan.w...@gmail.com> wrote in message
news:a00cdd79-1b73-48d9...@n33g2000pri.googlegroups.com...

=======

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

billious

unread,
Jan 9, 2009, 9:45:46 PM1/9/09
to

...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?)


Timo Salmi

unread,
Jan 11, 2009, 2:11:19 PM1/11/09
to
Aidan Whitehall <aidan.w...@gmail.com> wrote:
> but the line
> echo command blah blah >>"C:\somefile.txt"
> isn't being added to somefile.txt. I've tried

You might wish to brush up on poison characters and escape characters in
cmd scripting. E.g.
http://www.netikka.net/tsneti/info/tscmd047.htm
http://www.netikka.net/tsneti/info/tscmd012.htm#poison
http://lipas.uwasa.fi/~ts/http/http2.html#cmdscript

All the best, Timo

--
Prof. Timo Salmi mailto:t...@uwasa.fi ftp & http://garbo.uwasa.fi/
Hpage: http://www.uwasa.fi/laskentatoimi/english/personnel/salmitimo/
Department of Accounting and Finance, University of Vaasa, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.htm

Aidan Whitehall

unread,
Jan 12, 2009, 4:55:36 AM1/12/09
to
On Jan 9, 7:09 pm, "Pegasus \(MVP\)" <I....@fly.com.oz> wrote:

> 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!

Aidan Whitehall

unread,
Jan 12, 2009, 4:57:03 AM1/12/09
to
On Jan 10, 2:45 am, "billious" <billious_1...@hotmail.com> wrote:

> ...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?

Aidan Whitehall

unread,
Jan 12, 2009, 4:57:30 AM1/12/09
to
On Jan 11, 7:11 pm, Timo Salmi <t...@uwasa.fi> wrote:

> You might wish to brush up on poison characters and escape characters in
> cmd scripting. E.g.
>  http://www.netikka.net/tsneti/info/tscmd047.htm
>  http://www.netikka.net/tsneti/info/tscmd012.htm#poison
>  http://lipas.uwasa.fi/~ts/http/http2.html#cmdscript

Will take a look... thanks Timo.

billious

unread,
Jan 12, 2009, 7:40:45 AM1/12/09
to

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.


Al Dunbar

unread,
Jan 12, 2009, 6:50:13 PM1/12/09
to

"foxidrive" <got...@woohoo.invalid> wrote in message
news:5ptem41hgkuecb8c9...@4ax.com...

> On Fri, 9 Jan 2009 07:43:51 -0800 (PST), Aidan Whitehall
> <aidan.w...@gmail.com> wrote:
>
> 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.
>
>
>>>"C:\somefile.txt" echo/command blah blah

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
>>"C:\somefile.txt" echo/command2
>>"C:\somefile.txt" echo/command3 blah blahblah blahblah blahblah blah
>>"C:\somefile.txt" echo/command4 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


0 new messages