Ray Camden's Friday Puzzler - Compare Directories

Posted December 7, 2007 at 11:34 AM

Tags: ColdFusion

Ray Camden put up a Friday Puzzler to compare directories. This felt like a place to quickly throw some ColdFusion query of queries together. This solution will work with small directories, but certainly, I doubt that this will scale nicely as it would end up generating ENORMOUSLY SQL statements.

To start off with, I created two small directories:

Directory A:

  • \A\20071207.txt
  • \A\a-only.txt
  • \A\duplicate_file.txt

Directory B:

  • \B\20071207.txt
  • \B\b-only.txt
  • \B\duplicate_file.txt

Each directory has a file that is unique to them (a-only.txt and b-only.txt). Each on has a duplicate file (duplicate_file.txt). And, each on of them has a duplicate file that is a different size (20071207.txt). To compare these two directories from the directory, I ran this code:

 Launch code in new window » Download code as text file »

  • <!--- Query files from directory A. --->
  • <cfdirectory
  • action="list"
  • directory="#ExpandPath( './A/' )#"
  • name="qA"
  • />
  •  
  •  
  • <!--- Query files from directory B. --->
  • <cfdirectory
  • action="list"
  • directory="#ExpandPath( './B/' )#"
  • name="qB"
  • />
  •  
  •  
  • <!--- Query for files that are in A, but not in B. --->
  • <cfquery name="qAOnly" dbtype="query">
  • SELECT
  • [name]
  • FROM
  • qA
  • WHERE
  • [type] = 'File'
  • AND
  • (
  • 1 = 1
  •  
  • <!---
  • Make sure the current A record does NOT
  • match any records in the B query.
  • --->
  • <cfloop query="qB">
  •  
  • AND
  • [name] != '#qB.name#'
  •  
  • </cfloop>
  • )
  • </cfquery>
  •  
  •  
  • <!--- Query for files that are in B, but not in B. --->
  • <cfquery name="qBOnly" dbtype="query">
  • SELECT
  • [name]
  • FROM
  • qB
  • WHERE
  • [type] = 'File'
  • AND
  • (
  • 1 = 1
  •  
  • <!---
  • Make sure the current B record does NOT
  • match any records in the A query.
  • --->
  • <cfloop query="qA">
  •  
  • AND
  • [name] != '#qA.name#'
  •  
  • </cfloop>
  • )
  • </cfquery>
  •  
  •  
  • <!---
  • Query for files that are both in A and B, but not
  • on the same date or size.
  • --->
  • <cfquery name="qBoth" dbtype="query">
  • SELECT
  • qA.[name]
  • FROM
  • qA,
  • qB
  • WHERE
  • qA.[type] = 'File'
  • AND
  • qA.[name] = qB.[name]
  • AND
  • (
  • qA.datelastmodified != qB.datelastmodified
  • OR
  • qA.size != qB.size
  • )
  • </cfquery>
  •  
  •  
  •  
  • <!--- Dump out results. --->
  • <cfdump
  • var="#qAOnly#"
  • label="Directory A Only"
  • />
  •  
  • <br />
  •  
  • <cfdump
  • var="#qBOnly#"
  • label="Directory B Only"
  • />
  •  
  • <br />
  •  
  • <cfdump
  • var="#qBoth#"
  • label="Both But Different"
  • />

When we run that, we get this CFDump output:


 
 
 

 
Directory Compare Using ColdFusion Query of Queries  
 
 
 

So, it works for small little directory compares. I know anything about File Diffs or Directory Diffs, so I am sure that there are way more optimized ways of doing this. But, ColdFusion query of queries makes this a rather easy task.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Dec 7, 2007 at 4:41 PM // reply »
153 Comments

My solution also used QoQ, but differently:

http://www.coldfusionjedi.com/index.cfm/2007/12/7/Friday-Challenge--Compare-Directories#cB5E79338-19B9-E658-9D3917D4071BDB2D

-R


Dec 7, 2007 at 9:37 PM // reply »
6 Comments

Interesting approach. I requested the challenge since I have to write a script to see if previous people were doing their development on the development server or the production server :(

Here is a link to my original idea:
http://www.mediafire.com/?6yttt3x1kdt (download)
http://snippets.dzone.com/posts/show/4864 (view online)

Any thoughts on my solution would be appreciated.


Dec 10, 2007 at 8:01 AM // reply »
7,481 Comments

@Rick,

Your solution is very good. My hat is off to you, good sir. It's nice to see someone who really knows how to leverage ColdFusion query of queries. Sometimes I think they are one of the unsung heroes of ColdFusion.


Mar 21, 2008 at 12:18 PM // reply »
7 Comments

Thanks for the article, I was seeking it.


Mar 27, 2008 at 11:50 AM // reply »
1 Comments

"So you can find the information on it on my search resource
http://fileshunt.com"


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 11, 2010 at 9:14 AM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Just need to add - we still did have to get the external service to clean up their WSDL and flatten it as per the article I posted before ... read »
Mar 11, 2010 at 9:00 AM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
I finally got to the bottom of my problem - the fact was that the WSDL was full of complex types that Axis didn't like - I should have been able to receive it as a struct but that just didn't work so ... read »
Mar 11, 2010 at 8:55 AM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
@Dmitry, No problem my man. You should be able to just parse the SOAP response into XML and be able to access all the nodes that way. The only thing you have to be careful of is the namespaces whic ... read »
Mar 11, 2010 at 8:49 AM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
cfhttp response is located in it's FileContent property ???? You can't imagine how many times I had to work with cfinvoke and it's complex responses and their weird classes deserialization because I ... read »
Mar 11, 2010 at 8:43 AM
Ask Ben: Building An AJAX, jQuery, And ColdFusion Powered Application
@Nathan, Not saying this is the correct way to do this, but I've handled this in many different ways depending on how "secure" the function needs to be. In a case like yours (users needs to be l ... read »
Mar 11, 2010 at 7:57 AM
FLEX On jQuery: Turning HTML Links Into Standard UI Elements
@Peter, Yeah, that's how I build to - we have AJAX and jQuery where is enhances a given page... but we don't require it to tie the entire application together (it's still very much a request-respon ... read »
Mar 11, 2010 at 5:38 AM
Delaying ColdFusion Session Persistence Until User Logs In
Hmmm, I didn't get any email notification here - I definitely selected all the checkboxes (they're still ticked). ... read »
Mar 11, 2010 at 5:05 AM
Ask Ben: Selecting XML Attributes Given Other XML Attributes
Seems like I'm missing something. If I take this code and put it in a .cfm file and try to run it I get this error: Detail Premature end of file. ErrNumber 0 ExceptionMessage Premature end of f ... read »