Task scheduler for cleaning up temporary uploaded files

You can share your Lua Scripts with everybody here.
FTP
Site Admin
Posts: 1918
Joined: Tue Sep 29, 2009 6:09 am

Task scheduler for cleaning up temporary uploaded files

Post by FTP »

-- Description: task scheduler for cleaning up temporary uploaded files
-- Author: Luke
-- Date: 2010-04-10

Code: Select all

local root_dir = "c:/temp"	--physical path for storing temporary uploaded files
local timeout = 3600*3	--clean up uploaded files older than 3 hours
local removefiles = {}
local now = os.time()
if c_GetFileDir(root_dir) ~= nil then
	for isdir,filename in c_GetFileDir(root_dir) do
		if isdir == false and filename ~= nil and filename ~= "." and filename ~= ".."  then
			local filepath = root_dir.."/"..filename
			if c_FileExist(filepath) then
				if c_GetFileTime(filepath) + timeout < now then
					table.insert(removefiles, filename)
				end
			end
		end
	end
end

for _,filename in ipairs(removefiles) do
	c_RemoveFileDir(root_dir.."/"..filename)
end
Image
cyberbeak
Posts: 2
Joined: Fri Mar 18, 2011 4:42 am

Re: Task scheduler for cleaning up temporary uploaded files

Post by cyberbeak »

Hello,

I wonder if you can help me, or point me in the right direction. I have been trying for several days to create a lua script to run in the Task Scheduler and:

1. Recognize when a file has been uploaded to a specific directory
2. email that file to my email address
3. Erase the file

I have written several batch files that accomplish this, but none of them will run from the Task Scheduler Program Execute Window (I have tried both bat and exe files)

The only thing even slightly unusual about the script is that, since the uploaded files are coming from a video camera which is motion-activated, the files are named Lobby-xxx-xxx-xxx where the x's are day-date-time stamps relating to the event. That means that the script can never know in advance what the exact name of the file is that has been uploaded.

Any advice or reference that anyone can provide would be greatly appreciated.

Thank you in advance.
FTP
Site Admin
Posts: 1918
Joined: Tue Sep 29, 2009 6:09 am

Re: Task scheduler for cleaning up temporary uploaded files

Post by FTP »

I think you just need to add an event script for event "OnFileUploaded":

Code: Select all

local filepath = "%PathName"
if string.find(filepath,"d:/ftp/test") then
	c_SendMail("test@xxxx.com","a file has been uploaded","%Name uploaded a file '%FileName' ",filepath,"SMTPconfig")
	c_RemoveFileDir(filepath)
end
cyberbeak
Posts: 2
Joined: Fri Mar 18, 2011 4:42 am

Re: Task scheduler for cleaning up temporary uploaded files

Post by cyberbeak »

Thank you so much. You are a lifesaver!!!!
matt631
Posts: 4
Joined: Fri Aug 12, 2011 11:17 am

Re: Task scheduler for cleaning up temporary uploaded files

Post by matt631 »

We have a folder structure which looks like..

E:\support\users\<CustomerUsernameHere>\<CustomerUsernameHere>\
E:\support\users\<CustomerUsernameHere>\incoming\

We would like to flush the top Dir out every 60 days for all customers, but leave the bottom Dir untouched. Could this script be modified to achieve such a result?

Presumable we would have to run a loop passing in a different username as a variable to the root clean up dir every-time. It sounds way beyond my knowledge does anyone have anything similar they are willing to share?
priit
Posts: 3
Joined: Fri Mar 23, 2012 11:48 am

Re: Task scheduler for cleaning up temporary uploaded files

Post by priit »

Hi!

First, awesome code - really useful!
Secondly, could you modify it so it would delete subdirs aswell?

[quote="FTP"]-- Description: hourly task scheduler for cleaning up temporary uploaded files
-- Author: Luke
-- Date: 2010-04-10

Code: Select all

local root_dir = "c:/temp"	--physical path for storing temporary uploaded files
local timeout = 3600*3	--clean up uploaded files older than 3 hours
local removefiles = {}
local now = os.time()
if c_GetFileDir(root_dir) ~= nil then
	for isdir,filename in c_GetFileDir(root_dir) do
		if isdir == false and filename ~= nil and filename ~= "." and filename ~= ".."  then
			local filepath = root_dir.."/"..filename
			if c_FileExist(filepath) then
				if c_GetFileTime(filepath) + timeout < now then
					table.insert(removefiles, filename)
				end
			end
		end
	end
