Help - Search - Members - Calendar
Full Version: Delete Files Older Than x Hours
Intellipool Network Monitor - Forum > Intellipool Network Monitor > Lua scripts
kprestage
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);
RA
Very nice work there Kevin, I'm pretty sure this is a very handy script for many sys admins out there.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.