My Projects
Search Blog

Categories
Archives
Useful Links
Photo Albums
RSS

Powered by
BlogCFM v1.15

Vivio Technologies CFML Hosting
15 June 2008
Creating IIS Virtual Directories with Coldfusion 8 and .NET integr

Ever had a need to automate the creation of virtual directories in IIS?  Coldfusion 8 can do this quite well using the System.DirectoryServices.DirectoryEntry class in the .NET framework.

After a whole lot of trial and error, this is what I came up with and it seems to work quite nicely:

 

<!--- get the server ID for the specific web site
    from IIS.  The default web site is usually "1"
    --->
<cfset serverid="2017647531">

    <!---
        //  metabasePath is of the form "IIS://<servername>/<service>/<siteID>/Root[/<vdir>]"
        //    for example "IIS://localhost/W3SVC/1/Root"
    --->
<cfset metabasePath = "IIS://localhost/W3SVC/#serverid#/Root">

<!--- name of the virtual directory --->
<cfset vdirName = "vdir001">

<!--- physical path for this virtual directory --->
<cfset physicalPath = "D:\Inetpub\ricktest\realdir">

<!--- full path to the System.DirectoryServices.dll --->
<cfset dllPath = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.DirectoryServices.dll">

<!--- instantiate a DirectoryEntry .NET object --->
<cfobject
    type=".net"
    class="System.DirectoryServices.DirectoryEntry"
    assembly="#dllPath#"
    name="site">

<!--- initialize the object with the metabase path --->   
<cfset site.init(metabasePath)>

<!--- you need the classname for later --->
<cfset className = site.Get_SchemaClassName()>

<cfif reFind("VirtualDir$",className) gt 0>
    <!--- Get the children so we can add a new child --->
    <cfset vdirs = site.Get_Children()>
    <cftry>
        <!--- add the new virtual directory --->
        <cfset newVDir = vdirs.Add(vDirName, className)>
        <cfcatch type="any">
            <cfoutput><h3>Failed to create #vDirName#</h3></cfoutput>
            <cfif cfcatch.ToString() contains "already exists">
                <p>Another directory with that name already exists.</p>
            <cfelse>
                <cfoutput><p>#cfcatch.ToString()#</p></cfoutput>
            </cfif>
            <cfabort>
        </cfcatch>
    </cftry>
    <Cftry>
        <!--- set the PAth property to the physical directory path --->
        <cfset newVDir.Get_Properties().Get_Item("Path").Set_Value(physicalPath)>
        <!--- commit the changes --->
        <cfset newVDir.CommitChanges()>

        <cfcatch type="any">
            <cfdump var="#cfcatch.ToString()#">
            <cfdump var="#cfcatch#">
            <Cfabort>
        </cfcatch>
    </cftry>
</cfif>


 

 

Posted by rickroot at 9:08 AM | Link | 5 comments
06 April 2007
Paging Through Record Sets without URL variables

One of my clients recently asked me to avoid the use of URL variables when paging through record sets.  He wanted the "clean" URL look.  So I converted some code I'd written to the following.

The following code uses a hidden form and some javascript to achieve the first/next/previous/last functionality.  It also displays "Showing records X - Y of Z total records"

<cfsetting enablecfoutputonly="yes">
<cfset perPage = 100>
<!--- set defaults --->
<cfparam name="session.lastStartRow" default="1">
<cfparam name="startRow" default="#session.lastStartRow#">
<cfif startRow gt qry.recordCount or startrow lt 1>
     <cfset startRow = 1>
</cfif>
<!--- calculate next and previous starting position --->
<cfset nextStartRow = startRow + perPage>
<cfset prevStartRow = startRow - perPage>
<!--- calc the last row for display purposes --->
<cfset endRow = startRow + perPage - 1>
<cfif endRow gt qry.recordCount>
     <cfset endRow = qry.recordCount>
</cfif>
<!--- save in case we leave the page and come back --->
<cfset session.lastStartRow = startRow>
<!--- the index of the first row of the LAST page is lastPageStart --->
<cfset lastPageStart = qry.recordCount - (qry.recordCount MOD perPage)>
<!--- output the hidden form used for navigation --->
<cfoutput><form name="frmPage" action="DataTable.cfm" method="post">
<input type="hidden" name="startRow" value="1" id="startRow">
</form>
<script language="Javascript">
function goPage(startRow) {
     // update the form and submit
     document.frmPage.startRow.value = startRow;
     document.frmPage.submit();
}
</script>
</cfoutput>

<!---
     Save the navigation to a variable so we can
     output it above AND Below the content easily.
--->
<cfsavecontent variable="pager"><cfoutput>
<p>
Showing records #startRow#-#endRow# of #qry.recordCount#.
<br/>
<a href="javascript:goPage(1);">First</a> &nbsp;
<cfif startRow gt 1><a href="javascript:goPage(#prevStartRow#);">Previous</a><cfelse>Previous</cfif> &nbsp;
<cfif endRow lt qry.recordCount><a href="javascript:goPage(#nextStartRow#);">Next</a><cfelse>Next</cfif> &nbsp;
<a href="javascript:goPage(#lastPageStart#)">Last</a>
</p></cfoutput></cfsavecontent>
<cfsetting enablecfoutputonly="yes">


<cfoutput>#pager#</cfoutput>

<cfoutput query="qry" maxrows="#perPage#" startrow="#startRow#">
     <!--- do stuff --->
</cfoutput>

<cfoutput>#pager#</cfoutput>


 

 

Posted by rickroot at 5:41 PM | Link | 9 comments
06 February 2007
Determining Zip Code Proximity
How to find results in your database based on their proximity to a specified zip code

So I got a request from one of our development officers the other day.  They wanted to be able to perform prospect reports based on zip code, or a range of zip codes.  So I started investigating, and found numerous resources thanks to the members of the coldfusion community on the cf-talk mailing list.

First, someone pointed me to www.teamreadline.com, a company that sells zip code databases for $5.  The database contains city, state, state abbreviation, zip code, latitude, and longitude.  I forked out the $5 and downloaded the data, and then imported it into SQL Server.

Posted by rickroot at 12:51 PM | Link | 18 comments
18 December 2006
UDF to generate File Drops in CSV, Excel, and Tab delimited formats
At a reader's request, I'm posting a UDF I wrote to generate file drops from queries.  The UDF takes as its arguments a query, a format (EXCEL, CSV, or TAB), a list of column names, and a list of column labels.  It returns a text string that can be used to either generate a file on the server or can be returned with cfcontent with the appropriate mime type...
Posted by rickroot at 9:41 AM | Link | 6 comments
28 November 2006
Using coldfusion to strip some or all HTML tags from a string

Years ago (ie, 1999), I wrote a custom tag called CF_TAGSTRIPPER that would strip HTML tags from a source string, optionally preserving specific tags.

A few years ago, I converted it to a UDF and I've used it in a variety of my applications to strip out all but a few tags - like if I wanted to allow users to use bold and italic tags.

Recently, I've had a desire to strip out only certain tags, so I modified my tagStripper function to perform this task as well.  Here's the code.  Please let me know if you see anything wrong with it!

click the link to continue reading....

Posted by rickroot at 3:45 PM | Link | 14 comments