Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

Delete images

Hi,

I want to delete all images that do not have any path stored in my database.

this is what I have.
<CFQUERY DATASOURCE="xx" NAME="DeleteNonAssociatedImages" cachedwithin="#createTimeSpan(0,0,0,-1)#">
	Select PathImage1, PathImage2, PathImage3, PathImage4, 
	SmlPathImage1,SmlPathImage2,SmlPathImage3,SmlPathImage4  from ADS
	where (PathImage1 <> "" or PathImage2 <> "" or PathImage3 <> "" 
	or PathImage4 <> "" or SmlPathImage1 <> "" or SmlPathImage2 <> "" 
	or SmlPathImage3 <> "" or SmlPathImage4 <> "")
</CFQUERY>

 <cfif isDefined("DeleteNonAssociatedImages") and DeleteNonAssociatedImages.recordcount gt 0>

	<cfset myArray = ArrayNew(1)> 

	<cfloop query = "DeleteNonAssociatedImages"> 
		<cfset DisplayData = ArrayAppend(myArray,'#trim(listLast(PathImage1, "/"))#,#trim(listLast(PathImage2, "/"))#,#trim(listLast(PathImage3, "/"))#,#trim(listLast(PathImage4, "/"))#,#trim(listLast(SmlPathImage1, "/"))#,#trim(listLast(SmlPathImage2, "/"))#,#trim(listLast(SmlPathImage3, "/"))#,#trim(listLast(SmlPathImage4, "/"))#')> 
	</cfloop> 

	<cfset myList = ArrayToList(myArray, ",")> 

  <cfloop list="#myList#" index="i">
   <cfoutput> #i# <br /></cfoutput>
  </cfloop>

the #i# output is this:
7_IMG3516.JPG 
7_Sml_IMG3516.JPG 
5_bgSearch.gif 
5_BTChat11.gif 
5_BTContactUs1.gif 
5_blackdot.gif 
5_Sml_bgSearch.gif 
5_Sml_BTChat11.gif 
5_Sml_BTContactUs1.gif 
5_Sml_blackdot.gif 
7_IMG3515.JPG 
7_Sml_IMG3515.JPG 
7_IMG3201.JPG 
7_Sml_IMG3201.JPG 
7_img006.jpg 
7_IMG2672.JPG 
7_IMG2681.JPG 
7_IMG2675.JPG 
7_Sml_img006.jpg 
7_Sml_IMG2672.JPG 
7_Sml_IMG2681.JPG 
7_Sml_IMG2675.JPG 
8_IMG20110614212835.jpg 
8_Sml_IMG20110614212835.jpg 
7_IMG2672.JPG 
7_IMG2675.JPG 
7_IMG2969.JPG 


the list above is a list of images that I have a path in my database so, anything other than the images above need to be deleted. 

This code below is wrong but I am trying my best. I have to delete all images that are not in my above list.

  <cfloop list="#myList#" index="i">
	<cfloop from="1" to="4" index="x">
		<cfif #i# neq fileExists(expandPath(PathImage[x]))>
		
			<cffile action = "delete" file = "#expandPath(SelectInactiveUserADS.PathImage[x])#">

		</cfif>
	</cfloop>
  </cfloop>

Open in new window

0
lulu50
Asked:
lulu50
  • 7
  • 4
1 Solution
 
gdemariaCommented:
Lulu, it seems a bit inside out to me..  If you start with your records in your database and then check the folder for these files, you are only looking for files in your database... but I think you said you want to find files that are NOT in your database.

I think you need to start with a list of files from your directory and then check to see if they are in the database.  If they are not, then you delete the file.

You can use <cfdirectory> to get a list of files from your directory (it returns a query so you can just swap it out for your deleteNonAssociatedImages query).

Then you loop through the query of your directory files, for each one, see if the file is in your database, if it is not, then delete the file.

Does that make sense?
0
 
lulu50Author Commented:
yes, it make sense that's what i should try to first.

I am going to start by list all my files that are in my directory first.

