My original post on this subject got some improvements and suggestions from different people. So I decided to put together all the improvements and write a new post. The improvements were suggested by Dan Wilson and Qasim Rasheed. Thanks to both of them. The function can be found here Updated DirectoryListing Function.
Here is what has changed :-

  1. Instead of using list(), the function now uses listFiles() method which gives direct access to the underlying methods like getName(), getParent() etc .
  2. I converted the long Date variable to a CF Date object using java.util.Date .
  3. The code is lesser now and all the unnecessary CreateObject() calls are now removed.

Update : 21/02/2008

I have received some modified code from Ed Martin which he talks about in his comments below. Thanks Ed. Here is the code :-

<cfcomponent name="directoryReader" displayname="directoryReaderComponent">

<cffunction name="Directorylisting" returntype="query" output="true">
<cfargument name="pathToparse" type="string" required="true" />
<cfargument name="recurse" type="boolean" default="false" required="false" />
<cfargument name="dirInfo" type="query" default="#queryNew('datelastmodified,name,size,type,directory,hidden,pathname,attributes')#">
<cfset var thisFile = "" />
<cfset var listFiles = "" />
<cfset var theType = '' />
<cfif Len(arguments.pathToparse)>
<cfset listFiles =  createObject("java","java.io.File").init(Trim(arguments.pathToParse)).listFiles() />
<cfset theDate = createObject("java","java.util.Date") />
<cfset theDateFormat = createObject("java","java.text.SimpleDateFormat") />
<cfloop from="1" to="#arraylen(listFiles)#" index="thisFile">
<cfset queryAddRow(arguments.dirInfo)>
<cfset querySetCell(arguments.dirInfo,"datelastmodified", theDateFormat.format(listFiles[thisFile].lastModified()))>
<!--- <cfset querySetCell(arguments.dirInfo,"datelastmodified", listFiles[thisFile].lastModified() )> --->
<cfset querySetCell(arguments.dirInfo,"name", listFiles[thisFile].getName() )>
<cfset querySetCell(arguments.dirInfo,"size", Val( listFiles[thisFile].length() ) )>
<cfset querySetCell(arguments.dirInfo,"directory", listFiles[thisFile].getParent() )>
<cfset querySetCell(arguments.dirInfo,"hidden", listFiles[thisFile].isHidden() )>
<cfset querySetCell(arguments.dirInfo,"pathname", listFiles[thisFile].getPath() )>
<cfset querySetCell(arguments.dirInfo,"attributes", listFiles[thisFile].canWrite() )>
<cfset theType = 'dir'>
<cfif listFiles[thisFile].isFile()>
<cfset theType = "file">
</cfif>
<cfset querySetCell(arguments.dirInfo,"type", theType )>
<cfif arguments.recurse AND listFiles[thisFile].isDirectory() AND NOT listFiles[thisFile].isHidden()>
<cfset arguments.dirInfo = Directorylisting( listFiles[thisFile].getPath(),true, arguments.dirInfo ) />
</cfif>
</cfloop>
<cfquery name="dirInfo" dbtype="query">
SELECT datelastmodified,name,size,type,directory,hidden,pathname,attributes
FROM arguments.dirInfo
ORDER BY Type ASC
</cfquery>
</cfif>
<cfreturn dirInfo />
</cffunction>

</cfcomponent>