Quantcast
Viewing latest article 18
Browse Latest Browse All 68

Checking File Expiration

Checking File Expiration

“Fish and visitors stink after three days”
Benjamin Franklin, Poor Richards Almanac

Files are like guests, some are like family, others eat your groceries and won’t leave. TestComplete’s built-in aqFile object helps decide which files have over-stayed by asking…

  • Is the file older than three days?
  • Is anyone using the file? Has it been accessed in the last week?
  • Is anyone updating the file? Has it been modified in the last month?

The answers help determine a file’s value. In a text logging scenario, for example, you can evict a file before it has a chance to balloon. The aqFile GetCreationTime method returns a Date/Time. To establish an expiration time, pass the file creation time to aqDateTime
AddDays. Then compare aqDateTime’s Now to see if it exceeds the expiration time.

var path = "c:\\data\\mytextlog.txt"; 

if (aqFile.Exists(path)){
  var created = aqFile.GetCreationTime(path); 
  var expiration = aqDateTime.AddDays(created, 3); 
  
  // if file creation time exceeds the expiration time
  if (aqDateTime.Now() > expiration){
    // do something about the expired file
    aqFile.Delete(path);
    aqFile.Create(path);
    // make sure the file's Creation time is reset to 
    // the current date and time so we can calculate its
    // expiration date and time later
    aqFile.SetCreationTime(path, aqDateTime.Now()); 
  } 
}

The key methods that make this work are GetCreationTime that reads the file’s Date/Time, and SetCreationTime that time-stamps the creation date of the physical file on disk. To unit test this script, create a shorter expiration date using one of the other aqDateTime methods: AddHours, AddMinutes, or AddSeconds.

To know when a file was last modified or accessed, use GetLastWriteTime and GetLastAccessTime methods instead of GetCreationTime. The example below checks if a file has been modified in the last ten minutes. If the file has “expired”, instead of recreating the file, the file is moved to an archive directory and hidden.

function archive(path, archivePath){
  if (aqFile.Exists(path)){
    var modified = aqFile.GetLastWriteTime(path); 
    var expiration = aqDateTime.AddMinutes(modified, 10); 
  
    // if file hasn't been written to in the last ten minutes...
    if (aqDateTime.Now() > expiration){
      // decompose the file path to its component parts
      var filePath = aqFileSystem.GetFileFolder(archivePath); 
      var fileName = aqFileSystem.GetFileNameWithoutExtension(path); 
      var extension = aqFileSystem.GetFileExtension(path);
      // put it back together again  
      var newPath = filePath + fileName + 
        aqConvert.DateTimeToFormatStr(aqDateTime.Now(), "_%m%d%Y%H%M%S") 
        + "." + extension; 
      // rename and move the file to archive directory 
      aqFile.Rename(path, newPath);
      // change attributes to hidden and readonly
      var attributes = 
        aqFileSystem.faReadOnly | aqFileSystem.faHidden
      aqFile.SetFileAttributes(newPath, attributes);
    }  
  } 
}

To slice-and-dice file paths, use the aqFileSystem object’s GetFileFolder, GetFileNameWithoutExtension and GetFileExtension methods. The example shreds the original file path, then reconstitutes it as a unique name on a new path. The Locals window shows variable state just before the file is renamed. While the path variable places mytextlog.txt in the \data directory, newPath points to the \data\archive directory.

Image may be NSFW.
Clik here to view.

If you want to make a file read-only or hidden, use the SetFileAttributes method. SetFileAttributes takes the path to the file and an integer. This second parameter is made up of one or more aqFileSystem constants like faReadOnly and faHidden. To assign multiple attributes, use the bitwise OR operator:

var attributes =
  aqFileSystem.faReadOnly<strong> | aqFileSystem.faHidden

aqFile.SetFileAttributes(newPath, attributes);

Wrap Up

The built-in aqFile, aqFileSystem and aqDateTime objects form a basic palette for expiring dated files and a score of other useful tasks.

The post Checking File Expiration appeared first on Falafel Software Blog.


Viewing latest article 18
Browse Latest Browse All 68

Trending Articles