?
Solved

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

Posted on 2008-11-07
6
Medium Priority
?
1,408 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:Hedley Phillips
  • 3
  • 3
6 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22903775
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
 
LVL 14

Author Comment

by:Hedley Phillips
ID: 22959109
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
 
LVL 43

Accepted Solution

by:
Steve Knight earned 1000 total points
ID: 22959379
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
LVL 14

Author Comment

by:Hedley Phillips
ID: 23049242
Increased points as 125 was just mean.
0
 
LVL 14

Author Closing Comment

by:Hedley Phillips
ID: 31514300
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 23049502
No problem for delay, thanks for coming back in the end.

Steve
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question