Tuesday, April 15, 2014

Windows Scripting (the normal shell) for Ex VB Programmers - Lesson 3–Objects

After the basics of variables, Selection and Iteration statements, let us do something useful in real life. We start with the first object FileSystemObject. This gives us access to the filesystem. An essential part of scripting. We may need to create files, modify content, look for their existence. The idea is to make you familiar with using objects as well.

Let us take an example

Set fs = CreateObject("Scripting.FileSystemObject")
Set dc = fs.Drives
For Each d in dc
    wscript.echo d.driveletter
Next

Save this script as fsdrives.vbs

Run it

C:\scripts>cscript fsdrives.vbs //nologo
C
D
F

C:\scripts>

I have three drives on my laptop. Your machine may have more or less. Let us understand the code.

Line 1 is creating an object of FileSystemObject type. Without this you cannot use what is in this object. An object is contains methods (functions) and variables. The variables may be simple variables or may be collections. Collections are more like an array, you can use a for loop to go through each of the items, and then access the properties of each.

fs is an instance of the FileSystemObject

fs.drives is referencing a property of the FileSystemObject

fs.drives returns a collection of drives.

To loop through it we need a for loop. dc is the drive collections we have used. You have a collections of drives. For each drive there are many properties. We are referencing one of the property called driveletter.

Let us make the above program more richer. We not only need the drive, but also want to know the capacity and free space.

' Main Code Starts here

Set fs = CreateObject("Scripting.FileSystemObject")
Set dc = fs.Drives
s=chr(10) & "Number of Drives " & dc.count
wscript.echo s
s=string(65,"-")
wscript.echo s
For Each d in dc
    s=d.driveletter & chr(9) & d.path & chr(9) & d.isready
    if d.isready then
        s=s & chr(9) & d.volumename
        s=s & chr(9) & d.filesystem
        ts=int(int(int(d.totalsize/1024)/1024)/1024) & " GB"
        s=s & chr(9) & ts
          ts=int(int(int(d.freespace/1024)/1024)/1024) & " GB"
        s=s & chr(9) & ts
          ts=int(int(int(d.availablespace/1024)/1024)/1024) & " GB"
        s=s & chr(9) & ts       
    end if
    wscript.echo s
Next

' We have explored, volumename, filesystem, totalsize, freespace
' availablespace, driveletter, path and isready properties from
' the drives collection. These values are for each drive.
' chr(9) is for tab and chr(10) is for new line
' Anything typed after single quote is a comment.
' space is shown in bytes. change to KB, MB, GB by dividing 1024

Copy and paste the code. Run it.

C:\scripts>cscript fsdrives.vbs //nologo

Number of Drives 3
-----------------------------------------------------------------
C       C:      True                  NTFS    421 GB  195 GB  195 GB
D       D:      True    LENOVO  NTFS    28 GB    19 GB    19 GB
F       F:      False

C:\scripts>

That is the output I get. Your challenge would be to fine tune the code further so that the numbers are aligned below each other. On your machine you may see more or less drives and the numbers different.

No comments: