Use cfdirectory to find unmatched files in two directories and perform function

545 views Asked by At

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?

1

There are 1 answers

2
Adam Tuttle On BEST ANSWER

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>