JSI Tip 0501 - How do I pipe the entire contents of a batch file (commands and responses) to a log file?

Jerold Schulman

April 21, 1998

1 Min Read
ITPro Today logo in a gray background | ITPro Today

The syntax is:

Drive:BatFolderBatchName.bat>Drive:LogFolderLogName.log 2>&1

To test this, create c:zzzbatch1.bat which contains:

@echo on
REM z: is an invalid drive letter
z:
c:
cd c:zzz
copy batch1.bat copy.tmp
ren copy.tmp ren.tmp
dir
exit


At a command prompt, type:

c:zzzbatch1.bat>c:tempbatch1.log 2>&1

When you open c:tempbatch1.log, you will see:


X:>REM z: is an invalid drive letter

X:>z:
The system cannot find the drive specified.

X:>c:

C:>cd c:zzz

C:ZZZ>copy batch1.bat copy.tmp
1 file(s) copied.

C:ZZZ>ren copy.tmp ren.tmp

C:ZZZ>dir
Volume in drive C is NTDISK
Volume Serial Number is 1C91_4321

Directory of C:ZZZ

04/21/98   11:09a          .
04/21/98   11:09a          ..
04/21/98   11:04a         319 batch1.bat
04/21/98   11:04a         319 ren.tmp
         4 File(s)         638 bytes
             834,271,232 bytes free

C:ZZZ>exit


If you wish to fully log a scheduled job, put the command in another batch file. Example: c:zzzbatch0.bat contains:

c:zzzbatch1.bat>c:tempbatch1.log 2>&1

Then schedule c:zzzbatch0.bat:

AT hh:mm cmd /c "c:zzzbatch0.bat"

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like