Reputation: 79467
In my program I need to be able to delete the directory with the old output. But some files inside it might be locked. In this case, I want to warn the user that he needs to close the applications that lock them before the program can proceed, with a Retry/Cancel dialog, and if the user clicks Cancel, I want the directory to stay intact.
So, can anyone tell me of a way to check if an entire directory tree can be deleted (no locked files in any subdirectories), without modifying its contents - something like a dry-run.
Upvotes: 1
Views: 456
Reputation: 24273
Using the Restart Manager will tell you if any files are in use and offer to shut them down. Nothing will protect against the race condition of something locking them after your check though.
Upvotes: 1
Reputation: 12403
You can call _access function on each file in directory-tree. But still someone can lock a file after it was checked.
Upvotes: 1
Reputation: 36082
Maybe you could try and rename the parent directory, in Windows at least you would get an error saying it is not possible if a file in a subfolder is locked. If rename succeeds it should be safe to delete and prevents others of accessing the tree before you delete it.
Upvotes: 3