Solved

Coldfusion cffile delete question

Posted on 2013-05-29
8
511 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
Comment Utility
>   <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
Comment Utility
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_
Comment Utility
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
 

Author Comment

by:Bang-O-Matic
Comment Utility
Yes  need to get rid of images that no longer have a matching db record.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 52

Expert Comment

by:_agx_
Comment Utility
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
Comment Utility
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_
Comment Utility
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
Comment Utility
Thanks _agx_ that works perfect!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now