Interesting addition to ColdFusion 11 (and an example of it in ColdFusion 10)

While preparing for a presentation I'm doing on ColdFusion 11 on Wednesday night, I discovered an interesting update to the directoryList function. In ColdFusion 11 you can now pass a function for filtering as opposed to just a list of extensions. (And yes, a list is valid - I'll explain more in a bit.) The example function was a bit messy so I updated it to make it a bit nicer. Here is the example.

boolean function filterBySize(path, type, extension) {
	var sizeLimit = 1024 * 100; //more than 10 KB
	var extensionList = "jpg,jpeg,gif,png";

	if(type is "dir") return false;
            
    if(listFindNoCase(extensionList,extension)) {
		var fileInfo = getFileInfo(path);
		var size = fileInfo.size;
		if(size >= sizeLimit) return true;
	}

    return false;

}

filteredResults = directorylist(expandPath("."), true, "path", filterBySize);
writeDump(filteredResults);

Pretty nifty, right? Of course, it isn't too difficult to do in ColdFusion 10 either. Here's a version backported:

filteredFiles = arrayFilter(directoryList("/Users/ray/Dropbox/pictures", true, "path","*.jpg|*.jpeg|*.gif|*.png"), function(path) {
	var sizeLimit = 1024 * 1000;

	var fileInfo = getFileInfo(path);
	var size = fileInfo.size;
	if(size >= sizeLimit) return true;
    return false;
});

This is pretty much the same except I put the directoryList inside a call to arrayFilter. Oh, and by the way, even though the docs have said for a while now that the extension filter argument only supported one extension, that isn't true. For a while now you've been able to pass multiple extensions by using the pipe as a delimiter. I fixed that in the docs (while I was fixing the example code too).

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for Extend by Auth0. He focuses on serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments