So I hit up a friend of mine at work to write a quick write up and example of batch scripting; which he leverages heavily administratively, and here’s the result:
For me, batch is the quickest and easiest way to automate most tasks for server administration. These tasks can be completed by leveraging built-in commands, resource kit executables, and other Microsoft/SysInternals type tools. I would like to begin at the beginning, but if I had that much patience I would have learned a real scripting language, so I will start in the middle.
Step 1 is figuring out how to get information you need to manipulate out of the command line, here’s a good start:
Step 2 is formatting and passing that information to additional tools and/or parsing the results for review, (done by the batch itself).
Scenario: You have a directory on a server containing many user profiles. Under these profile directories is a ‘cache’ directory that you want to erase every night. User profiles are create/deleted on a regular basis, so creating a static batch of RMDIR commands is not an option.
Volume in drive C has no label.
Volume Serial Number is B043-A733
Directory of C:\Users
07/03/2012 07:20 AM <DIR> .
07/03/2012 07:20 AM <DIR> ..
07/03/2012 07:17 AM <DIR> Andy
07/03/2012 07:17 AM <DIR> Curtis
07/03/2012 07:17 AM <DIR> Daniel
07/03/2012 07:17 AM <DIR> Mark
You want to run the command: rmdir /s /q c:\users\<insert username>\cache, where the username is dynamically pulled from the current c:\users directory. First, tweak your command with any available options/flags that give you an output that is easier to work with. In this case, the /b (brief) flag for DIR will strip the output of any extra format.
C:\>dir c:\users /b
We will reference the output from this command as a single string variable per line, so removing header formatting and unnecessary columns is ideal. For this script I’ll be using a FOR loop; there are several different types of FOR loops in DOS, though I use /F (filenameset) almost exclusively. Typing FOR /? from a command prompt will give you the full list of options, but we will focus on the two that read from either a text file or a command output. From the FOR /? help:
FOR /F [“options”] %variable IN (file-set) DO command [command-parameters]
FOR /F [“options”] %variable IN (‘command’) DO command [command-parameters]
The “options” I most commonly use are “tokens” and “delims”, to specify which column(s) to read from, and what character(s) delimit columns (blank space is the default delimiter). We narrowed down the output to only one column, so “delims” will not be used in this example, and there is only one token available.
@echo off for /f “tokens=1” %%a in (‘dir c:\users /b’) do (rmdir /s /q c:\users\%%a\cache)
Lets take a closer look at each component of the for loop and what it is doing:
Perform an action for every line in a text output
Identify the first word on every line (tokens=2 would be the second word, etc)
Reference that first word of every line as %%a
in (‘dir c:\users /b’)
The text output we are reading from is the command ‘dir c:\users /b’
you can also read a text file instead of command output by removing the single quotes,
IE: for /f “tokens=1” %%a in (serverlist.txt)
action(s) to be performed for every line
Now this batch is ready to be added as a scheduled task and automatically delete the specified cache directories every night. You can create the scheduled task via command line as well, but I don’t see much of a need for that in this case. As much as I enjoy automation and CLI, I typically avoid both when they are not the quickest way to get the job done.