21 Batch files aren’t dead yet 42Posted by 9 Sean Ryan 8 4e
3 Aug 31 2 24 4d
OK, I can hear the Powershell, WSH, Ruby, Perl and other such fans geeking out already, but the truth is the command line shell DOS isn’t a dead horse yet. It still provides some very friendly ways of performing automated tasks. I have put together a group of bat files that perform weblog backups. You could use these files to perform other kinds of backups if needed. Also for those out there who still like to play in DOS, there are a few neat little tidbits within the group of files. I don’t usually place code in my entries, but this worked well, and I figured out much of it by perusing the web and getting some bits from here and there to perfect the toolset. So, thanks everyone.
Now, to use these bat files you will need two utilities. The first is a little add-on for the command line called doff which makes formatting dates very easy. You can find this at http://www.jfitz.com/dos/. Just copy this file into Windows\System32. You don’t need to run a regsvr on this either. Next, because it is rather commonplace, I chose to use winzip for the zipping portion of the action set. You could easily change this to pkzip or some other compression utility that has a command line option.
OK, if you have all that set up - you can begin. The next piece I will share is that you’ll need to check your logfile nomenclature. In this example I am pulling hourly logs based on the previous day. So, I download logs that look like ex07082300.log, ex07082301 and so on. The common element is the 23 in this case. So, today is the 24th and I want to download yesterdays logs into a date created directory with the name of the day as the name of the zip archive. Make sense?. There are four files in this example, because I like to separate functionality in my code rather than putting it all in one.
Once you copy these files you can run it on a schedule. To do this you can run schtasks in XP and higher (start > run > type cmd > type schtasks /?) or AT in previous versions (type AT /?). The file you want to schedule is localBackup.bat.
Click here or right click-save link as… to get the zipped folder with all the contents. There are six files in the toolset, but the two script files are recreated on the fly from a called bat file each time it is run. They are there to share the example. To conclude, please test this and let me know if there are any date based bugs. I haven’t run this on the first of the month or last day of a month yet. As with all code from the web, please use at your own risk and feel free to modify and share as you like.
Update: The files worked on the first day of the month. It downloaded the last day of the previous month as needed. This has yet to break on me, so there it is…5d