Skip to main content
Inspiring
October 31, 2019
Question

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

  • October 31, 2019
  • 2 replies
  • 1467 views
<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 has been closed for replies.

2 replies

BKBK
Community Expert
Community Expert
November 1, 2019

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>
Inspiring
November 1, 2019

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.

BKBK
Community Expert
Community Expert
November 2, 2019

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

WolfShade
Legend
October 31, 2019

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,

 

^ _ ^

Inspiring
October 31, 2019

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.

WolfShade
Legend
October 31, 2019

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

 

V/r,

 

^ _ ^