0
 
lulu50Author Commented:
I got this far this is listing all my images in my directory

this is the output:

array  
1 4.gif
2 6.gif
3 A1.gif
4 A3.gif


The directory existed: F:\xx\xx\xx.com\LoadImages

<cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "LoadImages">
<cfif DirectoryExists(currentDirectory)>
<cfdirectory directory="#currentDirectory#" name="dirQuery" action="LIST">

<!--- Get an array of directory names. --->
<cfset dirsArray=arraynew(1)>
<cfset i=1>
<cfloop query="dirQuery">
<cfif dirQuery.type IS "file">
    <cfset dirsArray[i]=dirQuery.name>
    <cfset i = i + 1>
</cfif>
</cfloop>
<cfdump var="#dirsArray#">
<br>
    <cfoutput>
    <p>The directory existed: #currentDirectory#</p>
    </cfoutput>
<cfelse>
    <cfoutput><p>No directory. #currentDirectory#</p></cfoutput>
</cfif>

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
lulu50Author Commented:
(it returns a query so you can just swap it out for your deleteNonAssociatedImages query).

I don't understand this part.

0
 
lulu50Author Commented:
<!--- how can I loop thru myList and thru my dirsArray to see if there is no match then delete the images. --->

dirsArray has a list of files names in my directory:

array  
1 4.gif
2 6.gif
3 A1.gif
4 A3.gif


Mylist has a list of images name in my database

7_IMG3516.JPG
7_Sml_IMG3516.JPG
5_bgSearch.gif
5_BTChat11.gif
5_BTContactUs1.gif
5_blackdot.gif
5_Sml_bgSearch.gif
5_Sml_BTChat11.gif
5_Sml_BTContactUs1.gif
5_Sml_blackdot.gif
7_IMG3515.JPG
7_Sml_IMG3515.JPG
7_IMG3201.JPG
7_Sml_IMG3201.JPG
7_img006.jpg
7_IMG2672.JPG
<CFQUERY DATASOURCE="lulu50_ChurchesBulleti" NAME="DeleteNonAssociatedImages" cachedwithin="#createTimeSpan(0,0,0,-1)#">
	Select PathImage1, PathImage2, PathImage3, PathImage4, 
	SmlPathImage1,SmlPathImage2,SmlPathImage3,SmlPathImage4  from ADS
	where (PathImage1 <> "" or PathImage2 <> "" or PathImage3 <> "" 
	or PathImage4 <> "" or SmlPathImage1 <> "" or SmlPathImage2 <> "" 
	or SmlPathImage3 <> "" or SmlPathImage4 <> "")
</CFQUERY>


<cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "LoadImages">
<cfif DirectoryExists(currentDirectory)>
<cfdirectory directory="#currentDirectory#" name="dirQuery" action="LIST">

<!--- Get an array of directory names. --->
<cfset dirsArray=arraynew(1)>
<cfset i=1>
<cfloop query="dirQuery">
<cfif dirQuery.type IS "file">
    <cfset dirsArray[i]=dirQuery.name>
    <cfset i = i + 1>
</cfif>
</cfloop>

 <cfif isDefined("DeleteNonAssociatedImages") and DeleteNonAssociatedImages.recordcount gt 0>

 <cfset myArray = ArrayNew(1)> 

	<cfloop query = "DeleteNonAssociatedImages"> 
		<cfset DisplayData = ArrayAppend(myArray,'#trim(listLast(PathImage1, "/"))#,#trim(listLast(PathImage2, "/"))#,#trim(listLast(PathImage3, "/"))#,#trim(listLast(PathImage4, "/"))#,#trim(listLast(SmlPathImage1, "/"))#,#trim(listLast(SmlPathImage2, "/"))#,#trim(listLast(SmlPathImage3, "/"))#,#trim(listLast(SmlPathImage4, "/"))#')> 
	</cfloop> 

	<cfset myList = ArrayToList(myArray, ",")> 

 <!--- how can I loop thru myList and thru my dirsArray to see if there is no match then delete the images. --->
 
 
 </cfif>

