Solved

Mass delete files from server?

Posted on 2011-03-19
3
344 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
[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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
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