The cfdirectory tag can be pretty slow when reading in large numbers of files. I was trying to read a directory that contained 13,000 files and it was often timing out. In my case, I only needed the names of the files, not the size or date modified or anything else that cfdirectory returns. In that case, specifying the LISTINFO=”name” attribute should speed things up considerably. That tells CF not to stat() each file to get the additional information. Unfortunately this only became available in CFMX 7, and this particular project is running on 6.1

But using java you can achieve the same result:

<cffunction name="Directorylisting" returntype="query" output="true">
	<cfargument name="pathToparse" type="string" required="true" />

	<cfset var dirInfo1 = queryNew('name')>
	<cfset var thisFile = "" />
	<cfset var listFiles = "" />

		<cfif Len(arguments.pathToparse)>
			<cfset listFiles =  createObject("java","").init(Trim(arguments.pathToParse)).list() />
			<cfloop from="1" to="#arraylen(listFiles)#" index="thisFile">

				<cfset queryAddRow(dirInfo1)>
				<cfset querySetCell(dirInfo1,"name", listFiles[thisFile] )>

	<cfreturn dirInfo1 />

This is a stripped down version of Anuj Gakhar’s version of this function, his does recursion and returns even more data than cfdirectory does.

This brought the scan of 13,000 files down to less than 1 second! A larger test on 170,000 files came back in 20 seconds.

If you didn’t need the results returned as a query you could make this even faster by just returning the array that comes back from list(). But I needed to use QoQ on these results later.

Mark Kruger also has a couple interesting posts on performance issues with cfdirectory. (post 1 and post 2)

Leave a Reply

You must be logged in to post a comment.