end

for _,filename in ipairs(removefiles) do
	c_RemoveFileDir(root_dir.."/"..filename)
end
FTP
Site Admin
Posts: 1918
Joined: Tue Sep 29, 2009 6:09 am

Re: Task scheduler for cleaning up temporary uploaded files

Post by FTP »

Code: Select all

local root_dir = "D:/ftptest"   --physical path for storing temporary uploaded files
local timeout = 3600*24*3   --clean up uploaded files older than 3 hours
local removefiles = {}
local now = os.time()

function checkfile(nowdir)
	if c_GetFileDir(nowdir) ~= nil then
	   for isdir,filename in c_GetFileDir(nowdir) do
	      if isdir == false then
		 if filename ~= nil and filename ~= "." and filename ~= ".."  then
		     local filepath = nowdir.."/"..filename
		     if c_FileExist(filepath) then
		        if c_GetFileTime(filepath) + timeout < now then
		           table.insert(removefiles, nowdir.."/"..filename)
		        end
		     end
		 end
	      else
	          checkfile(nowdir.."/"..filename)
	      end
	   end
	end
end

checkfile(root_dir)
 
for _,filename in ipairs(removefiles) do
   c_RemoveFileDir(filename)
end
priit
Posts: 3
Joined: Fri Mar 23, 2012 11:48 am

Re: Task scheduler for cleaning up temporary uploaded files

Post by priit »

Thanks! It successfully deletes files from subdirs.
But I was actually more interested in the possibility of deleting the subdirs too.
Is that possible?

General idea is to have a dir where all files and subdirs are kept only for a predetermined time and the automatically deleted by a script.

Or maybe there is another logical solution, rather than a script?
FTP wrote:

Code: Select all

local root_dir = "D:/ftptest"   --physical path for storing temporary uploaded files
local timeout = 3600*24*3   --clean up uploaded files older than 3 hours
local removefiles = {}
local now = os.time()

function checkfile(nowdir)
	if c_GetFileDir(nowdir) ~= nil then
	   for isdir,filename in c_GetFileDir(nowdir) do
	      if isdir == false then
		 if filename ~= nil and filename ~= "." and filename ~= ".."  then
		     local filepath = nowdir.."/"..filename
		     if c_FileExist(filepath) then
		        if c_GetFileTime(filepath) + timeout < now then
		           table.insert(removefiles, nowdir.."/"..filename)
		        end
		     end
		 end
	      else
	          checkfile(nowdir.."/"..filename)
	      end
	   end
	end
end

checkfile(root_dir)
 
for _,filename in ipairs(removefiles) do
   c_RemoveFileDir(filename)
end
priit
Posts: 3
Joined: Fri Mar 23, 2012 11:48 am

Re: Task scheduler for cleaning up temporary uploaded files

Post by priit »

Hi!

Could somebody modify the script so it would delete all the subdirs aswell?
priit wrote:Thanks! It successfully deletes files from subdirs.
But I was actually more interested in the possibility of deleting the subdirs too.
Is that possible?

General idea is to have a dir where all files and subdirs are kept only for a predetermined time and the automatically deleted by a script.

Or maybe there is another logical solution, rather than a script?
FTP wrote:

Code: Select all

local root_dir = "D:/ftptest"   --physical path for storing temporary uploaded files
local timeout = 3600*24*3   --clean up uploaded files older than 3 hours
local removefiles = {}
local now = os.time()

function checkfile(nowdir)
	if c_GetFileDir(nowdir) ~= nil then
	   for isdir,filename in c_GetFileDir(nowdir) do
	      if isdir == false then
		 if filename ~= nil and filename ~= "." and filename ~= ".."  then
		     local filepath = nowdir.."/"..filename
		     if c_FileExist(filepath) then
		        if c_GetFileTime(filepath) + timeout < now then
		           table.insert(removefiles, nowdir.."/"..filename)
		        end
		     end
		 end
	      else
	          checkfile(nowdir.."/"..filename)
	      end
	   end
	end
end

checkfile(root_dir)
 
for _,filename in ipairs(removefiles) do
   c_RemoveFileDir(filename)
end
eneuhar
Posts: 6
Joined: Tue Aug 07, 2012 3:08 pm

Re: Task scheduler for cleaning up temporary uploaded files

Post by eneuhar »

Was anyone able to modify this script so that it deletes the subdirectories as well?
Post Reply