I'm a big fan of scripting. I'm not talking about PHP or Javascript, however I am a fan of those too and may cover them in future posts, but this one is about batch scripting in Windows. What's so great about batch scripting? Lots of things! It saves me trouble and hassle, it automates things that would otherwise be a manual process, and overall keeps me from doing work ;). (No really, if I had to do things manually key by key that leaves more room for error and error isn't fun)
An example I'll take from a colleague of mine, Peter who is our LANDesk Engineer (and maintains a great blog which you should checkout if you're interested in LANDesk and desktop management).
This script is simple and highly useful: Delete IIS logs that are older then 14 days. If you run a busy web sever using Microsoft Internet Information Services you may have noticed how large your daily log files can get (mine are over 90mb each day). This can quickly stack up over the course of a few months and sooner or later you'll run out of space on your server.
forfiles /p "C:\inetpub\logs\LogFiles\W3SVC1" /s /m *.* /c "cmd /c Del @path" /d -14
So what's going on here is that the forefiles
command will look in the default path for IIS logs, and then for all files in the \W3SVC1
and it's subdirectories (if any) it will execute the Del
command if they have a last modified date of 14 days (or 2 weeks). Now just create a scheduled task in Windows for it to run every night or once a week and it will keep your logs nice and tidy. Fore more info check out the details on the forefiles
command at TechNet here.
Of course you can just use the Windows GUI and schedule the task or you can script it! Just put the following into the Command Prompt:
SCHTASKS /create /tn "Clear IIS Logs" /tr "\"c:\Pathto\cleanupscript\script.bat\" arguments" /sc daily /sd 8/31/2013 /st 2:00:00
This will make your IIS cleanup script run every night at 2:00 am. Now for the fun stuff: There's a little application I found called Nircmd. Nircmd is awesome, it lets you script just about every Windows thing you can thing of. Take a look at their website, but some of the things are:
- Change, or Mute your computer's volume.
- Open and close your CD drive (Why someone would want to do this is beyond me, but you can!)
- Change your display resolution and bit depth
- Enable and Disable your screen saver
- and many more things!
I just want to highlight one of Nircmd's useful features: Turning on and off your monitors. Man this is one useful command. So the monitors I use on my Windows computer at work are 2 old Apple 20" LCD Cinema displays. The power button (or more like a touch pad) is inconveniently located on the right side of the monitors and the one on the right has it's button inaccessible since it's butted up next to the other. Each night before I leave I made it a habit to switch these screens off overnight. Well that got quite bothersome as I don't like my monitors being re-adjusted (since they had to be moved to reach the power button, and once my screen angles are adjusted, I like them where they are.). I used Nircmd to script my monitors turning on and off everyday at 8 am and at 4:30 pm. I also have a .bat file on my desktop that I can use to quickly turn them both off when I need to leave my desk for an extended period. The script is simple, just take these and drop them into .bat files and schedule them:
nircmd.exe monitor off
and nircmd.exe monitor on
Hey you can call me lazy, but it's great! Each day when I come in my monitors just turn on, and when I'm getting ready to leave they turn off by themselves :). While this may seem like a more superfluous use of this tool, I am working on a project at work that may require these same scheduled batch scripts to solve an energy savings obstacle. Scripting is great, you can really pretty much everything with it, on both Windows and Mac OS X. Go try and find some scripts that make your life easier!