Advanced Files and Folders in WSH - A Real-World Scenario
(Page 2 of 4 )
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 _
& " contains " & .FreeSpace & " bytes of free space"
End With
You should see an output like this:
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 _
& " contains " & ConvertBytes(.FreeSpace) & " of free space"
End With
Here’s the output of our script after using the ConvertBytes function.
There is 77 GB of 111.7 GB available on drive C:
That means that drive C: contains 77 GB of free space
That makes it much more readable in my opinion.

Next: Examining File Attributes >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer