Skip to main content
Ben Nadel at cf.Objective() 2010 (Minneapolis, MN) with: Doug Hughes and Ezra Parker and Dan Wilson and John Mason and Jason Dean and Luis Majano and Mark Mandel and Brian Kotek and Wil Genovese and Rob Brooks-Bilson and Andy Matthews and Simeon Bateman and Ray Camden and Chris Rockett and Joe Bernard and Dan Skaggs and Byron Raines and Barney Boisvert and Simon Free and Steve 'Cutter' Blades and Seth Bienek and Katie Bienek and Jeff Coughlin
Ben Nadel at cf.Objective() 2010 (Minneapolis, MN) with: Doug Hughes Ezra Parker Dan Wilson John Mason Jason Dean Luis Majano Mark Mandel Brian Kotek Wil Genovese Rob Brooks-Bilson Andy Matthews Simeon Bateman Ray Camden Chris Rockett Joe Bernard Dan Skaggs Byron Raines Barney Boisvert Simon Free Steve 'Cutter' Blades Seth Bienek Katie Bienek Jeff Coughlin

Ray Camden's Friday Puzzler - Compare Directories

By
Published in Comments (5)

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.

Want to use code from this post? Check out the license.

Reader Comments

16,115 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.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel
Managed hosting services provided by:
xByte Cloud Logo