Monday, July 22, 2019

NAV CAL - Delete old files by created date using File Management

Name DataType Subtype Length
FileMgt Codeunit File Management
FileSystemObject Automation 'Microsoft Scripting Runtime'.FileSystemObject
TheFile Automation 'Microsoft Scripting Runtime'.File

Name DataType Subtype Length
FileList Record Name/Value Buffer
FileDateCreated Text 50
DeleteOlderThan Text 50


DeleteOldFiles()
CREATE(FileSystemObject,FALSE,TRUE);
IF VET.FIND('-') THEN BEGIN
  REPEAT
    FileMgt.GetServerDirectoryFilesList(FileList,VET."Archive File Location");
      IF FileList.FIND('-') THEN BEGIN
        REPEAT
          TheFile := FileSystemObject.GetFile(FileList.Name);
          FileDateCreated := FORMAT(TheFile.DateCreated);
          DeleteOlderThan := FORMAT(EndLogDate);
          IF FileDateCreated <= DeleteOlderThan THEN BEGIN
            FileMgt.DeleteClientFile(FileList.Name);
            //MESSAGE('File date:'  + FileDateCreated + 'Delete older than' + DeleteOlderThan + ' file deleted.');
          END;
        UNTIL FileList.NEXT <= 0;
      END;
  UNTIL VET.NEXT <=0;
END;

No comments:

Post a Comment