<cfdump var="#dirsArray#">
<br>
    <cfoutput>
    <p>The directory existed: #currentDirectory#</p>
    </cfoutput>
<cfelse>
    <cfoutput><p>No directory. #currentDirectory#</p></cfoutput>
</cfif>

Open in new window

0
 
gdemariaCommented:
Try something like this...

In each loop of your list list, see if the image is in any of the fields of the table.  You may have to play with this a bit so you can be sure to get just the filename

For example, you don't want a file called   "3_test.jpg"  to match a file called "123_test.jpg"

(instead of putting in the CFFILE delete, I just put in "will delete this file..." so you can test without deleting any files)
<cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "LoadImages">
<cfif DirectoryExists(currentDirectory)>
<cfdirectory directory="#currentDirectory#" name="dirQuery" action="LIST">

<!--- Get an array of directory names. --->
<cfset dirsArray=arraynew(1)>
<cfset i=1>
<cfloop query="dirQuery">
<cfif dirQuery.type IS "file">
    <cfset fileName = dirQuery.file>
    <cfquery name="checkDB" datasource="#request.datasource#">
      select 1 from ADS
      where PathImage1 like '%/#fileName#'
      or    SmlPathImage2 like '%/#fileName#'
      or    SmlPathImage3 like '%/#fileName#'
      or    SmlPathImage4 like '%/#fileName#'
    </cfquery>
    <cfif checkDB.recordCount eq 0>
        Will delete this file #fileName#
    </cfif>    
</cfif>
</cfloop>

Open in new window

0
 
lulu50Author Commented:
gdemaria:

I test it.

it works great!!!  

it does select the file that I don't want it to be stored in my folder.

now, how can I delete those images?

is it something like this?

 <cffile action = "delete" file = "#fileName#">
<cfset currentDirectory = GetDirectoryFromPath(GetTemplatePath()) & "LoadImages">
<cfif DirectoryExists(currentDirectory)>
<cfdirectory directory="#currentDirectory#" name="dirQuery" action="LIST">

<!--- Get an array of directory names. --->
<cfset dirsArray=arraynew(1)>
<cfset i=1>
<cfloop query="dirQuery">
<cfif dirQuery.type IS "file">
    <cfset fileName = dirQuery.name>
    <cfquery name="checkDB" datasource="xx">
      select 1 from ADS
      where PathImage1 like '%/#fileName#' 
	  or    PathImage2 like '%/#fileName#'
	  or    PathImage3 like '%/#fileName#'
	  or    PathImage4 like '%/#fileName#' 
	  or    SmlPathImage1 like '%/#fileName#'
      or    SmlPathImage2 like '%/#fileName#'
      or    SmlPathImage3 like '%/#fileName#'
      or    SmlPathImage4 like '%/#fileName#'
    </cfquery>
    <cfif checkDB.recordCount eq 0><br>
	
	is this the way to delete?

 <cffile action = "delete" file = "#fileName#">

        Will delete this file <cfoutput>#fileName#</cfoutput>
    </cfif>    
</cfif>
</cfloop>
</cfif>

Open in new window

0
 
gdemariaCommented:
>  is it something like this?
    <cffile action = "delete" file = "#fileName#">


Yes, something like that... (excpet I think fileName just has the name of the file, not the full path)).   You want the full path to the file, it should come from the dirQuery

<cfloop query="dirQuery">

Please check the documentation to see what value gives you the full path to the file, I don't remember it..

 #dirQuery.filepath# or  #dirQuery.file#

whichever has the full path C:\inetputb\....etc...\myFile.jpg

 

0
 
lulu50Author Commented:
I did this

<cffile action = "delete" file = "#currentDirectory#/#fileName#">

it works great!!!!

Thank you for your help.  


0
 
lulu50Author Commented:
Excellent!!!!!

Thank you
0
 
gdemariaCommented:
Super!  Glad to hear it.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now