Write "<" or ">" to a .txt file - batch-file

I want to write "<" or ">" symbol to a text files.
There's an examle of what I tried:
echo echo. >file2.txt>file1.txt
But that doesn't work.
What I want is to make the batch file make file2.txt with this text below:
echo. >file1.txt
Does anybody know how to do that?

Escape the < or > using ^. Note to actually redirect to a file, you'll need an extra redirect operator.
Here's an example using > - the same solution works for < (and % as well).
echo .^> > file.txt
So to get the results you want (a text file containing echo. file2.txt > file1.txt) in a text file named redir.txt:
echo echo. file2.txt ^> file1.txt > redir.txt
The end result in redir.txt:
c:\temp>type redir.txt
echo. file2.txt > file.txt

Is this DOS? Look here:
http://www.robvanderwoude.com/escapechars.php
you have to use a caret "^". echo ^>
PHP use a \
http://php.net/manual/en/regexp.reference.escape.php
Is it Unix? Use quotes.

Related

I Want to Create a Batch File That Creates a Batch File which creates a text file [duplicate]

This question already has answers here:
Escape angle brackets in a Windows command prompt
(6 answers)
using batch echo with special characters
(8 answers)
Closed 9 months ago.
I want to Create a Batch File1 Which Creates a Batch File2, This Batch File2 Should then Create a txt file.
Below is the 4 lines script i Want to Append to Batch File2. I need Code for Batch File1.
Please Help
echo > t.txt
:r
echo 101010101010 >> t.txt
goto r
P.S: I am new to Stack Overflow, only my account is old. until now i did not know it's True
Powers, but i'm realising.. (excuse me for being informal)
Refer to How to Escape Characters
BATCHFILE1.bat that can create the second batch file BATCHFILE2.bat
#echo off
Title I am the BatchFile1.bat who wants to create the BatchFile2.bat
(
echo #echo off
echo echo^>t.txt
echo :r
echo echo 101010101010^>^>t.txt
echo goto r
)>BATCHFILE2.bat
Batch File2.cmd should look like this:
#CD . 1>"t.txt" 2>NUL || Exit /B
:r
#(Echo 101010101010) 1>>"t.txt"
#GoTo r
The first line will create your text file, unless the current directory is not writeable. Should that be the case, your batch file will simply close without entering the loop. The code in the loop will print your required string without including any trailing spaces.In order to stop writing to the file, you will need to enter Ctrl+C
Therefore to output it from Batch File1.cmd you'd need:
#(
Echo #CD . 1^>"t.txt" 2^>NUL ^|^| Exit /B
Echo :r
Echo #(Echo 101010101010^) 1^>^>"t.txt"
Echo #GoTo r
) 1>"Batch File2.cmd"
Using this method, you simply need to escape the usual special characters, plus any nested closing parentheses. In the above your special characters are the redirection and pipe characters.

How to redirect symbol `>` to .txt in batch file

I am trying to redirect the symbol > in a batch file to a .txt file and failing.
Example:
echo > > m.txt
So when I open the file m.txt I should see this:
>
you need to escape some special chars: echo ^> >m.txt
Other characters that need escaping: |&<> and % which is an exception: it is escaped with another %: %%

escape special character (%)

i have a command that displays the current name of the batch file that it's code is appended to.
echo %~n0%~x0
i want to send the command to a text file without changing its syntax.
for example;
echo %~n0%~x0 >> somefile.txt
i tried escaping the percentage sign with another percentage but it doesn't work.
echo %%~n0%%~x0
any ideas...?
Try the caret instead of the %% like this ^% you might also need to escape the escape (I know weird and why .. but ..you might).
Using the CARET ^ worked for me.
echo ^%%~n0^%%~x0 >> somefile.txt
this code will produce the batch file name
echo ^%~n0^%~x0 >> somefile.txt

Use a batch file to write txt to another file

How would i add this text to a file because it seems to get confused with the other greater than less than signs thanks
echo >> C:\docs\thisdoc.txt
If I've got you right, you want to write the text "echo >> c:\docs\thisdoc.txt" in a file? Then you need to escape the ">"characters with "^":
echo echo ^>^> C:\docs\thisdoc.txt > mybatch.cmd

Add a new line to a text file in MS-DOS

I am making a .bat file, and I would like it to write ASCII art into a text file.
I was able to find the command to append a new line to the file when echoing text, but when I read that text file, all I see is a layout-sign and not a space. I think it would work by opening that file with Word or even WordPad, but I would like it to work on any computer, even if that computer only has Notepad (which is mostly the case).
How can I open the text file in a certain program (i.e. WordPad) or write a proper space character to the file?
EDIT:
I found that it is the best way to use:
echo <line1> > <filename>
echo <line2> >> <filename>
P.S. I used | in my ASCII art, so it crashed, Dumb Dumb Dumb :)
echo Hello, > file.txt
echo. >>file.txt
echo world >>file.txt
and you can always run:
wordpad file.txt
on any version of Windows.
On Windows 2000 and above you can do:
( echo Hello, & echo. & echo world ) > file.txt
Another way of showing a message for a small amount of text is to create file.vbs containing:
Msgbox "Hello," & vbCrLf & vbCrLf & "world", 0, "Message"
Call it with
cscript /nologo file.vbs
Or use wscript if you don't need it to wait until they click OK.
The problem with the message you're writing is that the vertical bar (|) is the "pipe" operator. You'll need to escape it by using ^| instead of |.
P.S. it's spelled Pwned.
You can easily append to the end of a file, by using the redirection char twice (>>).
This will copy source.txt to destination.txt, overwriting destination in the process:
type source.txt > destination.txt
This will copy source.txt to destination.txt, appending to destination in the process:
type source.txt >> destination.txt
Maybe this is what you want?
echo foo > test.txt
echo. >> test.txt
echo bar >> test.txt
results in the following within test.txt:
foo
bar
echo "text to echo" > file.txt
Use the following:
echo (text here) >> (name here).txt
Ex. echo my name is jeff >> test.txt
test.txt
my name is jeff
You can use it in a script too.
I always use copy con to write text, It so easy to write a long text
Example:
C:\COPY CON [drive:][path][File name]
.... Content
F6
1 file(s) is copied

Resources