Ways to create a file with the echo command:
ECHO. > example.bat (creates an empty file called "example.bat")
ECHO message > example.bat (creates example.bat containing "message")
ECHO message >> example.bat (adds "message" to a new line in example.bat)
(ECHO message) >> example.bat (same as above, just another way to write it)
If you want to create a file via the ECHO
command, in a specific directory on your computer, you might run into a problem.
ECHO Hello how are you? > C:\Program Files\example.bat
(This will NOT make a file in the folder "Program Files", and might show an error message)
But then how do we do it? Well it's actually extremely simple... When typing a path or file name that has a space included in it's name, then remember to use "quotes"
ECHO Hello how are you? > "C:\Program Files\example.bat"
(This will create "example.bat" in the folder "Program Files")
But what if you want to make a file that outputs a new file?
ECHO message > file1.bat > example.bat
(example.bat is NOT going to contain "message > file1.bat")
example.bat will just contain "message"... nothing else
Then how do we do this? Well for this we use the ^
symbol.
ECHO message ^> file1.bat > example.bat
(example.bat is going to contain "message > file1.bat")
Same goes for other stuff in batch
The next step requires you to have some knowledge about variables and statements:
click here to learn about variables | click here to learn about if and else statements
Variables:
SET example="text"
ECHO %example% > file.bat
(This will output "text" to file.bat)
if we don't want it to output "text" but just plain %example% then write:
ECHO ^%example^% > file.bat
(This will output "%example%" to file.bat)
IF/ELSE statements:
ELSE statements are written with "pipes" ||
IF ^%example^%=="Hello" ECHO True || ECHO False > file.bat
(example.bat is going to contain "if %example%=="Hello" echo True")
[it ignores everything after the ELSE statement]
to output the whole line then we do the same as before.
IF ^%example^%=="Hello" ECHO True ^|^| ECHO False > file.bat
This will output:
IF %example%=="Hello" ECHO True || ECHO False
If the variable is equal to "Hello" then it will say "True", ELSE it will say "False"