Page 1 of 3

Task scheduler for cleaning up temporary uploaded files

Posted: Wed Apr 21, 2010 1:02 pm
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

Re: Task scheduler for cleaning up temporary uploaded files

Posted: Fri Mar 18, 2011 4:50 am
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.

Re: Task scheduler for cleaning up temporary uploaded files

Posted: Fri Mar 18, 2011 12:00 pm
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

Re: Task scheduler for cleaning up temporary uploaded files

Posted: Fri Mar 18, 2011 2:34 pm
by cyberbeak
Thank you so much. You are a lifesaver!!!!

Re: Task scheduler for cleaning up temporary uploaded files

Posted: Tue Nov 01, 2011 10:25 am
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?

Re: Task scheduler for cleaning up temporary uploaded files

Posted: Fri Mar 23, 2012 12:12 pm
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

Re: Task scheduler for cleaning up temporary uploaded files

Posted: Mon Mar 26, 2012 10:41 am
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

Re: Task scheduler for cleaning up temporary uploaded files

Posted: Mon Mar 26, 2012 2:14 pm
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

Re: Task scheduler for cleaning up temporary uploaded files

Posted: Mon Apr 16, 2012 8:56 am
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

Re: Task scheduler for cleaning up temporary uploaded files

Posted: Wed Sep 05, 2012 7:34 pm
by eneuhar
Was anyone able to modify this script so that it deletes the subdirectories as well?