Solved

Mass delete files from server?

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to get function 52 106
Webserver access problem 5 73
Automate ColdFusion Internet Form with VBA from Access 10 51
Domino Website - Redirection 12 76
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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