Determine Free Disk Space
Beyond the traditional tasks of managing event log entries, configuration files, and processes, other tasks are more specific to typical day-to-day operations, such as copying files. Evaluating the total file size of a collection of files and making sure enough disk space exists on a target location before executing a copy or move operation can be crucial, particularly if the amount of data you need to copy or move is large. If you don't want to write a WSH script to handle this operation but still want a way to automate the verification, you can combine Cmd.exe statements with two tools available in Windows 2003: Forfiles.exe and Freedisk.exe. Listing 1 shows a sample script that does just that. The Forfiles command within the line at callout A is:
Forfiles.exe /S /M *.DAT /C "Cmd /c echo @fsize"
This line echoes all *.dat file sizes on the screen. The /S switch specifies that the search must be done in the current folder and all subfolders, and the /M switch specifies that all *.dat files be searched. The /C switch defines the command to execute for each file that Forfiles finds. In this case,
"Cmd /c echo @fsize"
simply outputs the size of each found file on the screen.
Placing the Forfiles command inside a For /f statement, as callout A shows, makes it possible to calculate the total size of all *.dat files. The Forfiles invocation is placed between parentheses in the For /f statement, which causes the statement to assign the output of the Forfiles command to the %%i environment variable. The :MakeTotal %%i statement invokes a subroutine called :MakeTotal with a parameter that the %%i variable specifies. The subroutine calculates the total file size with each file that's passed in as a parameter. The total file size is calculated by the Set /A Total=%Total% + %1 statement in the routine. The %1 variable is the first parameter passed during the :MakeTotal call, which is the size of the file examined by the Forfiles tool and stored in the %%i variable during the subroutine call. The For /f statement calls the subroutine for each file size that the Forfiles command returns.
After the For /f statement finishes, the Freedisk tool is executed. Freedisk determines whether the required disk space is available according to two parameters: the disk size required and the size of the target disk. In Listing 1, Freedisk uses the total file size that the Forfiles command calculated and the C:\ disk size determined by the /D switch. Freedisk displays a message stating whether the required disk space is available or not. In the command file, the environment variable %ERRORLEVEL% equals 0 if the necessary space is available or 1 if the space isn't available.
Updated Commands
Some of the new command-line tools in Windows 2003 and XP can be confused with existing commands. For example, Schtasks.exe replaces AT.exe, which previous versions of Windows include. (AT still exists in Windows 2003 and XP, but Microsoft recommends using Schtasks instead.) The aim of both Schtasks and AT is to schedule predefined tasks by leveraging the features that the Task Scheduler Windows Service provides. Schtasks works the way AT does but supports a different set of command-line parameters and syntaxes. Schtasks can do everything the AT command does but also much more. (To work with Schtasks, you must be a member of the Administrators group on the computer that the command affects.) Consider the following Schtasks command:
SCHTASKS /Create /RU Administrator /RP Password1
/SC MINUTE
/ST 09:20 /ET 09:30
/TN "Event Creation"
/TR "C:\WINDOWS system32\Eventcreate.exe
/S vm75459312b.
LissWare.Net
/L application
/SO ScheduledEventCreation
/T Success
/ID 999
/D ScheduledEventLogCreation"
The /TN switch creates a scheduled job named Event Creation. The /SC switch specifies that the job is created every minute between 09:20, which the /ST switch specifies, and 09:30, which the /ET switch specifies. The TR switch specifies that the command to run is an Eventcreate command, with switches and parameters specified between quotation marks. This command will run under the Administrator context, where credentials are passed by means of the /RU and /RP switches. You can't create this type of scheduling with the AT command, at least not easily. Use the /? switch to get more information about options and syntaxes that Schtasks supports.
Gpupdate.exe refreshes Group Policy Objects (GPOs) the way the Secedit.exe /Refreshpolicy command in Windows 2000 does. Gpupdate replaces Secedit in Windows 2003 and XP. However, Gpupdate makes your life easier if you want to refresh a GPO because it doesn't require all the command-line parameters that Secedit does. Use the /? switch to see all the options that Gpupdate supports.
You can use Findstr.exe in Windows 2003 and XP instead of Find.exe. Findstr's purpose is exactly the same as that of Find—both search for a text pattern in a specified path of files. However, Findstr has more searching and filtering capabilities than Find has. Findstr can search a specific list of files and folders and can use regular expressions to filter file content. A regular expression is a special text string that describes a search pattern. (For more information about regular expressions, go to http://www.regular-expressions.info.)
Take Command
I've only scratched the surface of the time- and labor-saving possibilities that the command-line tools in Windows 2003 and XP present. If you want more information about all tools and command-line syntaxes that Windows 2003 and XP support, execute the following command:
C:\>HH.EXE %SystemRoot%\Help\Ntcmds.chm
When you do, you'll discover the command-line jewels that are sleeping in your Windows 2003 and XP installations.