IPB

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Delete Files Older Than x Hours, Scans Folder for files older than x hours and deletes them
Rating 5 V
kprestage
post Aug 7 2007, 04:38 PM
Post #1


Power user
***

Group: Power users
Posts: 192
Joined: 17-May 07
Member No.: 958



Not sure if this will be of any use to anyone, but here is a quick script I wrote to keep a processing folder of ours clean. It takes two arguments. The 1st is the folder to scan and the second is the number of hours old any valid files in the directory should be. This has not been tested too much, so use at your own risk!

CODE

----------------------------------------------------------------------------------------------------------
-- Name: DeleteFilesOlderThan.lua
-- Version: 1.0
-- Date: 2007-08-07
-- Description: Deletes files in the specified folder that are older than the specified number of hours
-- Arguments:
-- 1) Path of folder to check (in quotes)
-- 2) Maximum hours old files should be (Files older than this value will be deleted)
----------------------------------------------------------------------------------------------------------



path = GetArgument(0);
HoursOld = GetArgument(1);

file = TLuaFile();



--get the file list
sResult = file:GetFileList(path,"*.*")
--a temporary variable to hold the current file name.
sfilename = "";
--loop through results and parse file names
iLen = string.len(sResult);
iFileCount =0
iDeleted = 0;
for count = 0, iLen do
cCurrentChar = string.byte(sResult,count);

--if the current char is CR then we reached the end of a name
if cCurrentChar == 10 then
if sfilename ~= null then
--create the timespan to check
TimeSpan = TLuaDateTime();
TimeSpan:CreateSpan(HoursOld,0,0);
--set the filetimes
FileTime1 = TLuaDateTime();
FileTime2 = file:GetFileCreatedTime(sfilename );

--set the maximum age to allow for a file
FileTime1:Sub(TimeSpan);

--if the file is too old, we want to delete it
if FileTime2:LessOrEqual(FileTime1) then
del = TLuaFile();
iResult = del:DeleteFile(sfilename);
if iResult ~= 0 then
iDeleted = iDeleted + 1;
end
--print("File time: "..FileTime2:GetDate().." "..FileTime2:GetTime() ..sfilename);
--print("Check time: "..FileTime1:GetDate().." "..FileTime1:GetTime() .. sfilename);

end
sfilename = "";
end
--add the character to the current filename
else
if cCurrentChar ~= nil then
sfilename = sfilename .. string.char(cCurrentChar);
end

end
end
SetExitStatus(iDeleted .. " files deleted",true);
Go to the top of the page
 
+Quote Post
RA
post Aug 8 2007, 11:54 AM
Post #2


INM wizard
**********

Group: Root Admin
Posts: 2,282
Joined: 24-August 04
From: Intellipool AB, Härnösand, Sweden
Member No.: 3



Very nice work there Kevin, I'm pretty sure this is a very handy script for many sys admins out there.


--------------------
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

 



RSS Lo-Fi Version Time is now: 31st July 2010 - 05:53 PM