Using echo redirects inside of a redirect? (Help)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MasumeX
Posts: 4
Joined: 30 Dec 2011 18:22

Using echo redirects inside of a redirect? (Help)

#1 Post by MasumeX » 20 May 2012 15:49

So I am trying to make my batch file create a new batch file, and then have the created batch file edit a text file. For instance-


echo This is an edit >> text.txt >> text.bat

How would I be able to accomplish this? If you need more information, just reply here. Please help!

MasumeX
Posts: 4
Joined: 30 Dec 2011 18:22

Re: Using echo redirects inside of a redirect? (Help)

#2 Post by MasumeX » 20 May 2012 16:04

Slight edit with that-




echo echo This is an edit >> text.txt >> text.bat

aGerman
Expert
Posts: 4713
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Using echo redirects inside of a redirect? (Help)

#3 Post by aGerman » 20 May 2012 16:19

Escape special characters.
^ to ^^
< to ^<
> to ^>
| to ^|
& to ^&
% to %%

Regards
aGerman

MasumeX
Posts: 4
Joined: 30 Dec 2011 18:22

Re: Using echo redirects inside of a redirect? (Help)

#4 Post by MasumeX » 20 May 2012 16:23

Thank you so much! :D That was a big help! I finished it with-

echo echo This is an edit ^>^> text.txt >> text.bat

Question, Why does this work? I appreciate the solution, but could you help me learn why so I am better knowledged on this? Thank you!

aGerman
Expert
Posts: 4713
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Using echo redirects inside of a redirect? (Help)

#5 Post by aGerman » 20 May 2012 16:35

It's because each of these characters has its own functionality (escaping , redirection, piping, command concatenating, variable marking). You use >> to redirect the echo output to a file. To suppress their functionality and to let the cmd parse it as a literal character you have to escape them.
You could make some tests with the rules I gave you.

Code: Select all

echo ^^^<^>^|^&%%

displays

Code: Select all

^<>|&%

These rules (except the last for %) are not valid for strings enclosed in quotation marks.
Also "EnableDelayedExpansion" changes the behavior for strings in variables and the exclamation mark is another special character that you have to escape in some cases.

Regards
aGerman

MasumeX
Posts: 4
Joined: 30 Dec 2011 18:22

Re: Using echo redirects inside of a redirect? (Help)

#6 Post by MasumeX » 20 May 2012 16:46

That is great to know; thank you so much for your help!

Post Reply