Never been to DZone Snippets before?

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

« Newer Snippets
Older Snippets »
Showing 1-10 of 15 total  RSS 

Directory Compare

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.

   1  
   2  <cfsetting requesttimeout="9999">
   3  
   4  <cfparam name="FORM.DevelopmentDirectory" default="/kkkkkk/lllll/bbbb/ccc">
   5  <cfparam name="FORM.ProductionDirectory" default="/mmmmmm/ggggg/bbbb/ccc">
   6  <cfparam name="FORM.CommonDirectory" default="/ccc">
   7  
   8  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   9  <html xmlns="http://www.w3.org/1999/xhtml">
  10  <head>
  11  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  12  <title>Directory Compare</title>
  13  </head>
  14  
  15  <body>
  16  
  17  <h3>Directory Compare</h3>
  18  <form name="directoryCompare" action="index.cfm" method="post">
  19  <table>
  20  	<tr>
  21      	<td>Development Directory</td>
  22          <td><input name="DevelopmentDirectory" id="DevelopmentDirectory" type="text" value="<cfoutput>#FORM.DevelopmentDirectory#</cfoutput>" size="70"></td>
  23      </tr>
  24  	<tr>
  25      	<td>Production Directory</td>
  26          <td><input name="ProductionDirectory" id="ProductionDirectory" type="text" value="<cfoutput>#FORM.ProductionDirectory#</cfoutput>" size="70"></td>
  27      </tr>
  28  	<tr>
  29      	<td>Common Directory</td>
  30          <td><input name="CommonDirectory" id="CommonDirectory" type="text" value="<cfoutput>#FORM.CommonDirectory#</cfoutput>" size="40"></td>
  31      </tr>
  32      <tr>
  33      	<td>&nbsp;</td>
  34          <td><input type="submit" name="submit" value="submit"></td>
  35      </tr>
  36  </table>
  37  </form>
  38  
  39  <cfif isdefined('FORM.submit')>
  40  
  41  <cfdirectory action="list" directory="#FORM.ProductionDirectory#" name="prodDir" recurse="yes">
  42  <!--- <cfdump var="#prodDir#"> --->
  43  <cfdirectory action="list" directory="#FORM.DevelopmentDirectory#" name="devDir" recurse="yes">
  44  <!--- <cfdump var="#devDir#"> --->
  45  
  46  
  47  <!--- Need common directory base to compare --->
  48  <cfset dev_strip = Mid(FORM.DevelopmentDirectory,1,FindNoCase(FORM.CommonDirectory,FORM.DevelopmentDirectory))>
  49  <cfset prod_strip = Mid(FORM.ProductionDirectory,1,FindNoCase(FORM.CommonDirectory,FORM.ProductionDirectory))>
  50  
  51  
  52  <!--- o = Output Container --->
  53  <cfset o = StructNew()>
  54  <cfset o.keys = ArrayNew(1)>
  55  
  56  
  57  <cfloop query="prodDir">
  58  
  59  	<!--- Put array string in the order that will sort correctly --->
  60  	<cfset L = prodDir.TYPE & "|" & ReplaceNoCase(prodDir.DIRECTORY,prod_strip,"","all") & "/" & prodDir.NAME >
  61  	<!--- Turn the type/dir/name into something that makes a valid variable name --->
  62  	<cfset k = "K" & cfusion_encrypt(L,"key")>
  63  
  64  	<!--- Have we encountered this entry before? --->
  65  	<!--- This also filters duplicates between the two queries. --->
  66  	<cfif not StructKeyExists(o,k)>
  67  		<!--- Add to list of valid values --->
  68  		<cfset ArrayAppend(o.keys,L & "|" & k)>
  69          <cfset o[k] = StructNew()>
  70  		<cfset o[k].prod_path = "">
  71  		<cfset o[k].prod_type = "">
  72  		<cfset o[k].prod_size = "">
  73  		<cfset o[k].dev_path = "">
  74  		<cfset o[k].dev_type = "">
  75  		<cfset o[k].dev_size = "">
  76  	</cfif>
  77  
  78  	<cfset o[k].prod_path = prodDir.DIRECTORY & "/" & prodDir.NAME>
  79  	<cfset o[k].prod_type = prodDir.TYPE>
  80  	<cfset o[k].prod_size = prodDir.SIZE>
  81  
  82  </cfloop>
  83  <!--- <cfdump var="#o#"> --->
  84  
  85  
  86  <cfloop query="devDir">
  87  
  88  	<!--- Put array string in the order that will sort correctly --->
  89  	<cfset L = devDir.TYPE & "|" & ReplaceNoCase(devDir.DIRECTORY,dev_strip,"","all") & "/" & devDir.NAME >
  90  	<!--- Turn the type/dir/name into something that makes a valid variable name --->
  91  	<cfset k = "K" & cfusion_encrypt(L,"key")>
  92  
  93  	<!--- Have we encountered this entry before? --->
  94  	<!--- This also filters duplicates between the two queries. --->
  95  	<cfif not StructKeyExists(o,k)>
  96  		<!--- Add to list of valid values --->
  97  		<cfset ArrayAppend(o.keys,L & "|" & k)>
  98          <cfset o[k] = StructNew()>
  99  		<cfset o[k].prod_path = "">
 100  		<cfset o[k].prod_type = "">
 101  		<cfset o[k].prod_size = "">
 102  		<cfset o[k].dev_path = "">
 103  		<cfset o[k].dev_type = "">
 104  		<cfset o[k].dev_size = "">
 105  	</cfif>
 106  
 107  	<cfset o[k].dev_path = devDir.DIRECTORY & "/" & devDir.NAME>
 108  	<cfset o[k].dev_type = devDir.TYPE>
 109  	<cfset o[k].dev_size = devDir.SIZE>
 110  
 111  </cfloop>
 112  <!--- <cfdump var="#o#"> --->
 113  
 114  
 115  <cfset ArraySort(o.keys,"textnocase","asc")>
 116  
 117  
 118  <cfoutput>
 119  <table cellspacing="0" border="0" cellpadding="3">
 120  	<tr>
 121          <th>Development</th>
 122      	<th>Production</th>
 123      </tr>
 124  	<cfloop index="i" from="1" to="#ArrayLen(o.keys)#">
 125  	    <cfset k = ListLast(o.keys[i],"|")>
 126  	<tr>
 127      	<!--- DEV --->
 128          <td style="border-bottom:1px solid ##CCCCCC;">
 129          	<cfif o[k].prod_path EQ "">
 130              	<!--- On Dev NOT on Prod --->
 131  				<span style="color:##0000FF; font-weight:bold;">
 132              <cfelse>
 133              	<cfif o[k].prod_size NEQ o[k].dev_size>
 134                  	<!--- On Dev and On Prod, but Differnet --->
 135  					<span style="color:##FF9900; font-weight:bold;">
 136                  <cfelse>
 137                  	<!--- On Dev and On Prod and The Same (probably) --->
 138  					<span>
 139  				</cfif>
 140  			</cfif>
 141          	#o[k].dev_path#&nbsp;
 142              </span>
 143  			<cfif o[k].dev_path NEQ "">
 144              <span style="font-size:smaller; color:##999999;">(#o[k].dev_size#)</span>
 145              </cfif>
 146          </td>
 147          <!--- PROD --->
 148      	<td style="border-bottom:1px solid ##CCCCCC;">
 149          	<cfif o[k].dev_path EQ "">
 150              	<!--- On Prod NOT On Dev --->
 151  				<span style="color:##FF0000; font-weight:bold;">
 152              <cfelse>
 153              	<cfif o[k].prod_size NEQ o[k].dev_size>
 154                  	<!--- On Dev and On Prod, but Differnet --->
 155  					<span style="color:##FF9900; font-weight:bold;">
 156                  <cfelse>
 157                  	<!--- On Dev and On Prod and The Same (probably) --->
 158  					<span>
 159  				</cfif>
 160  			</cfif>
 161          	#o[k].prod_path#&nbsp;
 162              </span>
 163  			<cfif o[k].prod_path NEQ "">
 164              <span style="font-size:smaller; color:##999999;">(#o[k].prod_size#)</span>
 165              </cfif>
 166          </td>
 167      </tr>
 168      </cfloop>
 169  </table>
 170  </cfoutput>
 171  </cfif>
 172  
 173  </body>
 174  </html>
 175  

Email a JavaScript Error using ColdFusion

This script emails an administrator when an error occurs, and could easily be adapted to log the error instead

   1  
   2  /*
   3  wwJsError (requires prototype.js)
   4  		send:	sends javascript error output email via coldfusion
   5  		--------------------
   6  			path:		relative path from the calling page to the coldfusion
   7  					email handler for this application (usually processJSerror.cfm)
   8  			funcname:	name of the JavaScript function that threw an error
   9  			name:		JavaScript error name (usually e.name in a catch block)
  10  			message:	JavaScript error message (usually e.message in a catch block)
  11  */
  12  var wwJsError = {
  13  	send: function(path,funcname,name,message) {
  14  		try {
  15  
  16  			var url = path;
  17  
  18  			var pars = 'message=' + escape(message) + '&name=' +
  19  					escape(name) + '&funcname=' + escape(funcname);
  20  			
  21  			var httpMail = new Ajax.Request (
  22  				url, 
  23  				{
  24  					method: 'post', 
  25  					parameters: pars, 
  26  					onComplete: Prototype.emptyFunction
  27  				}
  28  			);
  29  	
  30  		} catch (e) {
  31  			//if sending the email fails, just die quietly w/ no browser error
  32  			Prototype.emptyFunction();
  33  		}
  34  	}
  35  }; //end wwJsError

Sample server-side script in ColdFusion
   1  
   2  <cfmail to="#application.administratoremail#" from="#application.administratoremail#"
   3  		subject="#application.apptitle# Error" type="HTML">
   4  	<strong>JavaScript Error:</strong> #form.funcname# threw the following error:<br />
   5  	<br />
   6  	<strong>Error name:</strong> #form.name#<br />
   7  	<strong>Error:</strong> #form.message#
   8  </cfmail>

Generate the error
   1  
   2  function makeError() {
   3  	try {
   4  		alert(anError);
   5  	} catch(e) {
   6  		wwJsError.send('cf/processJSerror.cfm','makeError()',e.name,e.message);
   7  	}
   8  }

Result by email

JavaScript Error: makeError threw the following error:

Error name: TypeError
Error: 'anError' is undefined

Current year

// outputs current year

   1  
   2  #year(now())#.

Clean magic quoting in ColdFusion 5 and later

I'm updating my weblog software and needed some code to properly convert typewriter quotes to proper typographer's quotes.

   1  
   2  <cfscript>
   3  function magicQuote(txt) {
   4      // Left quotes
   5      txt = REReplace(txt, "(^|[ " & Chr(9) & Chr(10) & Chr(13) & "'])""", "\1&##8220;", "ALL");
   6      txt = REReplace(txt, "(^|[ " & Chr(9) & Chr(10) & Chr(13) & "]|&##8220;)'",  "\1&##8216;", "ALL");
   7  
   8      // Right quotes
   9      txt = Replace(txt, """",        "&##8221;",  "ALL");
  10      txt = Replace(txt, "'&##8220;", "'&##8221;", "ALL");
  11      txt = Replace(txt, "'",         "&##8217;",  "ALL");
  12  
  13      return txt;
  14  }
  15  </cfscript>

ColdFusion 5 has problems with \s, and the character class is a workaround for this.

My ColdFusion Tagging Engine/Library

//via:http://www.whatspop.com/blog/2005/12/my-coldfusion-tagging-enginelibrary.cfm

   1  
   2  <cfset tags_list = " CaMELCaSE comma, 'singlequoted' <b>bold</b> ""doublequoted"" double double this should be over the limit now " />

   1  
   2  <cfset amount = 10 />
   3  <cfset length = 20 />

   1  
   2  <cfset tags_list = trim(tags_list) />
   3  <cfset tags_list = lcase(tags_list) />

   1  
   2  <cfset tags_struct = structnew() />
   3  <cfloop index="tag" list="#tags_list#" delimiters=" ">
   4  <cfset tags_struct[tag] = "" />
   5  </cfloop>
   6  <cfset tags_list = structkeylist(tags_struct," ") />
   7  <cfset tags_list = listsort(tags_list, "textnocase", "asc", " ") />

   1  
   2  <cfif tags_list neq "">
   3  <cfif listlen(tags_list," ") lte amount>
   4  <cfset amount = listlen(tags_list," ") />
   5  </cfif>
   6  <cfloop from="1" to="#amount#" index="tag">
   7  /* Perform a database upload here */
   8  <cfoutput>#htmleditformat(left(listgetat(tags_list,tag," "),length))#</cfoutput>
   9  </cfloop>
  10  <cfelse>
  11  Sorry, you need to provide at least one tag
  12  </cfif>

Customtag to remove blank lines

// description of your code here
Coldfusion output has a lot of useless blank lines and makes the HTML code hard to read. I use this customtag to remove the blank lines.

   1  
   2  <cfif thisTag.ExecutionMode IS "end">
   3  	<cfset thisTag.generatedContent =  ReReplaceNoCase(thisTag.generatedContent, '(?m)^\s+\r\n', '', "ALL")>
   4  </cfif>

Creating a PDF using iText and ColdFusion

// description of your code here

   1  
   2  <cfscript>
   3  // create document
   4  document 		= CreateObject("java", "com.lowagie.text.Document");
   5  document.init();
   6  
   7  // writer
   8  fileIO 			= CreateObject("java", "java.io.FileOutputStream");
   9  fileIO.init(pdf_path);
  10  writer 			= CreateObject("java", "com.lowagie.text.pdf.PdfWriter");
  11  writer.getInstance(document, fileIO);
  12  document.open();
  13  
  14  // newsinfo header image
  15  Image 			= CreateObject("java", "com.lowagie.text.Image");
  16  jpg 			= Image.getInstance(header_image);
  17  jpg.setAbsolutePosition(28, 713);
  18  jpg.setDpi(300,300);
  19  document.add(jpg);
  20  
  21  // top margin; dumb i know but i was in a hurry
  22  paragraph = CreateObject("java", "com.lowagie.text.Paragraph");
  23  paragraph.init(" ");
  24  for (i=0; i lt 9; i=i+1) {
  25  	document.add(paragraph);
  26  }
  27  
  28  // the fonts
  29  FontFactory 	= createobject("java", "com.lowagie.text.FontFactory");
  30  Font 			= createObject("java", "com.lowagie.text.Font");
  31  TimesLargeBI 	= Font.init(Font.TIMES_ROMAN, 14.0, Font.BOLDITALIC);
  32  TimesNormal 	= Font.init(Font.TIMES_ROMAN, 12.0);
  33  
  34  // all the text
  35  paragraph 		= CreateObject("java", "com.lowagie.text.Paragraph");
  36  
  37  paragraph.init("Hello World!", TimesLargeBI);
  38  paragraph.setIndentationLeft(indentation_left);
  39  paragraph.setIndentationRight(indentation_right);
  40  document.add(paragraph);
  41  
  42  paragraph.init("#dateFormat(now(), 'long')#", TimesNormal);
  43  paragraph.setIndentationLeft(indentation_left);
  44  paragraph.setIndentationRight(indentation_right);
  45  document.add(paragraph);
  46  
  47  document.close();
  48  </cfscript> 

Coldfusion Whois component

A whois cfc for querying a whois client.

whois.cfc
   1  
   2  <cfcomponent name="WhoIs" output="true">
   3  
   4     <!--- the host to connect to --->
   5     <cfset variables.server = "">
   6     <!--- port on server to connect to --->
   7     <cfset variables.port = "">
   8     <!--- the whois query to run --->
   9     <cfset variables.whoisQuery = "">
  10     <!--- socket timeout in seconds --->
  11     <cfset variables.connTimeout = 0>
  12     <!--- the result of the server response --->
  13     <cfset variables.response = "">
  14  
  15     <cffunction name="init" access="public" returntype="WhoIs" hint="">
  16        <cfargument name="server" type="string" default="whois.geektools.com"/>
  17        <cfargument name="port" type="numeric" default="43"/>
  18        <cfargument name="connTimeout" type="numeric" default="30" hint="Socket timeout in seconds"/>
  19        
  20        <cfscript>
  21           variables.server = arguments.server;
  22           variables.port = JavaCast("int",arguments.port);
  23           variables.connTimeout = (arguments.connTimeout * 1000);
  24           
  25           //create before use          variables.connection = createObject("java","java.net.Socket").init(variables.server,variables.port);
  26           
  27           //dump(variables.connection,true);       </cfscript>
  28        
  29        <cfreturn this>
  30     </cffunction>
  31     
  32     <cffunction name="lookup" access="public" returntype="any" hint="">
  33        <cfargument name="whoisQuery" type="string" required="true"/>
  34        <cfset var line = "">
  35        <cfset var eof = false>
  36        
  37        <cfscript>
  38           variables.whoisQuery = arguments.whoisQuery;
  39           variables.connection.setSoTimeout(JavaCast("int",variables.connTimeout));
  40           out = createObject("java","java.io.PrintStream").init(variables.connection.getOutputStream());
  41           //send the query to the server          out.println(variables.whoisQuery);
  42           variables.response = variables.response & "Connection made to: " & "[" & variables.server & ":" & variables.port & "]";
  43           
  44           instream = createObject("java","java.io.BufferedReader").init(createObject("java","java.io.InputStreamReader").init(variables.connection.getInputStream() ));
  45           
  46           
  47           try {
  48              //output results             while(not eof) {
  49                 line = instream.readLine();
  50                 variables.response = variables.response & line & "#chr(10)#";
  51              }
  52           }   
  53           //coldfusion cant check for null so check for an UndefinedVariableException          catch(coldfusion.runtime.UndefinedVariableException exception) {
  54                 eof = true;
  55           }
  56           catch(Any e){
  57              writeOutput("Catch All Exception: " & e);
  58           }
  59           out.close();
  60           instream.close();
  61           variables.connection.close();
  62        </cfscript>
  63        
  64        <cfreturn variables.response>
  65        
  66     </cffunction>
  67     
  68     
  69     <cffunction name="dump" access="public" returntype="void" output="true" hint="">
  70        <cfargument name="var" type="any" required="true"/>
  71        <cfargument name="abort" type="boolean" default="false"/>
  72        
  73        <cfdump var="#arguments.var#" label="#arguments.var#">
  74        <cfif arguments.abort><cfabort></cfif>
  75     </cffunction>
  76     
  77  
  78     
  79  </cfcomponent>


index.cfm
   1  
   2  <cfscript>
   3     wi = createObject("component","WhoIs").init("whois.geektools.com",43,10);
   4     results = wi.lookup("google.com");
   5     writeOutput(htmlCodeFormat(results));
   6  </cfscript>

Simple Cold Fusion Script That Generates HTML Select List Combo

   1  
   2  
   3  <!-- ## Simple Cold Fusion Script That Generates HTML Select List Combo ## -->
   4  <!-- ## That Allows User To Enter Date Of Birth ## -->
   5  <!-- ## Uploaded by James Cullin (james.cullin@humber.ca) ## -->
   6  <!-- ## http://jamescullin.com ## -->
   7  
   8  <cfoutput>
   9  
  10  <select>
  11  	<option> - Month - </option>
  12  	<cfloop index="m" list="January, February, March, April, May, June, July, August, September, October, November, December" delimiters=",">
  13  	<option value="#m#">#m#</option>
  14  	</cfloop>
  15  </select>
  16  
  17  <select>
  18  	<option> - Day - </option>
  19  	<cfloop index="d" from="1" to="31" step="1">
  20  	<option value="#d#">#d#</option>
  21  	</cfloop>
  22  </select>
  23  
  24  <select>
  25  	<option> - Year - </option>
  26  	<cfloop index="y" from="2004" to="1900" step="-1">
  27  	<option value="#y#">#y#</option>
  28  	</cfloop>
  29  </select>
  30  
  31  </cfoutput>
  32  

Alternate table cell background color

   1  
   2  BGCOLOR="###IIF(currentrow MOD 2, DE ('FFFFCC'), DE ('FFFFFF'))#"
« Newer Snippets
Older Snippets »
Showing 1-10 of 15 total  RSS