Writing the entire cmd command in a file that contains different characters like ">" or ">>",
I want to write this code in a file named final.txt:
@echo off dir file1.txt > file2.txt
I've tried something like this:
"@echo off dir file1.txt > file2.txt"
But he only writes
"@echo off dir file1.txt > "
It doesn't copy the whole command, and the reversed commas are copied as well.
Some characters have special meaning and are performed instead of echoes. >
is one of them. to echo them again, you must "escape" them (with another special char, the caret ^
). Also, to execute two commands on the same line, you must separate them by &
(another of these special characters). Last but not least, to be able to execute the final file, the extension must be .bat
or .cmd
. And you will need more echo
to repeat @echo ...
:
echo @echo off & dir file1.txt ^> file2.txt > final.bat
But it final.bat
will be more readable if you write each line on your own line:
echo @echo off>final.txt
echo dir file1.txt ^>file2.txt >>final.bat
( >>
used to append to a file instead of overwriting it)
just to note: echo
is not the only command that can be pacified with help @
. Shorter code with the same effect:
echo @dir file1.txt^>file2.txt>>final.bat
source to share