add command INSIDE batch file to generate text log (launched by double click)
add command INSIDE batch file to generate text log (launched by double click)
Advanced thanks to anyone who is stopping by to read this. Here's my scenario:
SCENARIO
ISSUES
Although the file and commands work perfectly for my needs, the results are too long for cmd (even after maximizing buffers in cmd settings); by the time the last command is executed, I can no longer scroll back up to the first commands -- cmd has purged them due to a space limitation.
GOAL
I would like a way for all of the above to continue as-is (even if I can't read all the way up in CMD) but also to add a command INSIDE the .bat file to SIMULTANEOUSLY output all results to a text file (exactly what appears onscreen: successes, failures, errors -- EVERYTHING)
Does such a command exist?
Also, if anyone knows a workaround to running out of space in cmd, I would love that too. Thanks again!
<
Or switch to PowerShell, which has a
Tee
cmdlet.– Ansgar Wiechers
Jun 5 '13 at 11:32
Tee
Using some version of tee (not native to Windows CMD prompt, but available for Windows) is a good option. But if all you need to do is see the content that scrolled off the top of the window, simply edit the window properties. Right click on the window title bar and select Properties, then Layout tab. Set your Buffer Width and Height to larger values (significantly larger than Window Size). I routinely set my Width to 1000 and Height to 3000, and then I am able to scroll to see nearly everything I ever need. Obviously the change must be made before executing your command.
– dbenham
Jun 5 '13 at 19:14
@dbenham: Good advice Dave, but the OP execute the Batch file via a double-click from the explorer! I used to create a shortcut to MS-DOS icon and modify its properties, but I don't know how to modify the properties of the cmd.exe session the system uses when it execute a Batch file via a double-click...
– Aacini
Jun 5 '13 at 20:23
@Aacini - There is an option to save the settings automagically for the double click short cut. I don't remember what I did, but it was simple. But upon re-reading the OP's question, I see that the buffers are already maxed out, so my advice doesn't help in this case.
– dbenham
Jun 5 '13 at 20:45
2 Answers
2
Assuming your batch files are not interactive and you do not need to see them as they run, then this will give you a log:
@echo off
call "d:path hereto foldermybatch" >"z:logsmybatch.log" 2>&1
start "" notepad "z:logsmybatch.log"
stderr needs to be redirected to stdout (
2>&1
), and the OP might prefer to simply TYPE the resultant log.– dbenham
Jun 5 '13 at 14:05
2>&1
Good point Dave. I've added the STDERR redirection. The OP mentioned that the CMD buffer is not large enough for his scripts output, so typing the file will lead to the same issue. :)
– foxidrive
Jun 5 '13 at 18:24
They could also use
more
instead of type
.– Andriy M
Jun 9 '13 at 20:01
more
type
@if (@CodeSection == @Batch) @then
if defined Restart goto Begin
rem TeeMyself.bat: Send output of this Batch file into screenOut.txt file
rem Antonio Perez Ayala
set Restart=True
call "%~F0" 2>&1 | Cscript //nologo //E:JScript "%~F0" 2>screenOut.txt
set Restart=
goto :EOF
:Begin
rem Place your Batch code here; for example:
rem The date: %date%, the time: %time%
dir TeeMyself.*
rem An error message sent to STDERR
verify bad
goto :EOF
@end
// JScript section
while ( ! WScript.Stdin.AtEndOfStream ) {
// Read the next line from Stdin
var line = WScript.Stdin.ReadLine();
// Duplicate the line to Stdout and Stderr
WScript.Stdout.WriteLine(line);
WScript.Stderr.WriteLine(line);
}
After run this Batch file the contents of screenOut.txt file is this:
C:Documents and SettingsAntonioMy Documentstest
>if defined Restart goto Begin
C:Documents and SettingsAntonioMy Documentstest
>rem Place your Batch code here; for example:
C:Documents and SettingsAntonioMy Documentstest
>rem The date: 05/06/2013, the time: 14:46:54.65
C:Documents and SettingsAntonioMy Documentstest
>dir TeeMyself.*
Volume in drive C has no label.
Volume Serial Number is CCA1-5338
Directory of C:Documents and SettingsAntonioMy Documentstest
05/06/2013 02:43 p.m. 748 TeeMyself.bat
1 File(s) 748 bytes
0 Dir(s) 15,004,553,216 bytes free
C:Documents and SettingsAntonioMy Documentstest
>rem An error message sent to STDERR
C:Documents and SettingsAntonioMy Documentstest
>verify bad
An incorrect parameter was
entered for the command.
C:Documents and SettingsAntonioMy Documentstest
>goto :EOF
And the contents of the screen is this:
C:Documents and SettingsAntonioMy Documentstest
>if defined Restart goto Begin
C:Documents and SettingsAntonioMy Documentstest
>rem TeeMyself.bat: Send output of this Batch file into screenOut.txt file
C:Documents and SettingsAntonioMy Documentstest
>rem Antonio Perez Ayala
C:Documents and SettingsAntonioMy Documentstest
>set Restart=True
C:Documents and SettingsAntonioMy Documentstest
>call "C:Documents and SettingsAntonioMy DocumentstestTeeMyself.bat" 2>&1 | Cscript //nologo //E:JScript "C:Documents and SettingsAntonioMy Documents
testTeeMyself.bat" 2>screenOut.txt
===================================================
The same contents of screenOut.txt file goes here
===================================================
C:Documents and SettingsAntonioMy Documentstest
>set Restart=
C:Documents and SettingsAntonioMy Documentstest
>goto :EOF
You should use "%~DP0screenOut.txt"
name in order to create the text file in the same folder of the Batch file.
"%~DP0screenOut.txt"
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Such a command does not exist. You have to use tee, GNUWin or batch version.
– Endoro
Jun 5 '13 at 4:27