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);