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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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.
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Hedley PhillipsOwnerAuthor Commented:
Increased points as 125 was just mean.
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.
Steve KnightIT ConsultancyCommented:
No problem for delay, thanks for coming back in the end.

Steve
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.