Hello fellow IT guru’s!

I need to write a batch file that will take a log that has yesterday’s date on it and mail it out. The name of the log is specifc (@MMM@DD@YYS6.TXT). I need to somehow write my batch file so that it will use the values for yesterday’s @MM@DD@YY as the variable for the file name.

I already have the command line mailer setup. All I need to be able to do is call for the file that has yesterday’s date on it and mail it out. I just can’t seem to figure out how to get the system to use yesterday’s date. I can get it to use today’s date just fine.

This is on a 2008R2 Server. I am open to other ideas. I just need to be able to grab this log and mail it out daily.

Any ideas?

2 Spice ups

today - 1

are you looking for daily email traffic on your network? for that try this

http://exchangeserverpro.com/daily-email-traffic-message-tracking-log-parser/

If powershell is an option instead of cmd, that is fairly easy to do. I’m not aware of a way in batch without writing a complicated script that factors in variable day months and leap years.

Edit: Googling around, this might do it, but I don’t have time to look it over right now: Batch files - DATE and TIME in NT batch

2 Spice ups

Not looking for daily traffic. Looking to email the previous day log out for archiving. It is not email dependant. It is a custon application that generates the log. The log contains played events from a Radio Automation system. I need to be able to send the logs to the right people. Seems like it is just easier to use a command line emailer (blat) in conjunction with a batch file and task schduler to automate the process. Its real sinmple. the issue I am having is that the log is not closed until 11:59pm, then the system names the log as the previous day date.

I.e. 010714S6.txt contains all items played on 01/07. The log is not saved until 12am 01/08. Since it now 01/08 and not 01/07, I cannot seem to get the system to step back a day and and grab the log that is named the previous day date.

Perhaps it would be easier to copy the day old log file (robocopy can do this) to a temp dir and then process all files in the temp dir. When the script is complete, delete all the files in the temp dir.

Same issue. I still need to be able to tell robocopy in a script to grab the day old log. How do I script it to grab the day old log and move it? I would be moving the problem from my custom script over the robocopy. Robocopy would need to be told to grab the day old file and nothing else. If I did a sync copy to the temp dir, then I would be moving the file from one folder to another. I would still need to deal with the day old filename at some point.

I might be looking at this with blinders on. I like the robocopy idea, but I just don’t see how that would solve the issue. Maybe you could expand on your idea a bit?

robocopy /maxage=1 /minage=1

Haven’t tried it but it should work.

I can test on some log files and see what works…

robocopy /maxage:1 /minage:0 <filename.ext or *.ext>

This should copy the file from the source to the destination that is not more than 1 day old (read as less than 24 hours old).

In the batch file that processes the blat command

  • forfiles /p /m /c “cmd /c blat command @file

or

  • for /r %%i in (*) do blat command %%i

the for line needs to be run in the path of the temp folder used in the robocopy - or changed to full path of files*

Thanks for the idea with robocopy. I’ll try it out and let you know!

Not sure about batch but if you can use PowerShell this should work for you:

Get-Date pulls current date we then use AddDays to make it yesterday and the ToString to format to file name.

###SETUP START###
#-------DO NOT MODIFY-------#
    #Yesterdays Date
        $date = ((Get-Date).AddDays(-1)).ToString("MMddyy")
###SETUP END###

###USER VARIABLES START###
#-------MODIFY AS NEEDED-------#
    #Sending account
        $from = "Logs@YOURDOMAIN.com"
    #Receiver
        $to = "YOUREMAIL@YOURDOMAIN.com"
    #Mail Subject
        $Subject = "SUBJECT"
    #Body of Email
        $MessageBody = "Attached is the log file for $date."
    # SMTP Server to be used
        $smtp = "YOURSMTPSERVER"
    #Enter log details (Path and name format)
        $file = "C:\logs\"+$date+"S6.txt"
###USER VARIABLES END###

###PROGRAM START###
	# Invokes the Send-MailMessage function to send notification email
		Send-MailMessage -SMTP $smtp -To $to -From $from -Subject $Subject -BodyAsHtml $MessageBody -Attachments $file
###PROGRAM END###
1 Spice up

WOW! I was looking for some help with my batch file and ChrisLeeRR goes and wrties it for me! You guys absolutely rock!

I was not expecting anyone to do my work for me. This is a welcome surprise. Thank you very much for the script. I will modify this and try it out.

I just can’t say thanks enough! I am blown away by the support of this forum.

No problem,

it was fun to put some of what I have learned in MVA: PowerShell JumpStart to use. I have also added this to my list of scripts and will be adding to my blog for future reference.

The SW community is a great resource and glad I found it.

Don’t forget to mark Best Answer and Helpful Posts

http://community.spiceworks.com/how_to/show/36402-how-to-mark-ba-and-hp

1 Spice up

Those two Jump Starts with Jeff Snover are amazing! I’m waiting for the v4.0 one; hopefully soon.

I have been slowly making my way through and posting my thoughts and summary on my blog (http://cleeit.blogspot.com/) I am on module 6 of v3. I have a long list of ones I want to do just need to find the time to watch/note/post.