Ray Camden's Friday Puzzler - Compare Directories

Posted December 7, 2007 at 11:34 AM by Ben Nadel

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:

  • <!--- 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.




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 »
10,640 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 A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »
Feb 9, 2012 at 10:29 PM
Learning ColdFusion 9: Application-Specific Data Sources
@Ben, No offence, but if people were really wanting advanced features they would be using a platform like ASP.NET MVC. CFML is so structurally compromised as a tag-based scripting language that ... read »
Feb 9, 2012 at 10:03 PM
Subversion - Cleanup Failed To Process The Following Paths
@Leviaguirre, do you still have problems with this? ... read »