Page 1 of 1
All Command Output & Errors to txt
Posted: 04 Nov 2015 23:25
by lmstearn
I applied David's last code block for
Script hybrid, which works fine for STDOUT.
Code: Select all
@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment
::--- Batch section within JScript comment ----------------------------
@echo off
::This block of code handles the TEE by calling the internal JScript code
if "%~1"=="_TEE_" (
cscript //E:JScript //nologo "%~f0" %2 %3
exit /b
)
::The rest of your batch script goes here
::This pipes to TEE in append mode
::PUT YR BAT NAME IN HERE
C:\Users\New\Desktop\my.bat | "%~f0" _TEE_ REDIRECToutput.txt 0
exit /b
----- End of JScript comment, beginning of normal JScript ------------------*/
var fso = new ActiveXObject("Scripting.FileSystemObject");
var mode=2;
if (WScript.Arguments.Count()==2) {mode=8;}
var out = fso.OpenTextFile(WScript.Arguments(0),mode,true);
var chr;
while( !WScript.StdIn.AtEndOfStream ) {
chr=WScript.StdIn.Read(1);
WScript.StdOut.Write(chr);
out.Write(chr);
}
Questions:
How do we apply the line "::The rest of your batch script goes here" without errors?
Is there a line of code if we do not wish to append, but overwrite?
Is it possible to modify it in some way to redirect all errors to the output file as well?
Re: All Command Output & Errors to txt
Posted: 05 Nov 2015 12:00
by aGerman
How do we apply the line "::The rest of your batch script goes here" without Errors?
If there is anything else that you want the tee batch should do you could place it there. No idea what you mean with "without error".
Is there a line of code if we do not wish to append, but overwrite?
If you don't pass a second argument (0 in this case) it will overwrite.
Is it possible to modify it in some way to redirect all errors to the output file as well?
Just merge stdout and stderr before you pipe it to the tee batch.
The only line you have to change is
Code: Select all
(2>&1 C:\Users\New\Desktop\my.bat) | "%~f0" _TEE_ REDIRECToutput.txt
C:\Users\New\Desktop\my.bat could look like that for test purposes
Code: Select all
@echo off &setlocal
echo normal output
>&2 echo error output
Re: All Command Output & Errors to txt
Posted: 05 Nov 2015 23:19
by lmstearn
aGerman wrote:If there is anything else that you want the tee batch should do you could place it there. No idea what you mean with "without error".
Probably a PEBCAK issue: I inserted the entire batch file in there and got an error.
Rephrasing the question: what is
recommended to put in there and is there any special wrap or escape sequence required for Batch code?
aGerman wrote:If you don't pass a second argument (0 in this case) it will overwrite.
Thanks!
aGerman wrote:The only line you have to change is
Code: Select all
(2>&1 C:\Users\New\Desktop\my.bat) | "%~f0" _TEE_ REDIRECToutput.txt
C:\Users\New\Desktop\my.bat could look like that for test purposes
Code: Select all
@echo off &setlocal
echo normal output
>&2 echo error output
Like a filter. Beautiful! The only other thing changed was this:
Code: Select all
(2>&1 C:\Users\New\Desktop\my.bat) | "%~f0" _TEE_ C:\Users\New\Desktop\REDIRECToutput.txt 0
else the output file gets buried in the System directory.
Re: All Command Output & Errors to txt
Posted: 06 Nov 2015 07:43
by Squashman
lmstearn wrote:Probably a PEBCAK issue:
Maybe it was an ID 10 T error.
Re: All Command Output & Errors to txt
Posted: 06 Nov 2015 10:32
by lmstearn
Squashman wrote:lmstearn wrote:Probably a PEBCAK issue:
Maybe it was an ID 10 T error.
Or mah batch buggy just ran outta gas.
Re: All Command Output & Errors to txt
Posted: 06 Nov 2015 18:01
by aGerman
OK back to the questions.
Rephrasing the question: what is recommended to put in there and is there any special wrap or escape sequence required for Batch code?
I
guess even that is not your actual question. What I read between the lines is that you want to have all the code in only one file.
Code: Select all
@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment
::--- don't change ----------------------------------------------------------
@echo off
if "%~1"=="_CODE_" (
cd /d "%~dp0"
call :code
exit /b
) else if "%~1"=="_TEE_" (
cscript //E:JScript //nologo "%~f0" %2 %3
exit /b
)
::--- you may change the file name ------------------------------------------
(2>&1 call "%~f0" _CODE_)|"%~f0" _TEE_ C:\Users\New\Desktop\REDIRECToutput.txt 0
pause
exit /b
:code
::--- your code here --------------------------------------------------------
echo normal output
>&2 echo error output
::--- don't change ----------------------------------------------------------
exit /b
----- End of JScript comment, beginning of normal JScript ------------------*/
var fso = new ActiveXObject("Scripting.FileSystemObject");
var mode=2;
if (WScript.Arguments.Count()==2) {mode=8;}
var out = fso.OpenTextFile(WScript.Arguments(0),mode,true);
var chr;
while( !WScript.StdIn.AtEndOfStream ) {
chr=WScript.StdIn.Read(1);
WScript.StdOut.Write(chr);
out.Write(chr);
}
As you can see I included my two test lines. Try to replace it with your code.
else the output file gets buried in the System directory.
You probably run the code in an elevated mode (as Administrator via right click or as SYSTEM via task scheduler). In that case the working directory of both cmd.exe and cscript.exe will be set automatically to %SystemRoot%\system32.
Re: All Command Output & Errors to txt
Posted: 09 Nov 2015 05:08
by lmstearn
Code: Select all
@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment:
::--- Batch section within JScript comment that calls the internal JScript ----
::--- don't change ----------------------------------------------------------
@echo off
if "%~1"=="_CODE_" (
cd /d "%~dp0"
call :code
exit /b
) else if "%~1"=="_TEE_" (
cscript //E:JScript //nologo "%~f0" %2 %3
exit /b
)
::--- This pipes to TEE in append mode: you may change the file name ------------------------------------------
(2>&1 call "%~f0" _CODE_) | "%~f0" _TEE_ C:\Users\New\Desktop\REDIRECToutput.txt 0
pause
exit /b
:code
::--- your code here --------------------------------------------------------
@ECHO OFF
@echo %%~f0 is %~f0 %%~1 is "%~1" %2 %3
echo normal output
>&2 echo error output
::--- don't change ----------------------------------------------------------
goto :eof
::exit /b might be better
----- End of JScript comment, beginning of normal JScript ------------------*/
var fso = new ActiveXObject("Scripting.FileSystemObject");
var mode=2;
if (WScript.Arguments.Count()==2) {mode=8;}
var out = fso.OpenTextFile(WScript.Arguments(0),mode,true);
var chr;
while( !WScript.StdIn.AtEndOfStream ) {
chr=WScript.StdIn.Read(1);
WScript.StdOut.Write(chr);
out.Write(chr);
}
Thanks, to complete my understanding why is the block beginning
if "%~1"=="_CODE_" not after the block beginning
Code: Select all
(2>&1 call "%~f0" _CODE_) | "%~f0" _TEE_ C:\Users\New\Desktop\REDIRECToutput.txt 0
where %~1 is set.
Is it possible in there to run
NotBatch files with typical launch parms:
Code: Select all
<"C:\Users\%Username%\Desktop\NotBatch.txt" >"C:\Users\%Username%\Desktop\output.txt" cmd.exe
Re: All Command Output & Errors to txt
Posted: 09 Nov 2015 15:09
by aGerman
lmstearn wrote:Thanks, to complete my understanding why is the block beginning
if "%~1"=="_CODE_" not after the block beginning
Code: Select all
(2>&1 call "%~f0" _CODE_) | "%~f0" _TEE_ C:\Users\New\Desktop\REDIRECToutput.txt 0
where %~1 is set.
You can't
set a parameter. You only can
pass it. What happens there is that the Batch file is called three times (actually four times).
First you run it via double click. There were no parameters passed. Hence neither _CODE_ nor _TEE_ can be found in %1 because it is empty.
call "%~f0" _CODE_ will be executed now.
That leads the batch file to run itself for the second time. But now _CODE_ was passed and for that reason the :code section is called from within the IF clause.
stdout and stderr are catched in the first instance of the batch process and they are merged via
2>&1. All Messages are in stdout out now and are pending to be redirected via pipeline (|).
The pipeline calls
"%~f0" _TEE_ which means that the batch file runs itself for the third time where _TEE_ was passed. In the ELSE IF clause cscript.exe is called with the batch file as first parameter, the output file as second parameter (and maybe any various third parameter).
Now the batch file runs for the fourth time but interpreted as JScript code with cscript.exe as interpreter. That's the point where stout will be read linewise and written to both window and file.
I'm not familiar with that technique. Sorry.
Re: All Command Output & Errors to txt
Posted: 12 Nov 2015 06:43
by lmstearn
I see now, quite ingenious.
At
SS64 the following FOR specifies
usebackq but doesn't use them. Is this legal or a typo?
Code: Select all
Parse the contents of a file:
...
FOR /F "usebackq tokens=1,2* delims=," %%G IN ("C:\My Documents\my textfile.txt") DO ECHO %%G
However something else is going on here when adding this in the code section we get all the text displayed as desired.
Drop the
usebackq and it only echoes the filename:
Code: Select all
FOR /F "tokens=1,2* delims=," %%G IN ("C:\Users\New\Desktop\my.bat") DO ECHO %%G
Edit: Missed dropping the quotes on the second string:
Code: Select all
FOR /F "tokens=1,2* delims=," %%G IN (C:\Users\New\Desktop\my.bat) DO ECHO %%G
This works. The example in SS64 shows short names- spaces in long filenames are obviously the problem in that case.
Re: All Command Output & Errors to txt
Posted: 12 Nov 2015 07:35
by Squashman
From the FOR command help.
Code: Select all
usebackq - specifies that the new semantics are in force,
where a back quoted string is executed as a
command and a single quoted string is a
literal string command and allows the use of
double quotes to quote file names in
file-set.