Link to home
Start Free TrialLog in
Avatar of E=mc2
E=mc2Flag for Canada

asked on

Add option to display to screen in addition to logging a file in a .bat routine

I would like to change this .bat file so that in addition to creating a log file that I am also able to see the activity displayed on my monitor as the script is running. 


What change would I need to make to this script?

Thanks in advance for your assistance. 


E:\Users\Users1\Scripts\Go.bat>"E:\Users\Users1\Logs\WithGobatlog.log" 2>&1

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

There is nothing in-built to allow for duplicating a file stream. A Windows port of the Unix TEE command is what I  used most in the past, but you can also use something like:

E:\Users\Users1\Scripts\Go.bat  2>&1 |  (@for /F "delims=" %L in ('more') do @echo %L >con& @echo %L) > "E:\Users\Users1\Logs\WithGobatlog.log" 

Open in new window

Or create a .cmd/.bat file tee.cmd

@echo off
for /F "delims=" %%L in ('more') do echo %%L >con& echo %%L

Open in new window

and use it like so:

E:\Users\Users1\Scripts\Go.bat  2>&1 |  tee.cmd > "E:\Users\Users1\Logs\WithGobatlog.log" 

Open in new window


Avatar of E=mc2

ASKER

Qlemo, thanks you, when I use the tee.cmd nothing displays to the screen. 

Avatar of E=mc2

ASKER

Thanks oBdA for the various suggestions.

Apart from the UnixTee utility, the first method might take me a long time to recode, and the second PowerShell method works well. 

With PowerShell unfortunately I colour coded some of the echo lines and of course I am not seeing those when using PowerShell. 

Avatar of E=mc2

ASKER

Qlemo, it seems that none of the methods work for me, either it blips or it runs but it does not output to the screen. 

Avatar of oBdA
oBdA

Can't reproduce; it shows the colors fine both when called from cmd.exe and from a powershell console.
Be aware that the escape codes will end up in the log as well (and the colors will work for the log, too, when viewed in the console).
@echo off
setlocal

REM Set the variable esc to Char(27)
powershell.exe -Command "Set-Content -Value ([char]27) -LiteralPath '%Temp%\esc.txt'" >NUL
for /f %%a in ('type "%Temp%\esc.txt"') do set esc=%%a

echo Hello World
echo %esc%[92mHello World%esc%[0m

Open in new window

User generated imageUser generated image
Avatar of E=mc2

ASKER

Thank you.  For the Tee-Object to work does tee.exe from https://unxutils.sourceforge.net need to be installed?

And I am running a .bat as opposed to a .cmd file would that make a difference?

No; that's a built-in cmdlet.
Just in case, as far as tee.exe is concerned: tee.exe is a stand-alone application; you can just extract it from the archive and put it in any folder. No need to "install" anything.
Avatar of E=mc2

ASKER

Thanks oBdA, actually when I opened the PowerShell file and ran it within, that's when I didn't see the colours. But when I right clicked on the file and ran it with PowerShell, the colours did show.   I presume when I call on it within a .bat file this will be ok.      

Ok and thanks very much for the information about the tee.exe as well, much appreciated.

Avatar of E=mc2

ASKER

Thanks again oBdA, this works.  My only loss during this process using the PowerShell script and the Tee Object is that where I have 'cls' in the script, that does not actually refresh when it's running this way.  However at least I get the log which is what I really needed.   Many thanks.

No clue why my suggestions didn't work. It does for me.