May 10
The Windows Sysinternals Suite by Mark Russinovich is a great set of tools. I use them all the time, executing them from ColdFusion and parsing the results. Here’s a quick example of using the PsList.exe tool to inspect the processes on your server, or another machine on your network.
<cfexecute
name = "#absolutePath#\PsList.exe"
arguments ="\\#serverName# #processname# -u #accountUsername# -p #accountPassword# -accepteula"
variable ="pResults"
timeout ="5"
/>
<pre>#pResults#</pre>
A few things to keep in mind: 1) you need to use the –accepteula switch the first time you use the command, or it will time out waiting for a response. 2) getting the permissions right to examine remote machines can be tricky. I can’t offer you any advice on that, you’ll just have to beat your head against it until it works. :)
Good Luck!
May 9
So back on March 5th I wrote a post about Using Cold Fusion to Count the Files in a Directory. Recently a component I wrote that keeps an eye on mail server processes choked to death on the large number of files it had to report on. Another component in the mail server failed (one vendor updated their component without telling another vendor about it, yada yada) and the mail queue backed up. With scores of thousands of files in the directory, I found CFDIRECTORY to be kind of slow.
So. I wrote a new function to that uses the FileSystemObject to look at a single directory, and I think it might work better. I pointed it at a photo gallery that had a large number of files and it was very responsive. Here’s hoping, right? Regardless, it’s another way of counting files in a folder; I’m sure I’ll need this again someday, and that’s what this blog is all about, right? Maybe you’ll find it handy, too.
Here’s the idea:
<cffunction name="fileCountFSO" returnType="numeric" output="false">
<cfargument name="folderLoc" required="no">
<cfparam name="folderLoc" default="">
<cflock
timeout ="30"
throwontimeout ="No"
name ="#CreateUUID()#"
type ="EXCLUSIVE"
>
<cftry>
<cfobject
type="COM"
name ="FSO"
class ="Scripting.FileSystemObject"
action ="CONNECT"
/>
<cfcatch>
<cfobject
type="COM"
name="FSO"
class="Scripting.FileSystemObject"
action="CREATE"
/>
</cfcatch>
</cftry>
</cflock>
<cftry>
<cfscript>
objFolderFiles = FSO.getFolder(folderLoc).Files;
objFolderCount = objFolderFiles.Count;
</cfscript>
<cfcatch>
<cfset objFolderCount = 0>
</cfcatch>
</cftry>
<cfreturn #objFolderCount#>
</cffunction>
Apr 8
Having your text form input autosuggest values for you from your database couldn’t be easier in ColdFusion 9. There are really only two things you have to worry about: 1) the autosuggest value in your CFINPUT tag, and 2) the component function that performs the query for you, and then returns the result in an array.
Here is an example of a field that auto-suggests country names for you.
Here’s the CFINPUT:
<cfinput
type="text"
name="mailingCountry"
value="#mailingCountry#"
autosuggest="cfc:directoryName.componentName.suggestCountry({cfautosuggestvalue})"
="width: 275px; background-color: ##e2e2f2;"
/>
In the database is a table filled with country names. Here’s the function to access it:
<cffunction name="suggestCountry" returnType="Array" access="remote" output="false">
<cfargument name="suggestValue" required = "true">
<cfquery
datasource="#request.thisDSN#"
name="provideRecords"
>
SELECT DISTINCT countryName
FROM lookup_country
WHERE countryName LIKE <cfqueryparam value="#suggestValue#%" cfsqltype="cf_sql_varchar">
ORDER BY countryName
</cfquery>
<!--- Convert the query to an array. --->
<cfset var provideArray = ArrayNew(1)>
<cfloop query="provideRecords">
<cfset arrayAppend(provideArray, countryName)>
</cfloop>
<cfreturn #provideArray#>
</cffunction>
That’s it! ColdFusion 9 writes the JavaScript/AJAX that queries the server and writes the results back to the input field. Pretty easy, huh?
Mar 29
This is really simple, but I do it all the time: looping through a query because you can’t nest query-driven <cfoutput>. The pattern is queryname.fieldname[x], and that’s all you really have to remember.
Here’s an example:
<cfoutput query=”myFirstQuery”>
#fieldFromMyFirstQuery#<br />
[[ Insert Second Query Here ]]
<cfif #mySecondQuery.recordCount# GT 0>
<cfloop from=”1” to=”mySecondQuery.recordCount#” index=”thisRecord”>
#mySecondQuery.fieldFromMySecondQuery[thisRecord]#<br />
</cfloop>
</cfoutput>
Mar 17
This is pretty trivial, and I know that ORM probably handles everything better these CF9 days (I haven't really jumped into that ORM thing yet) but I make a CFC for just about every table I work with and sometimes I just want to know how many of a particular thing there is in the table. Here's a function I include in all of my CFCs for that.
<cffunction name="countRecords" returnType="Numeric" output="false">
<cfargument name="sqlWhere" required="no">
<cfparam name="sqlWhere" default="thingID > 0">
<cfquery
datasource ="#request.thisDSN#"
name ="countAll"
>
SELECT COUNT(thingID) as ttlRecords
FROM things
WHERE #PreserveSingleQuotes(sqlWhere)#
</cfquery>
<cfset ttlRecords = #countAll.ttlRecords[1]#>
<cfreturn #ttlRecords#>
</cffunction>
That lets me do a quick invoke that wastes little time or bits and just gives me a number back. I can even pass a little SQL along with the request to get counts on specific things.
Recent Comments