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

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 ] 
3

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

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.

2

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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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