Solved

Mass delete files from server?

Posted on 2011-03-19
3
341 Views
Last Modified: 2013-12-24
I'm letting my members update multi images at the same time using a form. If I wanted to let them select the images they wanted to delete, how do I delete the files off the server. Do I use some form of a loop. I'm using cffile action for one but don't know how to do more then one at the same time??
0
Comment
Question by:overcolor
  • 2
3 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 35173904
Yep. You'd loop and delete each file separately.  Say your delete option is a bunch of checkboxes with the same name. The user selects the image records they want to delete. The selected record numbers are submitted as a list:

ie   form.selectedImages = 1,4,5,6,22

Use the list to query your db and return the file names you want to delete

<cfparam name="form.selectedImages" default="0">
<cfquery name="getImagesToDelete" ....>
        SELECT   FileName
        FROM      YourTable
        WHERE   RecordID IN (
             <cfqueryparam value="#form.selectedImages#" list="true" cfsqltype="cf_sql_integer">  
        )
</cfquery>

Open in new window


Then loop through the query and delete each file

<cfloop query="getImagesToDelete">
     <cffile action="delete" file="c:\path\to\#FileName#">
</cfloop>

Open in new window


 
0
 

Author Comment

by:overcolor
ID: 35174103
@Agx

Here is what I'm using now, is this correct?

Thank you for your help
<cfif isdefined('deleteimage')>
  
 <cfif fileExists('#mainsitepath#\images\photos\thumbs\#pics.image#')>

<cffile 
action = "delete"
file = "#mainsitepath#\images\photos\thumbs\#pics.image#">
</cfif>

    <cfparam name="form.deleteImage" default="">

<cfif listLen(form.deleteImage)>
     <cfquery datasource="#dns#">
     DELETE FROM photos
     WHERE  ID  IN 
     (
       <cfqueryparam value="#form.deleteImage#" cfsqltype="cf_sql_integer" list="true"> 
     )        
    </cfquery>
 </cfif> 

 </cfif>

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
ID: 35185374
Sorry for the delayed response.

Almost.  To delete multiple files, you'd need to run a query first so you can retrieve the physical file names. Once you have the query, loop through it and delete the files. Finally, delete the db records.

I don't know all of your column names, but it'd be something like this (not tested)

<cfparam name="form.deleteImage" default="">

<!--- if there's something to delete ....--->
<cfif listLen(form.deleteImage)>

     <!--- first get names of the files to be deleted ---->
     <cfquery name="getImagesToDelete" datasource="#dns#">
             SELECT   ID, TheFileName
             FROM      photos
             WHERE   ID IN (
                  <cfqueryparam value="#form.deleteImage#" list="true" cfsqltype="cf_sql_integer">  
             )
     </cfquery>

     <!--- next delete the image files on disk ---->
     <cfloop query="getImagesToDelete">
         <cfset pathToImage = "#mainsitepath#\images\photos\thumbs\#TheFileName#">
         <cfif FileExists(pathToImage)>
             <cffile action="delete" file="#pathToImage#">
         </cfif>
     </cfloop>

     <!--- finally, delete the image records ---->
        <cfquery datasource="#dns#">
          DELETE FROM photos
          WHERE  ID  IN 
          (
            <cfqueryparam value="#form.deleteImage#" cfsqltype="cf_sql_integer" list="true"> 
          )        
         </cfquery>
 </cfif> 

Open in new window

0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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