Access denied when creating a folder just deleted

I have a batch script which remove a folder and re-create it. The command that remove a folder is rd /s/q and the command create folder is mkdir. Recently I see something weird happening, everytime when the folder is deleted and re-created, there is error message Access is denied displayed. However, if I rerun the create folder command laer, it always pass successfully.

The explanation on http://support.microsoft.com/?id=159199 seems make sense,

This file is in a state known as pending deletion. This file has been deleted, but there are still handles open to it. NTFS will wait until all handles to this file are closed before updating the index. If an attempt is made to access the file, however, NTFS will deny the attempt. Because the file is listed in the index, but is effectively deleted, you can see the file but you cannot access it.

The workaround here is add a 60 seconds sleep between the folder deletion and folder creation operation.

Leave a Reply

Your email address will not be published.