Task scheduler for cleaning up temporary uploaded files

You can share your Lua Scripts with everybody here.

Task scheduler for cleaning up temporary uploaded files

Postby FTP » Wed Apr 21, 2010 1:02 pm

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


Image
FTP
Site Admin
 
posts 1202
 
joined Tue Sep 29, 2009 6:09 am

Re: Task scheduler for cleaning up temporary uploaded files

Postby cyberbeak » Fri Mar 18, 2011 4:50 am

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.
cyberbeak
 
posts 2
 
joined Fri Mar 18, 2011 4:42 am

Re: Task scheduler for cleaning up temporary uploaded files

Postby FTP » Fri Mar 18, 2011 12:00 pm

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
FTP
Site Admin
 
posts 1202
 
joined Tue Sep 29, 2009 6:09 am

Re: Task scheduler for cleaning up temporary uploaded files

Postby cyberbeak » Fri Mar 18, 2011 2:34 pm

Thank you so much. You are a lifesaver!!!!
cyberbeak
 
posts 2
 
joined Fri Mar 18, 2011 4:42 am

Re: Task scheduler for cleaning up temporary uploaded files

Postby matt631 » Tue Nov 01, 2011 10:25 am

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?
matt631
 
posts 3
 
joined Fri Aug 12, 2011 11:17 am

Re: Task scheduler for cleaning up temporary uploaded files

Postby priit » Fri Mar 23, 2012 12:12 pm

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
priit
 
posts 3
 
joined Fri Mar 23, 2012 11:48 am

Re: Task scheduler for cleaning up temporary uploaded files

Postby FTP » Mon Mar 26, 2012 10:41 am

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
FTP
Site Admin
 
posts 1202
 
joined Tue Sep 29, 2009 6:09 am

Re: Task scheduler for cleaning up temporary uploaded files

Postby priit » Mon Mar 26, 2012 2:14 pm

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

Postby priit » Mon Apr 16, 2012 8:56 am

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
priit
 
posts 3
 
joined Fri Mar 23, 2012 11:48 am

Re: Task scheduler for cleaning up temporary uploaded files

Postby eneuhar » Wed Sep 05, 2012 7:34 pm

Was anyone able to modify this script so that it deletes the subdirectories as well?
eneuhar
 
posts 6
 
joined Tue Aug 07, 2012 3:08 pm

Next

Return to Lua scripts

Who is online

Users browsing this forum: No registered users and 1 guest