Hi all,
I've been looking for a way to manage files by deleting based on the files creation date.
I've seen the aqFile.GetCreationTime method, however this appears to want a specific file name.
Is there any way within test complete of finding files (more than one) based on their creation date in order to then use this information to delete them?
I have a feeling this might be something for a batch job to do, which will take a fraction of the time to do this way over the amount of time I've spent looking for a way to do this within TestComplete.
Many thanks.
Tris
Solved! Go to Solution.
I would use a combination of several objects. You should use the aqFileSystem object and the method "findfiles" to retrieve a list of files. Then you can iterate through that list (all aqFileInfo objects) and, if the CreatedDate matches what you're looking for, delete it.
Relveant objects:
https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqobjiterator/index.html
https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqfileinfo/index.html
Good spot, I've refactoted it in to a single routine as below.
function LogManager() { var foundFiles, aFile; foundFiles = aqFileSystem.FindFiles("C:\\Automation\\Log\\All", "*"); if (foundFiles != null) while (foundFiles.HasNext()) { aFile = foundFiles.Next(); var DateTime = aqDateTime.Now(); var DateTimeMinusXDays = aqDateTime.AddDays(DateTime, -14); if (aFile.DateCreated < DateTimeMinusXDays) { aFile.Delete(); } } else Log.Message("No log files were found."); }
FYI - I've manage to do exactly as I said I would try above, taken a matter of minutes via batch file.
I made a batch file which TC will call and that will manage the files older than x days, see batchfile code below.
ForFiles /p "C:\FOLDER" /d -14 /c "cmd /c del @file"
/d sets the days value you are looking to delete, the example is going to delete anything older than 14 days
/c is for the command string which deletes the files which are older than 14 days in the example above
note, if you have sub-directories you want to clear out use /s like so... "C;FOLDER" /s
Note - still interested if my original question is possible, find TC is rather limited in a few ways and ideally want to keep as much of the functionality within the program if possible.
I would use a combination of several objects. You should use the aqFileSystem object and the method "findfiles" to retrieve a list of files. Then you can iterate through that list (all aqFileInfo objects) and, if the CreatedDate matches what you're looking for, delete it.
Relveant objects:
https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqobjiterator/index.html
https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqfileinfo/index.html
Thanks Tristaanogre, will look in to this later today, think it's a case of getting the item info back, from what I remember I could only get the number of files to be returned, so will have another stab at it with fresh eyes.
The aqFileSystem.FindFiles method will return a collection of aqFileInfo objects for all files that match the directory search. You then iterate through that and check the CreationDate and, if it matches what you want, simply call Delete.
Finally got round to being able to look in to this and have managed to get this working 🙂
Thought I'd share my code in case this helps someone in the future.
function LogFinder() { var foundFiles, aFile; foundFiles = aqFileSystem.FindFiles("C:\\Automation\\Log\\All", "*"); if (foundFiles != null) while (foundFiles.HasNext()) { aFile = foundFiles.Next(); OldLogsDelete(aFile); } else Log.Message("No files were found."); } function OldLogsDelete(aFile){ var DateTime = aqDateTime.Now(); var DateTimeMinusXDays = aqDateTime.AddDays(DateTime, -14); if (aFile.DateCreated < DateTimeMinusXDays) { FilePathString = aqConvert.VarToStr(aFile.Path) aqFileSystem.DeleteFile(FilePathString); Log.Message(FilePathString + " deleted" ); } }
Glad it worked for you!
One item of note... in your OldFilesDelete function, you SHOULD be able to just call aFile.Delete() as the aqFileInfo object supports a "Delete" method (I believe). But, if this is working, then that's fine. 🙂 Just looking for code efficiency. 🙂
Good spot, I've refactoted it in to a single routine as below.
function LogManager() { var foundFiles, aFile; foundFiles = aqFileSystem.FindFiles("C:\\Automation\\Log\\All", "*"); if (foundFiles != null) while (foundFiles.HasNext()) { aFile = foundFiles.Next(); var DateTime = aqDateTime.Now(); var DateTimeMinusXDays = aqDateTime.AddDays(DateTime, -14); if (aFile.DateCreated < DateTimeMinusXDays) { aFile.Delete(); } } else Log.Message("No log files were found."); }