1

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.

Ravindra Sinare
  • 675
  • 1
  • 9
  • 25
  • 1
    use something like this ">output.txt" look here: http://stackoverflow.com/a/20484234/3905529 – anion Aug 28 '15 at 08:15

3 Answers3

2

Two choices:

  1. Redirect (">") your commands in the .bat file directly, as you invoke them

EXAMPLE:

echo %DATE% %TIME% > mylog.txt
cmd1 >> mylog.txt
cmd2 >> mylog.txt
...
  1. Create a 2nd .bat file to call the first, and redirect everything in the first one:

EXAMPLE

call mybatfile.bat > mylog.txt
  1. 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.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • `> outfile.txt` or `>> outfile.txt` may also be stated in front of the command, e. g. `> outfile.txt echo Hey.`; if the output file (path) contains spaces, enclose it in a pair of `"` (this might be done in general); – aschipfl Aug 28 '15 at 08:58
  • "command 1>&2 Folderpath\mylog.txt" this is not working in my case. Also tried for multiple combinations. I am gettign this error " *** Unrecognized command line argument 'D:\out.txt'. " – Ravindra Sinare Sep 04 '15 at 09:35
1

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, ...).

fjellfly
  • 388
  • 1
  • 13
0

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
foxidrive
  • 40,353
  • 10
  • 53
  • 68