Page 1 of 1

Algorithm for deleting files

Posted: Tue Sep 05, 2023 10:37 am
by trevix
Hello.
I am trying to find the best and fastest way, on standalone opening, to delete stored files, according to some requirements:
I have a list of file names (there may be hundreds of lines) similar to this (underscore delimited):
I would like to only keep the newest first 10 files "for each user email". That is, to get a list of files to delete from the storage so that what is left are the most recent 10 files for each user.
I could do it with a lots of repeat loop but I think there could be a better way using arrays.
Any idea?
Thanks
Trevix

Re: Algorithm for deleting files

Posted: Tue Sep 05, 2023 10:59 am
by Klaus
Buongiorno trevix,

arrays? Come on, no need for arrays! :-D

1. Get the list of -> the detailed files
2. Sort this list by item 5 (Last modification date)
3. Delete line 1 to 10 of this list
4. Now use a repeat loop through the rest of the list and delete all these files.

Or did I misunderstand your problem?


Best

Klaus

Re: Algorithm for deleting files

Posted: Tue Sep 05, 2023 11:20 am
by trevix
The best I could think of, is the following:

Code: Select all

on mouseUp
     --create a dummy list
     repeat with U = 1 to 11
          put U & "_testuser2@" & cr after tList
     end repeat
     put "12_testuser1@" & cr after tList
     put "cane_testuser3@"  after tList

     --get list of owners
     set the itemdelimiter to "_"
     repeat for each line tLine in tList
          put empty into tOwnersArray[item 2 of tLine][tLine]
     end repeat
     breakpoint
     repeat for each line tEmail in the keys of tOwnersArray
          put the keys of tOwnersArray[tEmail] into tOwnerList
          sort lines of tOwnerList descending numeric by item 1 of each
          repeat with U = 11 to the number of lines of tOwnerList
               put line U of tOwnerList & cr after tDeleteList
          end repeat
     end repeat
     
     breakpoint
end mouseUp
Only 1_testuser2@ gets deleted, the older one of the 11 files of testuser2@

Re: Algorithm for deleting files

Posted: Tue Sep 05, 2023 11:39 am
by LCMark
@trevix: Assuming these files all sit in their own folder then you can do:

Code: Select all

command keepRecentFiles pFolder, pCount
   /* Fetch the list of all files in the specified folder. */
   local tFiles
   put files(pFolder) into tFiles
   
   /* The filenames are of the form YYYMMDDHHMMSS_<email>. So set the
    * the item delimiter appropriately to make accessing the timestamp
    * easy, and then sort descending by the first item so the most recent
    * files are first. */
   set the itemDelimiter to "_"
   sort tFiles descending text by item 1 of each
   set the itemDelimiter to comma
 
   /* Loop through the files, skipping the first pCount, and
     * deleting the rest. */
   local tIndex
   repeat for each line tFile in tFiles
      add 1 to tIndex
      if tIndex <= pCount then
         next repeat
      end if
      delete file (pFolder & slash & tFile)
   end repeat
end keepRecentFiles
Note: The sort is 'text' because timestamps which are YYYYMMDDHHMMSS have the same order as both numbers and text, but by using text you avoid any numeric conversions.

Re: Algorithm for deleting files

Posted: Tue Sep 05, 2023 11:44 am
by Klaus
Oops, sorry. completely overlooked -> "for each user email"

Re: Algorithm for deleting files

Posted: Tue Sep 05, 2023 12:37 pm
by LCMark
@Klaus: You aren't the only one :D

Re: Algorithm for deleting files

Posted: Tue Sep 05, 2023 6:23 pm
by jacque
@trevix, your handler should work if you change this line:

Code: Select all

repeat with U = 11 to the number of lines of tOwnerList
to this:

Code: Select all

repeat with U = the number of lines of tOwnerList down to 11
When deleting items or lines in a list you have to delete backwards. Otherwise what used to be line 12 becomes line 11 after the first deletion finishes.

Re: Algorithm for deleting files

Posted: Tue Sep 05, 2023 6:41 pm
by trevix
Correct, Jacque. Thanks.

Any way I was hoping on some slicker trick to do this thing.
All these repeat loop may not be efficient enough when deleting from a list of hundreds filenames.
One more thing that I don't know if the "delete file filepath" command is time consuming or not.

My final code is the following:

Code: Select all

--create a dummy list
     repeat with U = 1 to 11
          put U & "_testuser2@" & cr after tList
     end repeat
     put "12_testuser1@" & cr after tList
     put "cane_testuser3@"  after tList

     --get list of owners
set the itemdelimiter to "_"
repeat for each line tLine in tList
                    put empty into tOwnersArray[item 4 of tLine][tLine]
end repeat
repeat for each line tEmail in the keys of tOwnersArray
                    put the keys of tOwnersArray[tEmail] into tOwnerList
                    sort lines of tOwnerList descending text by item 1 of each --use text for yyyymmddhhmm
                    repeat with U = 11 to the number of lines of tOwnerList
                         put line U of tOwnerList into tFile
                         delete file gPrefTF["AllPath"]["PathBackup" & gSport] & tFile
                    end repeat
end repeat

Re: Algorithm for deleting files

Posted: Wed Sep 06, 2023 5:43 pm
by jacque
The best way to know how fast deletions will be is to just time it on some sample data, but in general I LC is very fast when working with files and unless you have thousands of files I don't think there will be too much delay.