Advanced Files and Folders in WSH
(Page 1 of 4 )
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.
Next: A Real-World Scenario >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer