Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Mass delete files from server?

Posted on 2011-03-19
3
Medium Priority
?
349 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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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…
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 …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

772 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