How do you determine the amount of free space on a volume?

Currently there is no native ColdFusion functionality to accomplish this task. If ColdFusion can't do it natively, the next best option for this kind of task is usually calling a Java class directly from ColdFusion. Unfortunately, there does not seem to be any build-in way for current versions of Java to perform this task. The good news on the Java front is that the Java 6 (Mustang) File class will have getUsableSpace() and getTotalSpace() methods.

If you are in a Windows environment, you can use COM to accomplish this task. Rob Brooks-Bilson created a custom functional on CFLib called FreeSpace that uses Windows COM.

<cfscript>
/**
 * Returns the amount of free space (in bytes) available to the ColdFusion server for a specified drive or network share. (Windows only)
 * 
 * @param drvPath 	 Drive letter (c, c:, c:\) or network share (\\computer\share). 
 * @return Returns a simple value. 
 * @author Rob Brooks-Bilson (rbils@amkor.com) 
 * @version 1, July 19, 2001 
 */
function FreeSpace(drvPath)
{
  Var fso  = CreateObject("COM", "Scripting.FileSystemObject");
  Var drive = fso.GetDrive(drvPath);
  Return drive.FreeSpace;
}
</cfscript>

This question was written by Jeremy Petersen
It was last updated on March 1, 2006.

Categories

File and Directory Access

Comments

comments powered by Disqus