cancel
Showing results for 
Search instead for 
Did you mean: 

Delete a file based on date?

SOLVED
Occasional Contributor

Delete a file based on date?

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

2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Community Hero

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/aqfilesystem/findfiles.htm...

https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqobjiterator/index.html

https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqfileinfo/index.html


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available

View solution in original post

Highlighted
Occasional Contributor

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.");
}

View solution in original post

7 REPLIES 7
Highlighted
Occasional Contributor

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.

Highlighted
Community Hero

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/aqfilesystem/findfiles.htm...

https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqobjiterator/index.html

https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqfileinfo/index.html


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available

View solution in original post

Highlighted
Occasional Contributor

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.

Highlighted
Community Hero

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.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
Highlighted
Occasional Contributor

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" );
    }
}

 

Highlighted
Community Hero

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. 🙂


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
Highlighted
Occasional Contributor

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.");
}

View solution in original post

New Here?
Join us and watch the welcome video:
Announcements
Join TechCorner Challenge!
Want a fun and easy way to learn TestComplete? Try solving weekly TechCorner challenges and get into the Leaderboard!


Challenge Status

Get properties of a web page element

See replies!

Compare images using the Region Checkpoint

See replies!

Compare HTML table with Excel file and correct data in Excel file

Participate!

How to execute remote test and obtain results via Test Runner REST API

Participate!
Top Kudoed Authors