<cfcomponent name="DirectoryRecurse"
hint="Object to recurse large directories for a list of files.">
<!--- properties --->
<cfproperty name="BatFileName" type="string"
hint="Name of the Bat File used to recurse the directories">
<cfproperty name="BatFileDir" type="string"
hint="Directory the bat file is in">
<!--- constructor --->
<cfscript>
variables.BatFileName = "RecurseDirectory.bat";
variables.BatFileDir = GetTempDirectory();
</cfscript>
<!---
Init
Initializes an object with specific variables
--->
<cffunction name="init" returntype="void" output="No">
</cffunction>
<!---
ListMostRecentFiles
--->
<cffunction name="ListMostRecentFiles" returntype="query" output="false">
<cfargument name="Directory" type="string" required="yes">
<cfargument name="FileCount" type="numeric" Default="15">
<cfset var qFileList = ListFiles( arguments.Directory ) />
<cfset var qMostRecent = "" />
<cfquery name="qMostRecent" maxrows="15" dbtype="query">
SELECT *
FROM qFileList
ORDER BY LastUpdated Desc
</cfquery>
<cfreturn qMostRecent />
</cffunction>
<!---
ListFiles
--->
<cffunction name="ListFiles" returntype="query" output="No">
<cfargument name="Directory" type="string" required="yes">
<cfset var qFileList = queryNew("LastUpdated,Size,Directory,FileName") />
<cfset var sDirList = RunBatFile(arguments.Directory) />
<cfset var aAllDirs = sDirList.split("Directory of")>
<cfset var nDirCount = arrayLen( aAllDirs ) />
<cfset var aCurrentDir = "" />
<cfset var nDirKey = "" />
<cfset var sDirName = "" />
<cfset var nFileCount = "" />
<cfset var nFileKey = "" />
<cfset var sCurrentLine = "" />
<!--- loop over the directories --->
<cfloop from="1" to="#nDirCount#" index="nDirKey">
<!--- and get the second dir by spliting the text with returns--->
<cfset aCurrentDir = aAllDirs[nDirKey].split( chr(10) )>
<!--- first line has the dir name --->
<cfset sDirName = trim( aCurrentDir[1] )>
<cfif Right( sDirName,1 ) Neq "\">
<cfset sDirName = sDirName & "\">
</cfif>
<cfset nFileCount = arrayLen( aCurrentDir ) />
<!--- loop over the files in this directory --->
<cfloop from="2" to="#nFileCount#" index="nFileKey">
<cfset sCurrentLine = trim( aCurrentDir[nFileKey] ) />
<cfif isDate( left( sCurrentLine,10 ) )
and Not mid( sCurrentLine,25,5) EQ "<DIR>">
<!--- and parse the file line to get its information --->
<cfscript>
queryAddRow(qFileList);
querySetCell(qFileList,"LastUpdated",ParseDateTime(left(
sCurrentLine,18) ) );
querySetCell(qFileList,"Size",val( replace( trim( mid(
sCurrentLine,19,20) ),",","","all") ) );
querySetCell(qFileList,"FileName",mid( sCurrentLine,40,len(
sCurrentLine ) - 39));
querySetCell(qFileList,"Directory",sDirName);
</cfscript>
</cfif>
</cfloop>
</cfloop>
<!--- and return the query --->
<cfreturn qFileList />
</cffunction>
<!---
CreateBatFile()
--->
<cffunction name="CreateBatFile" returntype="void" output="false">
<cfif not BatFileExists()>
<cffile action="WRITE" file="#GetBatFileLocation()#"
output="dir %1 /S"
attributes="Normal"
addnewline="No">
</cfif>
</cffunction>
<!---
BatFileExists()
--->
<cffunction name="BatFileExists" returntype="boolean" output="false">
<cfreturn fileExists( GetBatFileLocation() ) />
</cffunction>
<!---
GetBatFileLocation()
--->
<cffunction name="GetBatFileLocation" returntype="string" output="false">
<cfreturn variables.BatFileDir & "\" & variables.BatFileName />
</cffunction>
<!---
RunBatFile()
--->
<cffunction name="RunBatFile" returntype="string" output="false">
<cfargument name="Directory" type="string" required="yes">
<cfset var sDirList = "" />
<!--- if the bat fiel doesnt exist, create it --->
<cfif not BatFileExists()>
<cfset CreateBatFile()>
</cfif>
<!--- and run it --->
<cfexecute name="#GetBatFileLocation()#"
arguments="#arguments.Directory#"
variable="sDirList"
timeOut="30"></cfexecute>
<!--- and return its results --->
<cfreturn sDirList />
</cffunction>
</cfcomponent>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Sams Teach Yourself Regular Expressions in 10 Minutes by Ben Forta
http://www.houseoffusion.com/banners/view.cfm?bannerid=40
Message: http://www.houseoffusion.com/lists.cfm/link=i:5:160266
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/5
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:5
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.5
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54