Skip to main content
Inspiring
Oct 31, 2019 3:33 PM UTC
Question

directoryCreate() throws error even after ensuring it doesn't exist

<cfif not directoryExists(arguments.dirPath)>
	<cfset directoryCreate(arguments.dirPath)>
</cfif>

Even after checking that the directory doesn't exist, directoryCreate() fails with:

Message:	The specified directory {directoryPath} could not be created. 
Detail:	The most likely cause of this error is that {directoryPath} already exists on your file system.

Ultimately, I want to delete it to quickly clear all files, then recreate it.

I can instead loop through the directory to delete the files, but removing the directory altogether does the job when it's not being weird about whether or not it exists.

This topic is closed to new replies. Start a new post to keep the conversation going.

6 replies

BKBK
Community Expert
Community Expert
Nov 1, 2019 5:48 AM UTC

Myka, ColdFusion may be right. It just might be that, once in a while, the wrong value of directory path is passed as an argument to the function.

You should test with:

<cftry>
<cfif not directoryExists(arguments.dirPath)>      
    <cfset directoryCreate(arguments.dirPath)>
</cfif>
<cfcatch type="any">
    <cfdump var="#arguments#">
</cfcatch>
</cftry>
Translate
Inspiring
Nov 1, 2019 1:11 PM UTC

Unfortunately, I was working with the same directory the entire time. And I passed the same variable to the directoryExists() and directoryCreate(). That in itself should be consistent, but it was intermittent.

Translate
BKBK
Community Expert
Community Expert
Nov 2, 2019 10:36 AM UTC

Myka wrote:

Ultimately, I want to delete it to quickly clear all files, then recreate it.

 

As I said elsewhere in this forum. it is advisable in this situation to use named locks. 

Translate
WolfShade
Legend
Oct 31, 2019 4:32 PM UTC

What is the value of arguments.dirPath?  Is it something like "F:\path\to\directory"?  Or is it "/path/to/directory"?  The first should work, the second should not.

 

Alternately, you can use an IP address: "//12.12.12.12/path/to/directory".

 

V/r,

 

^ _ ^

Translate
Inspiring
Oct 31, 2019 4:39 PM UTC

Using the first - N:\folder1\folder2\etc\.

It seems to work intermittently.

I can nix the <cfif> and throw a try/catch around it with an empty catch block. That ultimately does what I want, but I'm thinking I shouldn't have to resort to that.

Translate
WolfShade
Legend
Oct 31, 2019 6:58 PM UTC

Yeah, I know it just _feels_ hackish.. but probably the best way to work around it.

 

V/r,

 

^ _ ^

Translate