Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 542
  • Last Modified:

Coldfusion cffile delete question

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
Bang-O-Matic
Asked:
Bang-O-Matic
  • 4
  • 4
1 Solution
 
_agx_Commented:
>   <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
 
Bang-O-MaticAuthor Commented:
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
 
_agx_Commented:
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
Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

 
Bang-O-MaticAuthor Commented:
Yes  need to get rid of images that no longer have a matching db record.
0
 
_agx_Commented:
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
 
Bang-O-MaticAuthor Commented:
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
 
_agx_Commented:
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
 
Bang-O-MaticAuthor Commented:
Thanks _agx_ that works perfect!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now