Java version of CFDIRECTORY: Updated

On November 8, 2007, in ColdFusion, by Anuj Gakhar

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","").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">
<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 ) />
<cfquery name="dirInfo" dbtype="query">
SELECT datelastmodified,name,size,type,directory,hidden,pathname,attributes
FROM arguments.dirInfo
<cfreturn dirInfo />


Tagged with:  

10 Responses to Java version of CFDIRECTORY: Updated

  1. RobW says:

    So did the improvements have any effect on the speed? Did this turn out to be faster or closer to cfdirectory’s time?

  2. Anuj Gakhar says:

    I did test it but I didnt notice much difference in this version and previous version, to be honest. May be thats just me. I would like if someone else could test it on different machines? Any takers?

  3. […] try to mimick the CFDirectory functionality using .NET objects. I have done that previously in Java as well but this time around its .NET […]

  4. Ed Martin says:


    I made some modifications I thought you might be interested in. I was getting an error on the Date function for some reason, so I changed that. Also, I put in a component so it could be called as an object. Finally, I added a query of queries so I could sort by Type. Here it is:

    <!— —>

    SELECT datelastmodified,name,size,type,directory,hidden,pathname,attributes
    FROM arguments.dirInfo

  5. Anuj Gakhar says:

    Thanks Ed. Got your email with the code. So are you using this rather than the normal CFDIRECTORY?

  6. Ed Martin says:

    Yes, I am.

  7. Anuj Gakhar says:

    Good. I updated the post with your code in it.

  8. Ed Martin says:

    Oh, I forgot – I also added an attribute column so the user could display whether the file was read-only or archived (this sends back a 1 or 0, depending on the file’s status – you’ll have to CFIF your code to display R or A or whatever).

  9. Anuj Gakhar says:

    @Ed, Like this one, there might be some more that you can include in the returned query. Its best to leave that option to the user to do what he wants with it, I guess.

  10. duncan says:

    Don’t forget to var scope theDate, theDateFormat and dirInfo (the local one used for the query at the end).

Leave a Reply

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

© 2011 Anuj Gakhar