1

In my application the user uploads a photos and the cfc resizes the photo, creates a new image and resizes that new image to a thumbnail. Trouble is, this function wasn't available earlier on in the game. I want to now look at the images directory and figure out which images don't have thumbnails.

I'm thinking that I could use cfdirectory to output a struct of both directories then loop over the files that only exist in the images and not in the thumbnails directory and run a function to resize the images and send them to the thumbnails directory.

Is this flawed thinking? Is there an easier way?

Ofeargall
  • 5,340
  • 5
  • 28
  • 33
  • For a one time operation? How many files in each and what is the naming convention? – Leigh Oct 20 '11 at 23:50
  • @Leigh It's a one-time operation. There's about 500 files in one folder and 400 in the thumbnails folder. They all have the same name just different folders... I use the file name of the thumbnail with some jQuery to call the larger image from the images folder. – Ofeargall Oct 20 '11 at 23:57

1 Answers1

5

That's a perfectly reasonable approach, and you don't even have to use recursive code. Just use the recursive option in CFDirectory to get a list of all files and use both the file name and path combined as the key, which guarantees a unique file you're checking. You may have to modify the result a bit so you know exactly where to put the new thumbnail, but this should get you pretty close.

<cfset originals_path = expandPath('originals') />
<cfset thumbs_path = expandPath('thumbs') />

<cfset no_thumbs = find_missing_thumbs(originals_path, thumbs_path) />
<cfdump var="#no_thumbs#" />

<cffunction name="find_missing_thumbs">
    <cfargument name="o" />
    <cfargument name="t" />

    <cfset var originals = 0 />
    <cfset var thumbs = 0 />
    <cfset var missing_thumbs = [] />
    <cfset var massaged_originals = 0 />
    <cfset var massaged_thumbs = 0 />
    <cfset var qSearch = 0 />

    <cfdirectory action="list" directory="#arguments.o#" name="originals" recurse="true" />
    <cfdirectory action="list" directory="#arguments.t#" name="thumbs" recurse="true" />

    <cfquery name="massaged_originals" dbtype="query">
        select name, directory + name as fullpath from originals
    </cfquery>
    <cfquery name="massaged_thumbs" dbtype="query">
        select name, directory + name as fullpath from thumbs
    </cfquery>

    <cfloop query="massaged_originals">
        <cfquery name="qSearch" dbtype="query">
            select massaged_thumbs.name from massaged_thumbs where massaged_thumbs.fullpath = '#massaged_originals.fullpath#'
        </cfquery>
        <cfif qSearch.recordCount eq 0>
            <cfset arrayAppend(missing_thumbs, massaged_originals.name) />
        </cfif>
    </cfloop>
    <cfreturn missing_thumbs />
</cffunction>
Adam Tuttle
  • 19,505
  • 17
  • 80
  • 113
  • I ran across this little gem the other day and it seems to work pretty well! It makes use of `not fileExists` when comparing directories. ` ` – Ofeargall Nov 04 '11 at 03:35
  • The whole thing starts with ``... And it only works if the images in both folders have the same name. – Ofeargall Nov 04 '11 at 03:36