How to sequentialy delete file from a folder
Hello gentlemen,
here is my question: I would like to delete files one by one (not all at a time because I need to check somethinkg to delete it or not) in a defined folder but how can I handle it? I have used the extension :
EnsLib.File.PassthroughService
But it only works when a file is moved in my folder... But I want to launch my program everyday (with a planifier) in the purpose to delete file that more that 2 months old. With "PassthroughService", it won't work since the the file is not moved so it won't go in my onProcessInput...
If you can offer me a solution, I'll take it :)
Here is a sample of my code:
Class citadelle.test.FileDeletingWithFilter Extends EnsLib.File.PassthroughService { Property DumpPath As %String(MAXLEN = 500); Property TargetEmailSenderName As %String(MAXLEN = 128); Property KeyOfLookUpTable As %String; Parameter SETTINGS = "KeyOfLookUpTable:Basic, TargetEmailSenderName:Basic:selector?multiSelect=1&context={Ens.ContextSearch/ProductionItems?targets=1&productionName=@productionId}, DumpPath:Basic"; Method OnProcessInput(pInput As %Stream.Object, Output pOutput As %RegisteredObject) As %Status { SET sc = $$$OK set ..Adapter.DeleteFromServer = "" //Useful because we check ourselves if delete is needed set tTableName = "NameFolderDoNotDelete" SET directory = "" SET filePath = pInput.Attributes("Filename") $$$TRACE("Filepath: "_filePath) SET filename = $PIECE(filePath,"\",*) SET dumpPath = ..DumpPath //Directory where to delete everything $$$TRACE(dumpPath) If ##class(Ens.Util.LookupTable).%ExistsTable(tTableName)'=1 { // send mail $$$TRACE(tTableName_".lut doesn't exist, mail sent to warn") SET msg = ##class(citadelle.email.MessageEmail).%New() SET msg.To = "XXXXXX" SET msg.Subject = "Erreur,une table n'existe pas" SET msg.Body = "Bonjour,<br/> la LookUp table '"_tTableName_"' n'existe pas.<br/> Veuillez la créer. <br/>Bien à vous,<br/> FileDeletingWithFilter.cls" DO ..SendRequestAsync(..TargetEmailSenderName,msg) } else { Set offset = $FIND(filePath,dumpPath) If (offset > 0) { SET directory = $EXTRACT(filePath, offset+1, *-$LENGTH(filename)-1) //+1 et -1 pour enlever les \ $$$TRACE("Lists of directory that we cannot delete: "_directory) } try { SET valueToCheck = ^Ens.LookupTable(tTableName,..KeyOfLookUpTable) $$$TRACE(valueToCheck) } catch (ex) { s sc = ex.AsStatus() $$$TRACE("No values found in the lookup table") set valueToCheck = "" } if sc '= $$$OK { q sc } SET isDeleteNeeded = 0 //default = do not delete if ($FIND($ZCONVERT(valueToCheck,"U"),$ZCONVERT(directory,"U")) = 0) { //the folder is NOT in the "we cannot delete" list of the folder // delete file that are more than 2 months old SET dateNow = $ZDATETIME($HOROLOG, 3, 1) //2018-12-14 11:22:58 Set fileStream=##class(%File).%New(filePath) set fileStreamTimestamp = fileStream.LastModifiedGet() set ecart = $system.SQL.DATEDIFF("mm",fileStreamTimestamp,dateNow) $$$TRACE("Gap: "_ecart_" months") if (ecart >= 2 ) { SET isDeleteNeeded = 1 } } if (isDeleteNeeded){ $$$TRACE("File " _ filePath _ " is now deleted") SET statusFile = ##class(%Library.File).Delete(filePath) } } QUIT sc } }
Thanks,
Thomas