Entries Tagged as 'Microsoft FileSystemObject'

Using ColdFusion to Count the Files in a Directory - Part II

<CFDIRECTORY> , Microsoft FileSystemObject 2 Comments »

So back on March 5th I wrote a post about Using Cold Fusion to Count the Files in a Directory.  Recently a component I wrote that keeps an eye on mail server processes choked to death on the large number of files it had to report on.  Another component in the mail server failed (one vendor updated their component without telling another vendor about it, yada yada) and the mail queue backed up.  With scores of thousands of files in the directory, I found CFDIRECTORY to be kind of slow.

So.  I wrote a new function to that uses the FileSystemObject to look at a single directory, and I think it might work better.  I pointed it at a photo gallery that had a large number of files and it was very responsive.   Here’s hoping, right?  Regardless, it’s another way of counting files in a folder; I’m sure I’ll need this again someday, and that’s what this blog is all about, right?  Maybe you’ll find it handy, too.

Here’s the idea:

<cffunction name="fileCountFSO" returnType="numeric" output="false">


<cfargument name="folderLoc" required="no">
<cfparam name="folderLoc" default="">


<cflock 
timeout ="30" 
throwontimeout ="No" 
name ="#CreateUUID()#" 
type ="EXCLUSIVE"
>


<cftry>


<cfobject 
type="COM" 
name ="FSO" 
class ="Scripting.FileSystemObject" 
action ="CONNECT"
/>


<cfcatch>


<cfobject 
type="COM" 
name="FSO" 
class="Scripting.FileSystemObject" 
action="CREATE"
/>


</cfcatch> 


</cftry>
 
</cflock>


<cftry>


<cfscript>


objFolderFiles = FSO.getFolder(folderLoc).Files;
objFolderCount = objFolderFiles.Count;


</cfscript> 


<cfcatch>


<cfset objFolderCount = 0>


</cfcatch>


</cftry>


<cfreturn #objFolderCount#>


</cffunction>

Determine the Size of a Directory with ColdFusion

Microsoft FileSystemObject No Comments »

The Microsoft FileSystemObject is a handy and speedy tool to help determine the size of a given folder.  You could probably trim this up a bit by using more CFSCRIPT, but here’s one way to make use of it:

<cffunction name="folderSize" returnType="numeric" output="false">

<cfargument name="folderLoc" 		required="yes">
<cfargument name="increments" 	required="no">

<cfparam 		name="increments" 	default="Mb">

<cflock 
	timeout					="30" 
	throwontimeout	="No" 
	name						="#CreateUUID()#" 
	type						="EXCLUSIVE"
	>
	
<cftry>

  <cfobject 
  		type			="COM" 
      name 			="FSO" 
      class 		="Scripting.FileSystemObject" 
      action 		="CONNECT"
	>
	</cfobject>
	
<cfcatch>

 <cfobject 
 			type			="COM" 
      name			="FSO" 
      class			="Scripting.FileSystemObject" 
      action		="CREATE"
  >
 </cfobject>
 
</cfcatch> 
</cftry>
    
</cflock>

<cftry>

  <cfscript>
      objFolder      	= FSO.getFolder(folderLoc);
      objFolderSize 	= LSParseNumber(objFolder.size); 
  </cfscript> 
  
 <cfcatch>
      <cfset objFolderSize = 0>
 </cfcatch>
  
</cftry>

<cfif #increments# is "Mb">
	<cfset divideBy = 1024^2>
<cfelseif #increments# is "Kb">
	<cfset divideBy = 1024>
<cfelse>
	<cfset divideBy = 1>
</cfif>

<cfset tehResults = #LSParseNumber(DecimalFormat(Evaluate(objFolderSize)))# / #divideBy#>	

<cfreturn #tehResults#>

</cffunction>
Powered by Mango Blog. Initial Template Design and Icons by N.Design Studio, modified by Michael Cummins.
RSS Feeds