Solved

Coldfusion cffile delete question

Posted on 2013-05-29
8
528 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

739 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