Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

ColdFusion Tip: How to tell if path is file or directory

DZone's Guide to

ColdFusion Tip: How to tell if path is file or directory

·
Free Resource


Today, one of the ColdFusion users asked me a question "Is there anyway I can find out whether a path is a directory or a file? There is no isDirectory() function available in ColdFusion and I don't want to write Java code to do this. Any help?"
For a moment I thought really! is there no way that ColdFusion can tell you whether a given path is of a directory or file? But sooner, I came across the function 'getFileInfo' which takes the path as an argument and returns a struct data that contains various metadata properties of the file. The struct includes a key - 'type' whose value can either be a directory or file. The below code shows how you can determine whether the given path is of a directory or file:

<cfset fileInfo = getFileInfo ( expandPath ( "./myDir" )) > <cfif fileInfo . type EQ "directory" > <!--- is a directory ---> <cfelseif fileInfo . type EQ "file" > <!--- is a file ---> </cfif> <cfdump var = " #fileInfo# " >

The other metadata properties such as canRead, canWrite, isHidden, lastModified, parent, size included in the resultant struct can also come handy.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}