20

I'm using a command line script to adjust multiple settings on a computer. However, I would like to have the entire output to also be logged into a .txt or .log file.

When I use my basic knowledge of the logging system it will put the output in a file but not actually execute it. In my case I would need it executed and then logged into a file for later reference.

Would someone be able to tell me how to do this?

0

4 Answers 4

19

If the question asks for a script to be "executed" and the entire batch file be output to a log file in Windows 8.1, then here is the simple answer:

Include this at the start of your batch file...

@echo off
set LOGFILE=batch.log
call :LOG > %LOGFILE%
exit /B

:LOG
[ your script goes here ]
5
  • Excellent, thank you! (Of course, %LOGFILE% isn't really needed here. :P)
    – Andrew
    Oct 27, 2017 at 22:22
  • 5
    My script poses a question - I'm not seeing it if this is used. How to log everything and see it on screen while executing?
    – Hrvoje
    Feb 7, 2019 at 13:00
  • Yeah, how would you do that so that you can see it while it is on your screen? Mar 12, 2020 at 21:28
  • 1
    In case anyone else has the same question as @Hrvoje, UnxUtils has a program in it called tee (also available with most Linux and Unix-based distributions, BTW) that will log output to a specified file AND output it to the screen. Use in this case: call :LOG | tee %LOGFILE% (not sure if this will work in this case, but it's a small download - might be worth a try) Jun 15, 2022 at 6:14
  • Not sure why, but call docs actually say: "Do not use pipes (|) or redirection symbols (< or >) with call."
    – djvg
    Sep 21, 2022 at 9:59
4

In your question, you mention:

"... it will put the output in a file but not actually execute it. In my case I would need it executed and then logged into a file for later reference."

Since you say the program is running and its output is being put into the file, I thought that you might have meant "displayed", instead of "executed".

If that is not what you meant, then it would have probably helped if that was explained better, perhaps with some sample output.

In any case, I'm posting this answer in case there are others who find this question/answer helpful.

So, basically, it sounds like you want the output of a script to be captured to a file, and to also be able to see the output of the script on the screen while the script is running.

(tl;dr version: use wintee, like this:

script 2>&1 | wtee logfile.txt)


For this post, I will be using a small test batch file, but your script could be as big and complicated or as simple as you need:

C:\>type a.cmd
@echo off

echo Command: "dir /b a*"
dir /b a*
echo.

echo Command: "dir /b non-existant-file"
dir /b non-existant-file
echo.


This is what happens when I run this batch script:

C:\>a.cmd
Command: "dir /b a*"
a.cmd

Command: "dir /b non-existant-file"
File Not Found

Notice that in the test script, the first execution of the "dir" command is successful, and the second one fails. I do this only to show what will happen with "error messages" when you run your script.

If I run the script and use redirection (">") to capture the output, I will see this

C:\>a.cmd > log.txt
File Not Found

C:\>type log.txt
Command: "dir /b a*"
a.cmd

Command: "dir /b non-existant-file"

C:\>

Notice the error message "File Not Found" was shown on the screen when the script was run, and was not actually captured into the file. That's because ">" captures "normal output" which has been sent to the STDOUT stream. "Error messages" are normally sent to the STDERR stream.

To capture "normal output" and "error messages", you need to also capture the STDERR stream, which is indicated by the "2" in "2>&1" in the command here:

C:\>a.cmd > log.txt 2>&1
C:\>type log.txt
Command: "dir /b a*"
a.cmd

Command: "dir /b non-existant-file"
File Not Found

In unix, there is a standard command: "tee"

Using the "tee" command, you can capture output from a program, and also display the output to the screen, at the same time.

The "tee" command is not standard with windows, but you can download a free version of "tee" for windows here: wintee. The downloaded program is named: "wtee.exe".

You use the "wtee.exe" program as shown below.

This will capture the script output to the file named "log.txt" as before, and it will also display the output to the screen while the script is running:

C:\>a.cmd 2>&1 | wtee log.txt
Command: "dir /b a*"
a.cmd

Command: "dir /b non-existant-file"
File Not Found

C:\>type log.txt
Command: "dir /b a*"
a.cmd

Command: "dir /b non-existant-file"
File Not Found
1
  • worth adding that tee is normally a part of Git distribution on Windows - you can find it in "C:\Program Files (x86)\Git\usr\bin"
    – AntonK
    May 15, 2022 at 10:03
1

This script will execute ls and log its output to a file called log.txt:

exec >log.txt 2>&1
ls

The log will not be executed.

3
  • 1
    Note that this assumes Unix/Linux, but my guess is the OP talks about Windows. It's also not running a script, but logging the output of the commands you enter and omits the critical information of how to stop the madness ...
    – Sven
    Jan 4, 2014 at 12:30
  • I'm indeed talking about Windows. Thanks though :) Jan 4, 2014 at 12:33
  • Actually, just testing it under Windows 10 in a .bat file. script param1 param2 param3 > my_awesome.log 2>&1 works pretty well. Mar 3 at 9:08
0

I would like to have the entire output to also be logged into a .txt or .log file.

That means beside outputting to console you also want to store that in a file? That's what tee is used for

If you have GNU tools on your PC then just use

your_commands.bat "your" "args" | tee output.log

If you don't have GNU tools then PowerShell already has the Tee-Object cmdlet that you can use. It also has an alias of tee. You can run these in PowerShell

cmd /c 'your_commands.bat "your" "args"' | Tee-Object output.log
.\your_commands.bat "your" "args" | Tee-Object output.log
.\your_commands.bat "your" "args" | tee output.log

If you must run the commands in cmd then call PowerShell from cmd

your.bat "your" "args" | powershell -Command "$Input | Tee-Object output.log"
your.bat your args | powershell -C "$Input | tee output.log"

Of course you can also save powershell -C "$Input | tee output.log" to tee.bat then call your.bat | tee.bat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.