Hello fellow IT guru’s!<\/p>\n
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<\/span>@[email protected]). I need to somehow write my batch file so that it will use the values for yesterday’s @MM<\/span>@DD@YY as the variable for the file name.<\/p>\n
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.<\/p>\n 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.<\/p>\n Any ideas?<\/p>","upvoteCount":2,"answerCount":16,"datePublished":"2014-01-07T20:19:56.000Z","author":{"@type":"Person","name":"chriscottingham9805","url":"https://community.spiceworks.com/u/chriscottingham9805"},"acceptedAnswer":{"@type":"Answer","text":" Not sure about batch but if you can use PowerShell this should work for you:<\/p>\n Get-Date pulls current date we then use AddDays to make it yesterday and the ToString to format to file name.<\/p>\n Hello fellow IT guru’s!<\/p>\n 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<\/span>@[email protected]). I need to somehow write my batch file so that it will use the values for yesterday’s @MM<\/span>@DD@YY as the variable for the file name.<\/p>\n 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.<\/p>\n 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.<\/p>\n Any ideas?<\/p>","upvoteCount":2,"datePublished":"2014-01-07T20:19:56.000Z","url":"https://community.spiceworks.com/t/batch-file-question-need-to-email-yesterdays-logs/267022/1","author":{"@type":"Person","name":"chriscottingham9805","url":"https://community.spiceworks.com/u/chriscottingham9805"}},{"@type":"Answer","text":" today - 1<\/p>","upvoteCount":0,"datePublished":"2014-01-07T20:25:01.000Z","url":"https://community.spiceworks.com/t/batch-file-question-need-to-email-yesterdays-logs/267022/2","author":{"@type":"Person","name":"jeff1792","url":"https://community.spiceworks.com/u/jeff1792"}},{"@type":"Answer","text":" are you looking for daily email traffic on your network? for that try this<\/p>\n http://exchangeserverpro.com/daily-email-traffic-message-tracking-log-parser/<\/a><\/p>","upvoteCount":0,"datePublished":"2014-01-07T20:25:32.000Z","url":"https://community.spiceworks.com/t/batch-file-question-need-to-email-yesterdays-logs/267022/3","author":{"@type":"Person","name":"psaini","url":"https://community.spiceworks.com/u/psaini"}},{"@type":"Answer","text":" 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.<\/p>\n 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<\/a><\/p>","upvoteCount":2,"datePublished":"2014-01-07T20:31:19.000Z","url":"https://community.spiceworks.com/t/batch-file-question-need-to-email-yesterdays-logs/267022/4","author":{"@type":"Person","name":"craigduff","url":"https://community.spiceworks.com/u/craigduff"}},{"@type":"Answer","text":" 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.<\/p>\n 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.<\/p>","upvoteCount":0,"datePublished":"2014-01-07T20:32:13.000Z","url":"https://community.spiceworks.com/t/batch-file-question-need-to-email-yesterdays-logs/267022/5","author":{"@type":"Person","name":"chriscottingham9805","url":"https://community.spiceworks.com/u/chriscottingham9805"}},{"@type":"Answer","text":" 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.<\/p>","upvoteCount":0,"datePublished":"2014-01-07T21:21:23.000Z","url":"https://community.spiceworks.com/t/batch-file-question-need-to-email-yesterdays-logs/267022/6","author":{"@type":"Person","name":"dabeast","url":"https://community.spiceworks.com/u/dabeast"}},{"@type":"Answer","text":" 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.<\/p>\n 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?<\/p>","upvoteCount":0,"datePublished":"2014-01-07T21:29:33.000Z","url":"https://community.spiceworks.com/t/batch-file-question-need-to-email-yesterdays-logs/267022/7","author":{"@type":"Person","name":"chriscottingham9805","url":"https://community.spiceworks.com/u/chriscottingham9805"}},{"@type":"Answer","text":" robocopy /maxage=1 /minage=1<\/p>\n Haven’t tried it but it should work.<\/p>","upvoteCount":0,"datePublished":"2014-01-07T21:49:34.000Z","url":"https://community.spiceworks.com/t/batch-file-question-need-to-email-yesterdays-logs/267022/8","author":{"@type":"Person","name":"dabeast","url":"https://community.spiceworks.com/u/dabeast"}},{"@type":"Answer","text":" I can test on some log files and see what works…<\/p>","upvoteCount":0,"datePublished":"2014-01-07T21:50:00.000Z","url":"https://community.spiceworks.com/t/batch-file-question-need-to-email-yesterdays-logs/267022/9","author":{"@type":"Person","name":"dabeast","url":"https://community.spiceworks.com/u/dabeast"}},{"@type":"Answer","text":" robocopy 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).<\/p>\n In the batch file that processes the blat command<\/p>\n or<\/p>\n 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*<\/p>","upvoteCount":0,"datePublished":"2014-01-07T22:10:49.000Z","url":"https://community.spiceworks.com/t/batch-file-question-need-to-email-yesterdays-logs/267022/10","author":{"@type":"Person","name":"dabeast","url":"https://community.spiceworks.com/u/dabeast"}},{"@type":"Answer","text":" Thanks for the idea with robocopy. I’ll try it out and let you know!<\/p>","upvoteCount":0,"datePublished":"2014-01-07T22:34:01.000Z","url":"https://community.spiceworks.com/t/batch-file-question-need-to-email-yesterdays-logs/267022/11","author":{"@type":"Person","name":"chriscottingham9805","url":"https://community.spiceworks.com/u/chriscottingham9805"}},{"@type":"Answer","text":" WOW! I was looking for some help with my batch file and ChrisLeeRR goes and wrties it for me! You guys absolutely rock!<\/p>\n 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.<\/p>\n I just can’t say thanks enough! I am blown away by the support of this forum.<\/p>","upvoteCount":0,"datePublished":"2014-01-08T15:21:16.000Z","url":"https://community.spiceworks.com/t/batch-file-question-need-to-email-yesterdays-logs/267022/13","author":{"@type":"Person","name":"chriscottingham9805","url":"https://community.spiceworks.com/u/chriscottingham9805"}},{"@type":"Answer","text":" No problem,<\/p>\n 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.<\/p>\n The SW community is a great resource and glad I found it.<\/p>\n Don’t forget to mark Best Answer and Helpful Posts<\/p>\n http://community.spiceworks.com/how_to/show/36402-how-to-mark-ba-and-hp<\/a><\/p>","upvoteCount":1,"datePublished":"2014-01-08T17:05:57.000Z","url":"https://community.spiceworks.com/t/batch-file-question-need-to-email-yesterdays-logs/267022/14","author":{"@type":"Person","name":"chrislee","url":"https://community.spiceworks.com/u/chrislee"}},{"@type":"Answer","text":" Those two Jump Starts with Jeff Snover are amazing! I’m waiting for the v4.0 one; hopefully soon.<\/p>","upvoteCount":0,"datePublished":"2014-01-08T17:17:18.000Z","url":"https://community.spiceworks.com/t/batch-file-question-need-to-email-yesterdays-logs/267022/15","author":{"@type":"Person","name":"semicolon","url":"https://community.spiceworks.com/u/semicolon"}},{"@type":"Answer","text":" I have been slowly making my way through and posting my thoughts and summary on my blog (http://cleeit.blogspot.com/<\/a>) 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.<\/p>","upvoteCount":0,"datePublished":"2014-01-08T17:19:09.000Z","url":"https://community.spiceworks.com/t/batch-file-question-need-to-email-yesterdays-logs/267022/16","author":{"@type":"Person","name":"chrislee","url":"https://community.spiceworks.com/u/chrislee"}}]}}
###SETUP START###\n#-------DO NOT MODIFY-------#\n #Yesterdays Date\n $date = ((Get-Date).AddDays(-1)).ToString(\"MMddyy\")\n###SETUP END###\n\n###USER VARIABLES START###\n#-------MODIFY AS NEEDED-------#\n #Sending account\n $from = \"[email protected]\"\n #Receiver\n $to = \"[email protected]\"\n #Mail Subject\n $Subject = \"SUBJECT\"\n #Body of Email\n $MessageBody = \"Attached is the log file for $date.\"\n # SMTP Server to be used\n $smtp = \"YOURSMTPSERVER\"\n #Enter log details (Path and name format)\n $file = \"C:\\logs\\\"+$date+\"S6.txt\"\n###USER VARIABLES END###\n\n###PROGRAM START###\n\t# Invokes the Send-MailMessage function to send notification email\n\t\tSend-MailMessage -SMTP $smtp -To $to -From $from -Subject $Subject -BodyAsHtml $MessageBody -Attachments $file\n###PROGRAM END###\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2014-01-08T11:33:24.000Z","url":"https://community.spiceworks.com/t/batch-file-question-need-to-email-yesterdays-logs/267022/12","author":{"@type":"Person","name":"chrislee","url":"https://community.spiceworks.com/u/chrislee"}},"suggestedAnswer":[{"@type":"Answer","text":"
\n
\n