[thelist] TIP: CF to move old log files

Erik Mattheis gozz at gozz.com
Sat Apr 20 23:53:00 CDT 2002


Someone posted something about archiving log files and I became overridden with guilt. The following script moved 5601 old log files for me in less than a minute.

<tip type="Moving old IIS log files with ColdFusion" author="Erik Mattheis">

<!--- set variables --->
<cfset num_actions = 0>

<!--- set the oldest_day to a string representing the oldest logfile you do _not_ want to move --->
<cfset oldest_date=DateFormat(Now(),"YYMM01")>

<!--- change these to point to your LogFile and log archives directories --->
<cfset log_file_dir="C:\WINNT\system32\LogFiles">
<cfset log_archive_dir="C:\log archives">

<!--- / set variables - there is still one thing below you might need to change --->

<!--- get the names of folders in the LogFile directory and loop through them --->
<cfdirectory action="list" directory="#variables.log_file_dir#" name="dirs">
<cfloop query="dirs">

	<!--- make sure we're working with something we want to! --->
	<cfif type IS "dir" AND name IS NOT "." AND name IS NOT "..">

		<!--- get a list of the log file names and loop through them --->
		<cfset this_dir = name>
		<cfdirectory action="list" directory="#variables.log_file_dir#\#variables.this_dir#" name="result">
		<cfloop query="result">

			<!--- you might need to change this:  set this_date to a string in the same format as oldest_date representing the date of this log file --->
			<cfset this_date = ReplaceList(name,"ex,.log","")>

			<!--- find out if we want to move this file --->
			<cfif type IS "file" AND variables.this_date LT variables.oldest_date>

				<!--- check to see if the directory we want to move this to exists, if it doesn't, create it --->
				<cfif NOT DirectoryExists(#variables.log_archive_dir# & "\" & #variables.this_dir#)>
					<cfdirectory action="create" directory="#variables.log_archive_dir#\#variables.this_dir#">
				</cfif>

				<!--- archive that log file! --->
				<cffile action="move" source="#variables.log_file_dir#\#variables.this_dir#\#name#" destination="#log_archive_dir#\#this_dir#\#name#">
				<cfset num_actions = variables.num_actions + 1>
			</cfif>
		</cfloop>
	</cfif>
</cfloop>

<!--- display results --->
<cfoutput>#variables.num_actions#</cfoutput> log files were moved

</tip>
--

__________________________________________
- Erik Mattheis

(612) 377 2272
http://goZz.com/

__________________________________________



More information about the thelist mailing list