Trying to output a scheduled task to a log file which has the date in the filename

Wondering if anyone knows if this is possible:

I am trying to schedule a batch file and output the results to a text file that contains the date in the filename.

I have tried the following and it doesn't work. The task runs with an 0x1 in the last Result.

C:\Test\test.bat > log_%DATE%.txt

Typing %DATE% into DOS gives me 07 but the scheduled task doesn't recognise this variable.

Thanks for your help.
LVL 14
Hedley PhillipsOwnerAsked:
Who is Participating?
 
Steve KnightIT ConsultancyCommented:
Just use the above code %date:/=-% in place of your %date% and it should work.  I just created a simple batch file:

rem 1.cmd
route print

Then set a scheduled task which ran

c:\1.cmd > c:\%date:/=-%.txt 2>&1

Ran the task and ended up with a file called c:\14-11-2008.txt with the output of route print.

When I setup the scheduled task (this was on vista) it whinged that there was text after the executable name but it worked fine...

The 2>&1 redirects any error message aswell as the normal output into the same file.  You coudl send to different files if you want, e.g.

c:\test.bat > "C:\normalog %date:/=-%.txt" 2>"c:\errorlog %date:/=-%.txt"

You could use %time% too if you want a date and time stamps.  Same sort of issue here in that time has a : in which you can't use in filenames so use

%time::=% to remove :'s and leave 135300 for instance for 13:53:00 or %time::=-% to replac them with -'s etc.

Hope that helps

Steve
0
 
Steve KnightIT ConsultancyCommented:
It fails because of the / in the filename interpreted as directory seperator (\).  Easiest quickest dirtiest way if you want to is :

%date:/=-% to replace /'s with -'s or
%date:/=% to remove /'s altogether.

You can fiddle about with extracting parts of the string to get other date formats etc. but this will generally work.

Steve
0
 
Hedley PhillipsOwnerAuthor Commented:
Sorry for the delay in replying. We have had a 'fun' few days involving a disabled write cache on our SAN box and all sorts of odd and bizarre unrelated network issues. Thank goodness it is Friday.

I'm a bit confused by your reply and would appreciate some further explanation.

Is what you have written to be included in the batch file, and it is the batch file that creates and writes to a log file? As I can't see how to enter any of the above into the Scheduled Tasks path window.

Thanks, and sorry for being so dim.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Hedley PhillipsOwnerAuthor Commented:
Increased points as 125 was just mean.
0
 
Hedley PhillipsOwnerAuthor Commented:
Sorry it took so long to get back to this. I have just tested and it works perfectly. Now going to apply this to all of my scheduled tasks.

thanks.
0
 
Steve KnightIT ConsultancyCommented:
No problem for delay, thanks for coming back in the end.

Steve
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.