Solved

Coldfusion cffile delete question

Posted on 2013-05-29
8
517 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
 

Author Comment

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

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Coldfusion 8 equivalent of the cf9 "new" 4 39
cfspreadsheet 15 84
Can I format excel columns with coldfusion 11? Using CFSPREADSHEET? 7 183
Cold Fusion Session Timing Out 11 60
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

920 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

17 Experts available now in Live!

Get 1:1 Help Now