CIS Logo SVC Logo

   Computing & Information Systems
   Department

 

Schoology Facebook        Search CIS Site      Tutorials

WSH Command Line Arguments



DriveSpace Example


Much like with Unix shell scripts, in Windows Script Host a script can use command line arguments. These can be useful when you wish to pass some information into a script. The drivespace.vbs example below is used at the command line by entering something like either of the following:
cscript drivespace.vbs c p w
wscript drivespace.vbs c w h
The first command uses cscript to run drivespace.vbs with c, p, and w as the command line parameters (arguments). The program then reports on the amount of free disk space for each of these three drives. The second command uses wscript to run drivespace.vbs and has c, w, and h as the command line parameters.


' Filename:  drivespace.vbs
'
' Author:  Br. David Carlson
'
' Date:  August 5, 2001
'
' Last Revised:  April 9, 2003
'
' This WSH script is to be run at the command prompt and there given drive letters
' as command line parameters.  The script then reports on the available disk space
' for each of these drives.
'
' Usage: [CScript | WScript] drivespace.vbs <driveletters>
' Example: cscript drivespace.vbs c w p

Dim DriveLetter
Dim drive
Dim FileSystemObj
Dim num
Dim k
Dim args
Dim msg

' Procedure to find free space on a drive and append this info to msg string:
Sub FindSpace(drive, msg)
   Dim space
   Dim units

   space = drive.FreeSpace
   if space >= 1048576 then
      space = Round(space / 1048576, 2)
      units = "megabytes"
   elseif space >= 1024 then
      space = Round(space / 1024, 2)
      units = "kilobytes"
   else
      units = "bytes"
   end if

   msg = msg & "Free space on drive " & DriveLetter & ": "  _
      & space & " (" & units & ")" & vbCRLF
End Sub

set args = WScript.Arguments
num = args.Count

if num = 0 then
   WScript.Echo "Usage: [CScript | WScript] drivespace.vbs <driveletters>"
   WScript.Quit 1
end if

set FileSystemObj = CreateObject("Scripting.FileSystemObject")
msg = "Drive space report" & vbCRLF

for k = 0 to num - 1
   driveletter = args.Item(k)
   set drive = FileSystemObj.GetDrive(DriveLetter)
   FindSpace drive, msg
next

WScript.Echo msg

WScript.Arguments is the collection containing the information on the arguments. Like all collections it has a Count property and an Item property, both of which are used above. Note how the Count is checked to see if it is zero. If so, we display an error message and quit the program. Item is an array of strings, and Item(k) gives a string containing the value supplied by the user as argument k.

The FileSystemObject's GetDrive method is used to produce a drive object corresponding to a given drive letter. A user-written procedure is then used to calculate the amount of free space on this drive and append to the msg string a string containing this free space information. Note that this is a good example of how to create a Sub procedure with parameters. Parameters are passed by reference by default, so that any changes made in the procedure are sent back out. This FindSpace procedure also has two local variables, space and units.

The code inside of this Sub procedure illustrates an if .. elseif .. else .. end if construct. Also, the Round function is used to round a number to 2 decimal places (where the 2 is specified in the second parameter). Recall, too, that the & is the string concatenation operator and the underscore indicates that a line of code is being continued.

FileReport Example


This example reports information on the file or files supplied as command line arguments (parameters). See the usage section in the comments as well as the example of use.

Suppose we try the script out by using the following in a DOS box (and suppose that the two files actually do exist):
cscript filereport.vbs c:\autoexec.bat m:\colors.doc
Then the output from the script looks something like the following. If you ran the script using wscript instead of cscript then these results would be shown in a graphical window instead of as text in the DOS box as shown here.
File Report:

autoexec.bat   8/6/2002 7:56:55 AM   MS-DOS Batch File
colors.doc   1/26/2003 3:38:44 PM   Microsoft Word Document
You might also run this script to look for an executable or dll file. Here is an example that looks for one of each:
wscript filereport.vbs c:\WINDOWS\system32\notepad.exe "C:\Program Files\Internet Explorer\ieproxy.dll"
Next, let's look at the script itself. In many ways it is similar to the previous script, but it uses some new features.


' Filename:  filereport.vbs
'
' Author:  Br. David Carlson
'
' Date:  March 14, 2004
'
' This WSH script reports on the file or files supplied as command-line arguments
' to this script.  For each such file, the output shows the name of the file, the
' date it was last changed, and the file type.
'
' Usage:  At the command prompt use either of the following:
' cscript filereport.vbs filename(s)
' wscript filereport.vbs filename(s)
'
' Example of use:   cscript filereport.vbs c:\autoexec.bat m:\paper.doc

Option Explicit

Dim cMsg, oShell, oFSO, oFile, k, num, args

Set oShell = WScript.CreateObject("WScript.Shell")
Set args = WScript.Arguments

num = args.Count

if num = 0 then
   WScript.Echo "Usage: [cscript | wscript] filereport.vbs <filename(s)>"
   WScript.Quit 1
end if

Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")

' Process the list of files:

cMsg = "File Report:" & vbCRLF & vbCRLF

for k = 0 to num - 1
   Set oFile = oFSO.GetFile(args.Item(k))
   cMsg = cMsg & oFile.name & "   " & oFile.DateLastModified & "   " & oFile.Type & vbCRLF
next

WScript.echo(cMsg)

Set oShell = Nothing
Set oFSO = Nothing
Set args = Nothing

By now you can probably understand this script's operation on your own. But just to be sure, here is a quick summary: As in the previous example, this script generates an error message and quits if the user failed to supply at least one parameter. Throughout the script a message string is built up in the variable cMsg. It is echoed at the end of the script. The loop goes through all of the arguments to the script by looking at args.Item(k) as k runs from 0 to num - 1. In each case, args.Item(k) is the pathname for a file. For example, it might be the "c:\autoexec.bat" mentioned in the comments. The GetFile method returns a File object containing information on this particular file. The script then use the name, DateLastModified, and Type properties on this File object to look up the obvious pieces of information about this file. Other useful properties of this object include path (for the complete pathname for the file), DateCreated, DateLastAccessed, and Size (for the size of the file in bytes). At the end of this script, Nothing is assigned into the objects to be sure that they are destroyed.

More Information


For further information see the References section.

Back to main Windows Script Host page



Author: Br. David Carlson
Last updated: February 11, 2007
Disclaimer