How do I find out if a specific file or directory exists on my ColdFusion server?

The directoryExists() function takes an absolute path as its only parameter. It will then test for the existence of that absolute path on the server. The function returns YES or NO. Sample code for the directoryExists() function would look as follows:

<cfset testDirectory = "C:\foo\">
 
  <cfif directoryExists(testDirectory)>
    Yes, #testDirectory# exists on the server.
  <cfelse>
    No, #testDirectory# does not exist on the server.
  </cfif>
</cfoutput>

The fileExists() function works the same way as the directoryExists(). You pass it in an absolute path, and it returns YES or NO depending on if the path exists. It is important to note that if you are testing for the existence of a file, you many first want to make sure the directory exists. Sample use of the fileExists() function would be as follows:

<cfset testFile = "C:\foo\foobar.cfm">
<cfoutput>
  <cfif fileExists(testFile)>
    Yes, #testFile# exists on the server.
  <cfelse>
    No, #testFile# does not exist on the server.
  </cfif>
</cfoutput>

This question was written by Jeremy Petersen
It was last updated on January 26, 2006.

Categories

File and Directory Access

Comments

comments powered by Disqus