KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Deleting files programmatically by the oldest modified date
PRODUCT: 4D | VERSION: 14.0 | PLATFORM: Mac & Win
Published On: August 11, 2014

Below is a utlity method to delete a number of files in a path programatically by file extension from sorting them from oldest to newest of their modified date. The method uses an array to collect all the files in the path and then extracts the appropriate file type to another array. To reference the file's modified date, in order to sort, another array would be used. Calling SORT ARRAY sorts both the file type modified date and file type arrays by date in decending order to delete a number of files from oldest to newest.

// -----------------------------------------------------------------
// Name: DELETE_FILES_FROM_OLDEST_DATE
// Description: Takes the path to where to delete the files, the
// file extension type to delete, and thenumber of files to delete
// from the oldest date.
//
// Parameters:
// $1 (TEXT) - Path to where the files delete
// $2 (TEXT) - File extension type to delete
// $3 (LONGINT) - Number of files to delete from the oldest date
// -----------------------------------------------------------------
C_TEXT($path;$1) // path of where the file(s) are located
C_TEXT($type_input;$2) // file extension type of the file selected
C_TEXT($type) // type of extensions
C_LONGINT($i;$last_num_files;$3) // last number of files to delete
C_DATE($modified_on) // date of the file that has been modified
C_DATE($date_temp;$date_working) // Use for the 2D array

ARRAY TEXT($folder_array;0) // array for the file names in a path
ARRAY TEXT($file_type_array;0) // array for the file type
ARRAY DATE($file_type_modified_date_array;0) // array for the file type modified date

If (Count parameters>2)

   $path:=$1
   $type_input:=Lowercase($2) // force characters to be lowercase
   $last_num_files:=$3

   DOCUMENT LIST($path;$folder_array) //puts the files names on a array

   For ($i;1;Size of array($folder_array)

      $type:=Document type($folder_array{$i}) // determine type
      $type:="@"+$type+"@" // wild card file extension

      If ($type_input=Lowercase($type)) // for the extension to be lowercase
         GET DOCUMENT PROPERTIES($path+"\\"+$folder_array{$i};$locked;$invisible;\
         $created_on;$created_at;$modified_on;$modified_at)

         APPEND TO ARRAY($file_type_array;$folder_array{$i})
         APPEND TO ARRAY($file_type_modified_date_array;$modified_on)
      End if

   End for

   // validate number of files to delete to what was found
   If (Size of array($file_type_array)>=$last_num_files)

         // sorting by decending order by oldest date
          SORT ARRAY($file_type_modified_date_array;$file_type_array;>)

       // Delete the number of files from oldest to newest
      For ($i;1;$last_num_files)
         DELETE DOCUMENT($path+"\\"+$file_type_array{$i})
      End for
   End if

End if



An example of using the method to delete 10 text files of ".txt" extension that are in a path.
DELETE_FILES_FROM_OLDEST_DATE ($path;".txt";10)