Solved

Mass delete files from server?

Posted on 2011-03-19
3
335 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
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 video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

22 Experts available now in Live!

Get 1:1 Help Now