Highlighted

Delete All FIles In A Folder

Participant ,
Jul 27, 2006

Copy link to clipboard

Copied

Hello,

I am would like to use cffile to delete all files in a directory every night to clean up pdf files that were created during the day. Is there a way to delete all files with naming each one? What would go in the file path?

<cffile action = "delete" file = "full_path_name">

Gary

Views

2.8K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Delete All FIles In A Folder

Participant ,
Jul 27, 2006

Copy link to clipboard

Copied

Hello,

I am would like to use cffile to delete all files in a directory every night to clean up pdf files that were created during the day. Is there a way to delete all files with naming each one? What would go in the file path?

<cffile action = "delete" file = "full_path_name">

Gary

Views

2.8K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 27, 2006 0
Contributor ,
Jul 27, 2006

Copy link to clipboard

Copied

You can use cfdirectory to get a name of the files in the directory. Something like:

<cfset myDir = "c:\PDFs\" />
<cfdirectory action="list" name="qDir" directory="#myDir#" />
<cfloop query="qDir">
<cfif qDir.type IS "file" >
<cffile action="delete" file="#myDir##qDir.name#" />
</cfif>
</cfloop>

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 27, 2006 0
Participant ,
Jul 27, 2006

Copy link to clipboard

Copied

It may be easier to just delete the directory using <cfdirectory action="delete" directory="dir_name" />

Then create a new empty one with the same name right afterwards.

good luck,
Mike

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 27, 2006 0
Community Beginner ,
Oct 31, 2019

Copy link to clipboard

Copied

...13 years later...

I like Mike's solution, but sometimes the directory functions are weird in that after you delete, then createa  new one, it throws an error saying that hte directory already exists. Even after checking for existence.

FWIW, I'm using directoryExists(), directoryDelete(), and directoryCreate(), rather than <cfdirectory>.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 31, 2019 0
BKBK LATEST
Adobe Community Professional ,
Nov 02, 2019

Copy link to clipboard

Copied

Myka wrote:

... sometimes the directory functions are weird in that after you delete, then createa  new one, it throws an error saying that hte directory already exists. Even after checking for existence.

 

Myka, there may be a good reason why it is impossible to "delete, then create" a directory or file within the same request. Namely, the thread to delete the directory or file may still be active when ColdFusion runs the function to create.

The usual solution is to use a named lock to separate the delete and create operations:

<cflock name="abc123">

    <!--- delete operation --->

</cflock> 

<cflock name="abc123">

    <!--- create operation --->

</cflock> 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 02, 2019 1