1.

Solve : How do I log the output from a .bat job.?

Answer»

Hi,
I'm planning writing a batch job to run every morning at 6:00 AM. I will be required to get the output from the job WRITTEN to a log file.

Any ideas on how to do this?

ThanksThere are 2 ways,
1] rewrite your batch to add '>>logfile' after every command - remembering to blank it at the start
2] run it as a parameter to command.com and redirect the output of command.com to your log :
command /c mybatch.bat > logfile

Good luck
GrahamHi Graham,
Thanks for the tips.

I'm not a DOS expert, so this question may sound a little basic to you.

1) How can I add the date and time to the log file name and

2) How can I direct the log file into a DIRECTORY (folder) so that I can keep a collection of their daily activities?

Thanks,
Les
Code: [Select]if not exist %date% (
md %folder%
)
SET folder=%date%
>>%folder%\%time%.txt

This sets the folder do the date, and then inside that folder it creates %time%.txtJacob
depending on your settings, %date% could contain / characters
I would amend your code a bit like this

Code: [Select]:: remove slashes
set folder=%date:/=_%
if not exist %folder% (
md %folder%
)
>>%folder%\%time%.txt
Baffled - you might like to play around with string slicing to put the date into 'military' format (yyymmdd) so that the folders list nicely alphabetically .... this will depend on your locale, in the uk, you would do
Code: [Select]:: reformat date
set folder=%date:~6,4%%date:~3,2%%date:~0,2%for US format, swap the 3,2 | 0,2
GrahamHi,
thanks for all your help.

I'm having difficulty with the writing to the file within a folder bit.

The code

md %folder%

works ok.

When I try to do the command

>>%folder%\%time%.txt

it fails to write ANYTHING. So I tried

>>fred2\fred.txt

and this worked. So I tried: -

>>%folder%\fred.txt

this failed to write anything.

Any ideas as to what I'm doing wrong?

ThanksDo the folder or file name have any spaces?

Try USING quote marks like this

>>"%folder%\%time%.txt"Hi,
Thanks again. That worked.

Can you tell me how to display comments including the date and time in the output log file which I'm creating?

Thanks Code: [Select]echo This is a comment >> logfile.txt
echo The date today is %date% >> logfile.txt
echo The time now is %time% >> logfile.txt
Thanks,
You're all geniuses.

It mow works perfectly.


Cheers...  There may be a problem caused if you echo output successively to a file called %time%.txt. This is because the system variable %time% contains the system time at the time the variable is read by the batch file. Obviously, as everybody knows, time does not stand still. So %time% changes.

Quote

C:\>echo %time%
10:14:45.42

As you can see, the time is expressed in hours, minutes, seconds and hundredths of a second

Consider the following

Code: [Select]echo off
echo Starting process >> %time%.txt
Process.exe
echo Finished process >> %time%.txt
Unless the process starts and finishes in less than 0.01 second, the two echo statements are going to output to 2 files with different names.


call your batch file mybatch.bat

Code: [Select]echo off
IF %1==logged goto :code
echo. >>logfile.log
echo %DATE% >>logfile.log
echo //START OUTPUT >>logfile.log
call %comspec% /c mybatch.bat logged >>logfile.log
echo //END OUTPUT >>logfile.log
exit
:code
[your code goes here]
try that


Discussion

No Comment Found