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.

1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

> 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);
"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'. "
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, ...).

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.