# How to write log file for a dos batch file



## The_Champs

Dear Experts,

I have created a batch file with certain DoS commands like copy, del & call. I want to have a log file which can tell me which all commands have been run and what was the output of the command. Read few old posts but couldnt able to understand them.

Pls help. I shall be greatful to you.

Regards,


----------



## Squashman

You need to redirect the output.
http://www.robvanderwoude.com/redirection.php
Post your batch file code and we can help you further.


----------



## TheOutcaste

See, fixed some typos and Squashman beat me to it

Welcome to TSG!

You just need to use Redirection.
A good writeup can be found here: Redirection

You can redirect the output of your file to a log file. If you have not used Echo Off, the commands will be output as well.
You can also redirect the output of each command from inside your batch file, as well as outputting a line to show what it is doing at that point.

For the first, use this:
*myfile.bat >Logfile.txt*
This creates a new file each time it's run
*myfile.bat >>Logfile.txt*
This appends to the *logfile.txt* each time it's run.

Errors still go to the screen. Some commands send info to the Error output, so that goes to the screen as well.
To capture that in a file, add *2>&1* to the line like so:
*myfile.bat >Logfile.txt 2>&1*

Or you can add it inside your file:


Code:


@Echo Off
>>Logfile.txt 2>&1 Echo Deleting files with a .tmp extension
>>Logfile.txt 2>&1 Del C:\Stuff\*.tmp
>>Logfile.txt 2>&1 Echo.
>>Logfile.txt 2>&1 Echo Copying files to backup
>>Logfile.txt 2>&1 Copy C:\Stuff D:\BackupofStuff

I prefer the redirection to be at the start of the line as it's easier to read (won't work with some commands though), but can actually be anyplace in the line, so this will do the same:


Code:


@Echo Off
Echo Deleting files with a .tmp extension>>Logfile.txt 2>&1
Del C:\Stuff\*.tmp>>Logfile.txt 2>&1
Echo.>>Logfile.txt 2>&1
Echo Copying files to backup>>Logfile.txt 2>&1
Copy C:\Stuff D:\BackupofStuff>>Logfile.txt 2>&1


----------



## The_Champs

Dear Experts,

Thanks a lot for your help!!!! You guys rock :up::up::up::up:

Regards,
The_Champs


----------



## The_Champs

Dear Expert,

Thanks a lot for your help!!!! You guys rock :up::up::up::up:

Regards,
The_Champs


----------

