How to get detailed drive information through COM

On January 10, 2008, in ColdFusion, by Anuj Gakhar

Back in 2001, when CFComet was live, I wrote a little script to read detailed drive information (windows only) via COM. I thought it might be a good idea to post it here as well. This code makes use of the FileSystemObject and displays some useful information. I did a little search and its still Live. Here is the code :-

[xml]
<!— Try to Create the Object —><CFTRY>
<CFOBJECT TYPE="COM" CLASS="Scripting.FileSystemObject" NAME="FSO" ACTION="CONNECT">
<CFCATCH type="ANY">
<CFOBJECT TYPE="COM" CLASS="Scripting.FileSystemObject" NAME="FSO" ACTION="CREATE">
</CFCATCH>
</CFTRY>
<!— Get all the existing drives . This can be any type of drives.
Fixed drives, network drives etc. —>
<CFSET Drives = FSO.Drives>
<CFOUTPUT>
<!— Display the no. of drives —>
Number of Drives : #Drives.Count#<BR>
<!— Make a table to show the results —><TABLE cellspacing="1" cellpadding="5">
<TR>
<TD>Drive</TD>
<TD>Total Size</TD>
<TD>Free Space</TD>
<TD>Available Space</TD>
<TD>Serial No.</TD>
<TD>Path</TD>
<TD>Type</TD>
<TD>Ready?</TD>
<TD>Name</TD>
<TD>System</TD>
</TR>
<!— This is a COM collection so we cant use conditional loops, index loops and other loops.
ColdFusion provides us a nice way of looping over COM collections so we are using that here .—>
<CFLOOP COLLECTION="#Drives#" ITEM="this">
<!— remove the below "CFIF" statement if you havent disabled any of your floppy drives .—>
<CFIF this.DriveLetter is not "A">
<TR>
<!— Display the Drive Letter —>
<TD>#this.DriveLetter#</TD>
<!—Display the Total size —><TD><CFIF this.isReady AND ISDefined("this.TotalSize")>
#NumberFormat(round(evaluate(this.TotalSize/1000000)))# Mb
</CFIF>
</TD>
<!— Display the free space —><TD>
<CFIF this.isReady AND ISNumeric(this.FreeSpace)>
#NumberFormat(round(evaluate(this.FreeSpace/1000000)))# Mb
</CFIF>
</TD>
<!— Display the available space —><TD>
<CFIF this.isReady AND IsNumeric(this.AvailableSpace)>
#NumberFormat(round(evaluate(this.AvailableSpace/1000000)))# Mb
</CFIF>
</TD>
<!— Display the serial number —><TD>
<CFIF this.isReady AND LEN(this.SerialNumber)>
#this.SerialNumber#
</CFIF>
</TD>
<!— Display the path —><TD>#this.path#</TD>
<!— These are the constants returned by Drive.DriveTypeConst DriveTypeRemovable = 1
Const DriveTypeFixed = 2
Const DriveTypeNetwork = 3
Const DriveTypeCDROM = 4
Const DriveTypeRAMDisk = 5
So we’ll use CFSWITCH statements to display the actual type —>
<TD>
<CFSWITCH expression="#this.DriveType#">
<CFCASE value="1">Removable</CFCASE>
<CFCASE value="2">Fixed</CFCASE>
<CFCASE value="3">Network</CFCASE>
<CFCASE value="4">CD ROM</CFCASE>
<CFCASE value="5">Ram Disk</CFCASE>
<CFDEFAULTCASE>Unknown</CFDEFAULTCASE>
</CFSWITCH>
</TD>
<!— Display if the Drive is ready —><TD>#this.isReady#</TD>
<!— Here we have to display the drive name. Before that, we willcheck if it is a network drive or not. If it is a network drive,
then we display the ‘sharename’; otherwise, we display the ‘volumename’.
—>
<TD>
<CFIF this.isReady>
<CFIF this.DriveType IS "3">
<!— #this.ShareName# —>
<CFELSE>
#this.VolumeName#
</CFIF>
</CFIF>
</TD>
<!— Display the type of Filesystem —>
<TD><CFIF this.isReady>#this.FileSystem#</CFIF></TD>
</TR>
</CFIF>
</CFLOOP>
</TABLE>
</CFOUTPUT>[/xml]

Tagged with:  

4 Responses to How to get detailed drive information through COM

  1. thanks! this may be very useful for the project I’m currently working on.

  2. Anuj Gakhar says:

    @Aaron, glad you liked it. As I wrote, this was done years ago, so its good to know it still is useful.

  3. Ian says:

    This looks like an old post – but is there any way to make this run on a network drive?

  4. Anuj Gakhar says:

    @Ian, I think a mapped drive would work just the same as a local drive. Did you try doing that?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2011 Anuj Gakhar