Reputation: 675
I am running some commands and getting the output. Now I want make it automatic. I have created the .bat file but I am unable to save output.
How can save the output after successfully running the command in batch file.
Upvotes: 1
Views: 14842
Reputation: 121649
Two choices:
EXAMPLE:
echo %DATE% %TIME% > mylog.txt
cmd1 >> mylog.txt
cmd2 >> mylog.txt
...
EXAMPLE
call mybatfile.bat > mylog.txt
Side notes:
a. "Text output" actually consists of two, separate "streams": stdout (normal text), and stderr (error text). If you want to redirect both to the same log file, you can use this syntax:
call mybatfile.bat > mylog.txt 2>&1
b. ">"
erases the previous contents before writing. ">>"
appends the new output to the previous contents of the file.
Upvotes: 2
Reputation: 41234
Using code from the answer from @paulsm4:
This method is better than adding a space at the end of each line as there are no extra and unwanted characters.
> mylog.txt echo %DATE% %TIME%
>> mylog.txt cmd1
>> mylog.txt cmd2
The reason why spaces are used in this method is because some numerals get eaten when there is no space.
echo %DATE% %TIME% > mylog.txt
cmd1 >> mylog.txt
cmd2 >> mylog.txt
Upvotes: 0
Reputation: 388
Just add a "> filename" after the command you are calling your script and the output will be written into this file ("filename").
See https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true for more details (appending, ...).
Upvotes: 1