0

I'm trying to delete folders and their contents when the folder is older than a certain date. The problem is, there could be files in the folder that are currently in use and newer than the folder date.

The following will not catch the locked files so the program fails: FYI: targetTime = now.Add(new TimeSpan(-12, 0, 0));

    try {
      if(modification >= targetTime) {
        Directory.Delete(dir, true); 
      }
    } catch(IOException) {
      // do nothing
    }

The following works but seems like a lot of extra work when catching an exception should work.

  string[] dirs   = Directory.GetDirectories(tempPath, "*", SearchOption.TopDirectoryOnly);

  foreach(string dir in dirs) {
    bool newFile = false;
    DateTime modification = File.GetLastWriteTime(dir);
    var file = Directory.GetFiles(dir, "*",SearchOption.AllDirectories).FirstOrDefault();

    // test for newer files and directories
    foreach(string testFile in files) {
      DateTime foundMod = File.GetCreationTime(testFile);
      if(foundMod >= targetTime) {
        newFile = true;
      }
      else if(modification >= targetTime) {
        newFile = true;
      }
    }

    // if nothing newer is found, then nothing should be locked
    if(!newFile) {
      Directory.Delete(dir, true); 
    }
  }

Is there a simpler way to catch the exception that causes Directory.Delete(dir, true); to fail when a file or folder is locked or in-use?

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Mr. Coz
  • 25
  • 11
  • 1
    By the time you iterate each file and check for an active handle, you could have just simply caught an IOException. Have you seen this post? -> https://stackoverflow.com/questions/26741191/ioexception-the-process-cannot-access-the-file-file-path-because-it-is-being – Ross Bush May 04 '23 at 18:15
  • I'm not seeing anything in https://stackoverflow.com/questions/26741191/ioexception-the-process-cannot-access-the-file-file-path-because-it-is-being that address my question as how to use the exception cause when trying to delete a folder that has open files inside it. Directory.Delete(dir, true) causes System.UnauthorizedAccessException:'Access to the path 'lock' is denied.' The link given seems to have to do with exceptions on files and not directories. – Mr. Coz May 04 '23 at 18:51
  • 1
    Sorry, that was misleading. Similar concept but with folders. Directory.Delete returns an IOException when the directory is in use. According to the docs and IOException when be returned when "The directory is being used by another process.". https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.delete?view=net-8.0 – Ross Bush May 04 '23 at 18:57
  • Thanks! That link had the info I needed. I was using IOException instead of UnauthorizedAccessException. IOException always worked for everything else I did so I didn't think about it. Two issues resolved; fixed my problem and I learned something. – Mr. Coz May 04 '23 at 20:03

1 Answers1

0

There were a few issues I needed to resolve to get it to work correctly. I know this is simple for most people, but for people like me, it's always a challenge. The final code segment that is triggered by a scheduled event is as follows:

  string tempPath = @"C:\<my path>\";
  DateTime now = DateTime.Now;
  DateTime targetTime = now.Add(new TimeSpan(-12, 0, 0));

  string[] files = Directory.GetFiles(tempPath, "*", SearchOption.TopDirectoryOnly);
  foreach(string file in files) {
    DateTime modification = File.GetCreationTime(file);
    try {
      if(modification <= targetTime) {
        File.Delete(Path.Combine(file));
      }
    } catch(Exception e) {
      // do nothing
    }
  }

  string[] dirs   = Directory.GetDirectories(tempPath, "*", SearchOption.TopDirectoryOnly);
  foreach(string dir in dirs) {
    DateTime modification = File.GetCreationTime(dir);
    try {
      if(modification <= targetTime) {
        Directory.Delete(dir, true);
      }
    } catch(Exception e) {
      // do nothing
    }
  }
}
Mr. Coz
  • 25
  • 11