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
Hi Thomas
Can I clarify your question, what you are really looking to do is each day start a process that will delete files in a given directory that are older than some period of time?
If so I may be able to help there.
Jenna
Hi Jenna,
I was interested in doing something that you are mentioning. Do you have anything that could start me off?
Thank you.
Kind Regards,
Salma
I have a task that deletes txt files older than x number of days:
Hopefully you can make this work for your needs
Hi
This is great thanks, it's exactly what I was looking for. Thank you so much.
Kind Regards,
Salma
Not sure if that's right, but maybe you're asking for this? You can do whatever you want on each iteration, including delete the obsolete files.
Hi-
Heres something I did on a Windows system at one point to purge files in a directory that were older than a given time frame
I'm pretty sure there is also ways to do this using something in %File or one of those classes also. I will poke around for an example there as well, but this should get you started.
Jen