KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Deleting files programmatically by days ago and later
PRODUCT: 4D | VERSION: 14.0 | PLATFORM: Mac & Win
Published On: May 29, 2014

Below is a utility method to delete files in a path programatically by file extension and the number of days ago (and later) from the current date.

// -----------------------------------------------------------------
// Name: DELETE_FILES_BY_DAYS_AGO
// Description: Takes the path to where to delete the files, the
// file extension type, and number of days ago (and later) from the current
// date to delete files.
//
// Parameters:
// $1 (TEXT) - Path to where the files delete
// $2 (TEXT) - File extension type to delete
// $3 (LONGINT) - Number of days ago from the current date to delete
// -----------------------------------------------------------------

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;$days;$3) // number of days ago to delete
C_DATE($modified_on) // date of the file that has been modified
ARRAY TEXT($temp_array;0) // array for the file names in a path

If (Count parameters>2)

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

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

    // extract the document list for a specific document type
   For ($i;1;Size of array($temp_array)
      $type:=Document type($temp_array{$i}) // determine type
      $type:="@"+$type+"@" // wild card file extension

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

         If ((Current date-$modified_on)>=$days) // check the number of days to delete
            DELETE DOCUMENT($path+"\\"+$temp_array{$i})
         End if
      End if
   End for

End if


An example of using the method to delete text files of ".txt" extension that are 7 days old.
DELETE_FILES_BY_DAYS_AGO ($path;".txt";7)