@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);
}
运行此批处理文件后,screenOut.txt 文件的内容如下:
C:\Documents and Settings\Antonio\My Documents\test
>if defined Restart goto Begin
C:\Documents and Settings\Antonio\My Documents\test
>rem Place your Batch code here; for example:
C:\Documents and Settings\Antonio\My Documents\test
>rem The date: 05/06/2013, the time: 14:46:54.65
C:\Documents and Settings\Antonio\My Documents\test
>dir TeeMyself.*
Volume in drive C has no label.
Volume Serial Number is CCA1-5338
Directory of C:\Documents and Settings\Antonio\My Documents\test
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 Settings\Antonio\My Documents\test
>rem An error message sent to STDERR
C:\Documents and Settings\Antonio\My Documents\test
>verify bad
An incorrect parameter was
entered for the command.
C:\Documents and Settings\Antonio\My Documents\test
>goto :EOF
屏幕的内容是这样的:
C:\Documents and Settings\Antonio\My Documents\test
>if defined Restart goto Begin
C:\Documents and Settings\Antonio\My Documents\test
>rem TeeMyself.bat: Send output of this Batch file into screenOut.txt file
C:\Documents and Settings\Antonio\My Documents\test
>rem Antonio Perez Ayala
C:\Documents and Settings\Antonio\My Documents\test
>set Restart=True
C:\Documents and Settings\Antonio\My Documents\test
>call "C:\Documents and Settings\Antonio\My Documents\test\TeeMyself.bat" 2>&1 | Cscript //nologo //E:JScript "C:\Documents and Settings\Antonio\My Documents
\test\TeeMyself.bat" 2>screenOut.txt
===================================================
The same contents of screenOut.txt file goes here
===================================================
C:\Documents and Settings\Antonio\My Documents\test
>set Restart=
C:\Documents and Settings\Antonio\My Documents\test
>goto :EOF
您应该使用"%~DP0screenOut.txt"
名称以便在批处理文件的同一文件夹中创建文本文件。