In part one of this series we began to explore how we could use VBScript’s FileSystemObject in WSH to work with files and folders. In this installment we will look at some of the more advanced methods and techniques that this object has to offer. Let’s get our hands dirty and learn about some more advanced scripting techniques.
We’ll start off with something useful. One of the most common scripting uses of copying files and folders is for archival or backup purposes. When moving large amounts of information it can be helpful to know how much drive space is available.
To begin, we’ll create a simple script to demonstrate the information that is available to us. Then we’ll create an actual, usable drive space script. The following script demonstrates all of the Drive object’s properties.
Set objFso = CreateObject("Scripting.FileSystemObject")
Set drive = objFso.GetDrive("C")
With drive
Wscript.Echo "Available Space:", .AvailableSpace
Wscript.Echo "Drive Letter:", .DriveLetter
Wscript.Echo "Drive Type:", .DriveType
Wscript.Echo "File System:", .FileSystem
Wscript.Echo "Free Space:", .FreeSpace
Wscript.Echo "Is Ready:", .IsReady
Wscript.Echo "Path:", .Path
Wscript.Echo "Root Folder:", .RootFolder
Wscript.Echo "Serial Number:", .SerialNumber
Wscript.Echo "Share Name:", .ShareName
Wscript.Echo "Total Size:", .TotalSize
Wscript.Echo "Volume Label:", .VolumeName
End With
As you can see, we’re just making a listing of the values that each property returns. A Cscript example should look something like this:
Available Space: 82646454272
Drive Letter: C
Drive Type: 2
File System: NTFS
Free Space: 82646454272
Is Ready: -1
Path: C:
Root Folder: C:
Serial Number: 1208326009
Share Name:
Total Size: 119949254656
Volume Label:
The values here are pretty self-explanatory. Keep in mind that all sizes are listed in bytes. Let’s take a look at the two values that you may be scratching your head over at the moment—DriveType and IsReady.
In Visual Basic, when a numeric value is converted to a Boolean, 0 becomes False and all other values become True. When a Boolean is converted to a numeric data type, False becomes 0 and True becomes -1.
The IsReady property simply returns a Boolean value indicating whether or not the drive is ready to be used. True means the drive is ready while False indicates that it’s not. Therefore, IsReady returns False if the drive is currently in use.
DriveType Constants
0 = Unknown
1 = Removable
2 = Fixed
3 = Network
4 = CD-ROM
5 = RAM Disk
The DriveType property returns one of the DriveType constants listed in the table to the left based upon the type of drive being analyzed.
You should also keep in mind that the Share Name property will only return a value if the DriveType Property returns 3 for a remote drive.
Let’s take a real-world example. The following script will simply return some information about the free space on the drive. You could easily use this code snippet in a script of your own to monitor drive space.
Set objFso = CreateObject("Scripting.FileSystemObject")
Set drive = objFso.GetDrive("C")
With drive
Wscript.Echo "There is " & .AvailableSpace & " of " & .TotalSize _
& " bytes available on drive " & .Path
Wscript.Echo
Wscript.Echo "That means that drive " & .RootFolder _
There is 82645839872 of 119949254656 bytes available on drive C:
That means that drive C: contains 82645839872 bytes of free space
Of course, if you’re like me, those numbers are still pretty obscure, right? Don’t worry, I’ve included a conversion function for you to use. Feel free to change it any way you like. Basically, it takes the number of bytes as a single string input and converts it to the nearest usable unit size.
Set objFso = CreateObject("Scripting.FileSystemObject")
Set drive = objFso.GetDrive("C")
With drive
Wscript.Echo "There is " & ConvertBytes(.AvailableSpace) _
& " of " & ConvertBytes(.TotalSize) & " available on drive " _
& .Path
Wscript.Echo
Wscript.Echo "That means that drive " & .RootFolder _
The FileSystemObject also allows us to view and edit a file or folder’s properties. This can be very helpful for error-checking before trying to modify a file or folder since a read-only attribute will result in an error.
Value
Attribute
0
None
1
Read-only
2
Hidden
4
System
32
Archive
64
Shortcut
2048
Compressed
The File and Folder objects both have an Attributes property that contains an integer representing the file or folder’s attribute settings. This integer is obtained by adding the constant value in the table to the left for each attribute that is applied to the file.
For example, a Read-only System file would have an attribute value of 5 by adding 1 for Read-only and 4 for System.
Let’s put this to real-world use. In your root folder is a file called boot.ini. This is a Windows configuration file that contains the contents of the Advanced Boot Menu. It typically has Read-only, Hidden, System, and Archive attributes giving it an attribute value of 39. Take a look at the following code.
Const NO_ATTRIBUTES = 0
Const READ_ONLY = 1
Const HIDDEN = 2
Const SYSTEM = 4
Const ARCHIVE = 32
Const LNK_SHORTCUT = 64
Const COMPRESSED = 2048
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.GetFile("C:boot.ini")
Wscript.Echo "The file " & objFile.Path & " with attribute code " _
& objFile.Attributes & " has the following attributes:"
If objFile.Attributes AND NO_ATTRIBUTES Then
Wscript.Echo "No attributes"
End If
If objFile.Attributes AND READ_ONLY Then
Wscript.Echo "Read-only"
End If
If objFile.Attributes AND HIDDEN Then
Wscript.Echo "Hidden"
End If
If objFile.Attributes AND SYSTEM Then
Wscript.Echo "System"
End If
If objFile.Attributes AND ARCHIVE Then
Wscript.Echo "Archive"
End If
If objFile.Attributes AND LNK_SHORTCUT Then
Wscript.Echo "Shortcut"
End If
If objFile.Attributes AND COMPRESSED Then
Wscript.Echo "Compressed"
End If
In the beginning of our code we set some constants for readability purposes. Their values correspond to the attribute values from the table above. We begin by connecting to the FileSystemObject and getting a handle on our file.
Then, we Echo the current file information. Finally, a series of If statements checks to see if each of the attributes exist. Notice that we check our values from smallest to largest. The code should have an output similar to the following:
The file C:boot.ini with attribute code 39 has the following attributes:
We’ve seen how to check for the existence of a file attribute. Now let’s learn how to set them. We can simply assign the Attributes property with whatever attribute value we choose in order to change it.
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.GetFile("C:boot.ini")
objFile.Attributes = 35
This code sample essentially removes the System attribute from the boot.ini file. We’ve manually set it to 35 which is: Read-only, Hidden, and Archive.
But what if we don’t want to set it manually? What if we just want to check for the existence of an attribute and set it if it isn’t there? The next code snippet will check our file to see if the System attribute is set. If it isn’t, it will add it without changing any of the other existing attributes.
Const SYSTEM = 4
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.GetFile("C:boot.ini")
If NOT objFile.Attributes AND SYSTEM Then
objFile.Attributes = objFile.Attributes + SYSTEM
End If
We’ve taken a piece of our code that checks if a value is set and modified the conditional statement slightly. Instead of checking to see if it is set, we’re checking to see if it is NOT set. If it isn’t, then we add the value to the existing attribute value.
That wraps up this segment. Stick around for my next article. In the final part of this series we’ll learn how to use the FileSystemObject to write good error-checking to avoid unwanted errors when working with files and folders. We’ll also take a brief look at how to deal with files and folders on a remote machine. See you then. Until next time, keep coding!