DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Kris has posted 1 posts at DZone. View Full User Profile

Directory Compare

12.07.2007
| 3843 views |
  • submit to reddit
        ColdFusion - Compare two directories and report: 
1. What files exist in folder A, but not B. 
2. What files exist in folder B, but not A.
3. What files exist in both, but appear different.

<cfsetting requesttimeout="9999">

<cfparam name="FORM.DevelopmentDirectory" default="/kkkkkk/lllll/bbbb/ccc">
<cfparam name="FORM.ProductionDirectory" default="/mmmmmm/ggggg/bbbb/ccc">
<cfparam name="FORM.CommonDirectory" default="/ccc">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Directory Compare</title>
</head>

<body>

<h3>Directory Compare</h3>
<form name="directoryCompare" action="index.cfm" method="post">
<table>
	<tr>
    	<td>Development Directory</td>
        <td><input name="DevelopmentDirectory" id="DevelopmentDirectory" type="text" value="<cfoutput>#FORM.DevelopmentDirectory#</cfoutput>" size="70"></td>
    </tr>
	<tr>
    	<td>Production Directory</td>
        <td><input name="ProductionDirectory" id="ProductionDirectory" type="text" value="<cfoutput>#FORM.ProductionDirectory#</cfoutput>" size="70"></td>
    </tr>
	<tr>
    	<td>Common Directory</td>
        <td><input name="CommonDirectory" id="CommonDirectory" type="text" value="<cfoutput>#FORM.CommonDirectory#</cfoutput>" size="40"></td>
    </tr>
    <tr>
    	<td> </td>
        <td><input type="submit" name="submit" value="submit"></td>
    </tr>
</table>
</form>

<cfif isdefined('FORM.submit')>

<cfdirectory action="list" directory="#FORM.ProductionDirectory#" name="prodDir" recurse="yes">
<!--- <cfdump var="#prodDir#"> --->
<cfdirectory action="list" directory="#FORM.DevelopmentDirectory#" name="devDir" recurse="yes">
<!--- <cfdump var="#devDir#"> --->


<!--- Need common directory base to compare --->
<cfset dev_strip = Mid(FORM.DevelopmentDirectory,1,FindNoCase(FORM.CommonDirectory,FORM.DevelopmentDirectory))>
<cfset prod_strip = Mid(FORM.ProductionDirectory,1,FindNoCase(FORM.CommonDirectory,FORM.ProductionDirectory))>


<!--- o = Output Container --->
<cfset o = StructNew()>
<cfset o.keys = ArrayNew(1)>


<cfloop query="prodDir">

	<!--- Put array string in the order that will sort correctly --->
	<cfset L = prodDir.TYPE & "|" & ReplaceNoCase(prodDir.DIRECTORY,prod_strip,"","all") & "/" & prodDir.NAME >
	<!--- Turn the type/dir/name into something that makes a valid variable name --->
	<cfset k = "K" & cfusion_encrypt(L,"key")>

	<!--- Have we encountered this entry before? --->
	<!--- This also filters duplicates between the two queries. --->
	<cfif not StructKeyExists(o,k)>
		<!--- Add to list of valid values --->
		<cfset ArrayAppend(o.keys,L & "|" & k)>
        <cfset o[k] = StructNew()>
		<cfset o[k].prod_path = "">
		<cfset o[k].prod_type = "">
		<cfset o[k].prod_size = "">
		<cfset o[k].dev_path = "">
		<cfset o[k].dev_type = "">
		<cfset o[k].dev_size = "">
	</cfif>

	<cfset o[k].prod_path = prodDir.DIRECTORY & "/" & prodDir.NAME>
	<cfset o[k].prod_type = prodDir.TYPE>
	<cfset o[k].prod_size = prodDir.SIZE>

</cfloop>
<!--- <cfdump var="#o#"> --->


<cfloop query="devDir">

	<!--- Put array string in the order that will sort correctly --->
	<cfset L = devDir.TYPE & "|" & ReplaceNoCase(devDir.DIRECTORY,dev_strip,"","all") & "/" & devDir.NAME >
	<!--- Turn the type/dir/name into something that makes a valid variable name --->
	<cfset k = "K" & cfusion_encrypt(L,"key")>

	<!--- Have we encountered this entry before? --->
	<!--- This also filters duplicates between the two queries. --->
	<cfif not StructKeyExists(o,k)>
		<!--- Add to list of valid values --->
		<cfset ArrayAppend(o.keys,L & "|" & k)>
        <cfset o[k] = StructNew()>
		<cfset o[k].prod_path = "">
		<cfset o[k].prod_type = "">
		<cfset o[k].prod_size = "">
		<cfset o[k].dev_path = "">
		<cfset o[k].dev_type = "">
		<cfset o[k].dev_size = "">
	</cfif>

	<cfset o[k].dev_path = devDir.DIRECTORY & "/" & devDir.NAME>
	<cfset o[k].dev_type = devDir.TYPE>
	<cfset o[k].dev_size = devDir.SIZE>

</cfloop>
<!--- <cfdump var="#o#"> --->


<cfset ArraySort(o.keys,"textnocase","asc")>


<cfoutput>
<table cellspacing="0" border="0" cellpadding="3">
	<tr>
        <th>Development</th>
    	<th>Production</th>
    </tr>
	<cfloop index="i" from="1" to="#ArrayLen(o.keys)#">
	    <cfset k = ListLast(o.keys[i],"|")>
	<tr>
    	<!--- DEV --->
        <td style="border-bottom:1px solid ##CCCCCC;">
        	<cfif o[k].prod_path EQ "">
            	<!--- On Dev NOT on Prod --->
				<span style="color:##0000FF; font-weight:bold;">
            <cfelse>
            	<cfif o[k].prod_size NEQ o[k].dev_size>
                	<!--- On Dev and On Prod, but Differnet --->
					<span style="color:##FF9900; font-weight:bold;">
                <cfelse>
                	<!--- On Dev and On Prod and The Same (probably) --->
					<span>
				</cfif>
			</cfif>
        	#o[k].dev_path# 
            </span>
			<cfif o[k].dev_path NEQ "">
            <span style="font-size:smaller; color:##999999;">(#o[k].dev_size#)</span>
            </cfif>
        </td>
        <!--- PROD --->
    	<td style="border-bottom:1px solid ##CCCCCC;">
        	<cfif o[k].dev_path EQ "">
            	<!--- On Prod NOT On Dev --->
				<span style="color:##FF0000; font-weight:bold;">
            <cfelse>
            	<cfif o[k].prod_size NEQ o[k].dev_size>
                	<!--- On Dev and On Prod, but Differnet --->
					<span style="color:##FF9900; font-weight:bold;">
                <cfelse>
                	<!--- On Dev and On Prod and The Same (probably) --->
					<span>
				</cfif>
			</cfif>
        	#o[k].prod_path# 
            </span>
			<cfif o[k].prod_path NEQ "">
            <span style="font-size:smaller; color:##999999;">(#o[k].prod_size#)</span>
            </cfif>
        </td>
    </tr>
    </cfloop>
</table>
</cfoutput>
</cfif>

</body>
</html>