Task scheduler for cleaning up temporary uploaded files

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

Re: Task scheduler for cleaning up temporary uploaded files

Post by FTP »

eneuhar wrote:Was anyone able to modify this script so that it deletes the subdirectories as well?

Please check out the old posts carefully, we have already pasted the script for cleaning up root folder and all subfolders.
eneuhar
Posts: 6
Joined: Tue Aug 07, 2012 3:08 pm

Re: Task scheduler for cleaning up temporary uploaded files

Post by eneuhar »

I tried the latest post of script, however it seems to only delete the files within subdirectories and not the folders themselves.
Guesseyder
Posts: 2
Joined: Fri Apr 24, 2015 6:48 pm

Code modified to be called once for each sub-directory in a

Post by Guesseyder »

I am attempting to modify this script so that I can load it into the task scheduler and every day at midnight it will launch to remove files and directories that are over 14 days old. I am using the Wing FTP server for Windows on a 64 bit Windows 2012 Server. I have separate directories set up for every user, and each user only has access to their own directory, and is blocked from all others.

Since I have given every client the ability to create their own sub-directories within their own directory, it makes it so that this script needs to be modified so that it will examine each of my user's directories and the sub-directories as a separate call to the script. This must be done in order to avoid it deleting the home directories of each user, which will be older than 14 days.

One method I could get this accomplished would be to call this script once for every account, with that account's directory set as the "local root_dir" variable. I fear this will become cumbersome as I will be required to add this to the task scheduler once for every client that exists, and every new client that I add to the server. I scoured the internet for an idea on how to get the Lua script to do this for each client's directory.

Most advice that I found for Lua scripting on the internet recommends that I install the LuaFileSystem library. Is this even possible to install and use with the Lua embedded in Wing FTP Server? I assumed it was not after I made a feeble attempt, and kept searching until I found another method. In my configuration, I have all user directories as a sub-directory of D:\Files. I want to point the Lua script to D:\Files as a starting point and have it loop through this code once for every directory found within.

I want this script to delete all files and sub-directory within each user's directory that are over 14 days old, and then do the same for the next user directory. I have cobbled this script together from the code listed on these forums, and from help files. Wing FTP server indicates that it has executed as it should, but it does not do anything. I fear that the 'built-in' functions that I am calling may not be included with Wing FTP server's implementation of Lua. If this is the case, and I do need to install a library, is this possible? Below is the script I cobbled together. I have made the code presented in this forum thread into a function named "CallTheMaid" so that it can be called once for every sub-directory found off of D:\Files. Can anyone tell me what I did wrong?

Thanks in advance,
Guesseyder

Code: Select all

==========================================================================================
Begin snippet
==========================================================================================
function CallTheMaid(subdir)
    local root_dir = "D:/TEST/" .. subdir .. "/"
   --physical path for storing temporary uploaded files
    local timeout = 3600*24*14	    --delete uploaded files older than 14 Days
    local removefiles = {}	--creates an empty table
    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
end



for subdir in c_GetDir("D:/TEST/") do
 CallTheMaid(subdir)
end
==========================================================================================
End snippet
==========================================================================================
FTP
Site Admin
Posts: 2072
Joined: Tue Sep 29, 2009 6:09 am

Re: Task scheduler for cleaning up temporary uploaded files

Post by FTP »

First of all, Lua functions must be separated, not one function embedded in another function.
Guesseyder
Posts: 2
Joined: Fri Apr 24, 2015 6:48 pm

Re: Task scheduler for cleaning up temporary uploaded files

Post by Guesseyder »

Ah yes. Thanks FTP.

I have altered the code and have broken out the functions, tested it, and it seems to be working correctly. I will offer this up to anyone else who may find it useful.




Code: Select all

-----------------------------------------------------------------------------------------------------------------
local root_dir = "D:/TEST/" .. subdir .. "/"
--physical path for storing temporary uploaded files
local timeout = 3600*24*14	    --delete uploaded files older than 14 Days
local removefiles = {}	--creates an empty table
local now = os.time()


for subdir in c_GetDir("D:/TEST/") do
 CallTheMaid(subdir)
end

function CallTheMaid(subdir)
    checkfile(root_dir)

    for _,filename in ipairs(removefiles) do
       c_RemoveFileDir(filename)
    end
end

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
-----------------------------------------------------------------------------------------------------------------
spenester
Posts: 1
Joined: Thu Jan 09, 2020 10:45 am

Re: Task scheduler for cleaning up temporary uploaded files

Post by spenester »

Hello Guesseyder
Sorry to comment on an old post and forgive my basic question (I have no experience of lua scripting).
I am trying to use your script to roll through a list of user directories located in the following structure.

/ftproot/test/user1
/ftproot/test/user2

I would want to delete all directories and files found in each userX directory but not the userX directories themself.

I therefore took your script and changed the root_dir location as below but i get the following error.

the Task Schedular gives the following...
[04] Thu, 09 Jan 2020 10:22:13 Failed to execute lua script of system task scheduler 'test clear'.

Would really appreciate any thoughts on this.

Many thanks

Code: Select all

----------------------------
local root_dir = "/ftproot/test/" .. subdir .. "/"
--physical path for storing temporary uploaded files
local timeout = 3600*1*1 --delete uploaded files older than 14 Days
local removefiles = {} --creates an empty table
local now = os.time()


for subdir in c_GetDir("/ftproot/test/") do
CallTheMaid(subdir)
end

function CallTheMaid(subdir)
checkfile(root_dir)

for _,filename in ipairs(removefiles) do
c_RemoveFileDir(filename)
end
end

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
FTP
Site Admin
Posts: 2072
Joined: Tue Sep 29, 2009 6:09 am

Re: Task scheduler for cleaning up temporary uploaded files

Post by FTP »

OK, actually you just need to remove that subfolder and then create a subfolder again:

Code: Select all

c_RemoveFileDir("/ftproot/test/user1")
c_MkDir("/ftproot/test/user1")
DomDis
Posts: 33
Joined: Sat Mar 18, 2023 7:44 pm

Re: Task scheduler for cleaning up temporary uploaded files

Post by DomDis »

FTP wrote: Sat Jan 11, 2020 3:38 am OK, actually you just need to remove that subfolder and then create a subfolder again:

Code: Select all

c_RemoveFileDir("/ftproot/test/user1")
c_MkDir("/ftproot/test/user1")
Wouldn't you need to check if the directory is empty first ?
FTP
Site Admin
Posts: 2072
Joined: Tue Sep 29, 2009 6:09 am

Re: Task scheduler for cleaning up temporary uploaded files

Post by FTP »

OK, there is no need to check whether the directory is empty first.
DomDis
Posts: 33
Joined: Sat Mar 18, 2023 7:44 pm

Re: Task scheduler for cleaning up temporary uploaded files

Post by DomDis »

FTP wrote: Sun Aug 20, 2023 3:21 am OK, there is no need to check whether the directory is empty first.
but if I'm deleting files that are say 30 days and older and I have files in here that are only 2 days old the c_RemoveFileDir("/ftproot/test/user1") and c_MkDir("/ftproot/test/user1") will wipe all that out no ?
Post Reply