Solved

Coldfusion cffile delete question

Posted on 2013-05-29
8
520 Views
Last Modified: 2013-05-30
I'm trying to setup some housekeeping on the images folder of my real estate listings site, by grabbing the ListID of active properties from by database table then comparing those ListID's to the photo names which are named like "ListID-1.jpg, ListID-2.jpg, etc..
 but I'm getting an error

"File C:\ColdFusion9\runtime\servers\coldfusion\SERVER-INF\temp\wwwroot-tmp\A100969-1.jpg specified in action delete does not exist."

The error occurred in C:\WWW\2013Fickling\rets.cfm: line 450
448 : <cfloop query="getFiles">
449 :   <cfif NOT listFindNoCase(iList,getFiles.name)>
450 :     <cffile action = "delete" file = "#getFiles.Name#">
451 :   </cfif>
452 : </cfloop>



when running the following code:

<!---DELETE OLD PICS--->
      
<cfquery name="getImages" datasource="mydata">
select ListID as imgfile from res
Order by 1
</cfquery>

<cfset iList = valueList(getImages.imgfile)>
<cfdirectory action = "LIST" directory = "#ExpandPath('/ftp/rets/photos/')#" name="getFiles">
<cfoutput>
<cfloop query="getFiles">
  <cfif NOT listFindNoCase(iList,getFiles.name)>
    <cffile action = "delete" file = "#getFiles.Name#">
  </cfif>
</cfloop>
</cfoutput>


A100969-1.jpg is a file in my photos folder that needs deleting, but for some reason the path is just wrong?
Any help appreciated!
0
Comment
Question by:Bang-O-Matic
  • 4
  • 4
8 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 39205976
>   <cffile action = "delete" file = "#getFiles.Name#">

My guess is CF adds a default/temp directory when you do not include the full path to the file. Try adding the "directory" too. Note - Not tested

  <cffile action = "delete" file = "#getFiles.directory##getFiles.Name#">
0
 

Author Comment

by:Bang-O-Matic
ID: 39206076
Hi _agx_ ,
That indeed fixed the error I was getting. Thanks!
Could you look through my code? something is amiss, when I ran that code it just deleted nearly all my images. It's supposed to compare the valid ListID's in my table against image names in my photos folder and just delete the ones I don't need.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39206373
Sorry - got called away :/ I cannot check it out now, but will review it when I get back.

Edit: But agreed the logic sounds off.  Can you elaborate on the kind of housekeeping? For example, are you trying to remove files that no longer have a matching db record - or something else>?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Bang-O-Matic
ID: 39206420
Yes  need to get rid of images that no longer have a matching db record.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39206567
I tested it and what you have should do exactly that.  Unless maybe the "imgfile" value doesn't match the one returned by cfdirectory?  Add some debugging to see if that's what's happening:

.... run query and get directory list ...
<strong>iList</strong>: <cfdump var="#iList#"><hr />
<cfoutput query="getFiles">
  <cfif NOT listFindNoCase(iList,getFiles.name)>
       DELETE #getFiles.directory##getFiles.Name#<br>
  <cfelse>
       KEEP #getFiles.directory##getFiles.Name#<br>
  </cfif>
</cfoutput>
0
 

Author Comment

by:Bang-O-Matic
ID: 39207843
Thanks _agx_,
I adjusted the query to get one ListiD.

iList: A120237

DELETE C:\WWW\2013Fickling\ftp\rets\photos\A120237-1.jpg
DELETE C:\WWW\2013Fickling\ftp\rets\photos\A120237-2.jpg
DELETE C:\WWW\2013Fickling\ftp\rets\photos\A120237-3.jpg
DELETE C:\WWW\2013Fickling\ftp\rets\photos\A120865-1.jpg
DELETE C:\WWW\2013Fickling\ftp\rets\photos\A120865-10.jpg
DELETE C:\WWW\2013Fickling\ftp\rets\photos\A120865-11.jpg
DELETE C:\WWW\2013Fickling\ftp\rets\photos\A120865-12.jpg
 etc...

So the file name "A120237-1.jpg is not an exact match to the ListID "A120237" so it's being deleted when it should be kept.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39208122
Ok, so there's two differences.  The db value doesn't contain a file extension or the numeric suffix  ie 1,2,3 ...10,11, ...

If the file names are always in this format:        

          AAAAAAAAA-XX.jpg

Just use list functions to grab the first part ie "A120237", then do the match like before

<cfoutput query="getFiles">
     <!--- split list on "-" and grab the 1st part --->
     <cfset valueToFind = listFirst(getFiles.name, "-")>
     <cfif NOT listFindNoCase(iList,valueToFind)>
          DELETE #getFiles.directory##getFiles.Name#<br>
      <cfelse>
           KEEP #getFiles.directory##getFiles.Name#<br>
      </cfif>
</cfoutput>
0
 

Author Comment

by:Bang-O-Matic
ID: 39208229
Thanks _agx_ that works perfect!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question