3

How exactly are locked files handled by the os ?

Or to be precise, why is it that sometimes I cannot delete a locked file from a Windows Explorer, but if I open a command prompt in the directory in question, I can delete it from there ?

3 Answers 3

4

When a process opens a handle to a file or directory it specifies what share level it wants. The share level can be any combination of the flags FILE_SHARE_DELETE, FILE_SHARE_READ and FILE_SHARE_WRITE. The names of the flags are pretty obvious. If I specify FILE_SHARE_READ this allows other processes to read the file, FILE_SHARE_WRITE allows other processes to write the file (possibly changing it under my feet) and FILE_SHARE_DELETE allows other processes to delete the file (though I think it doesn't actually get deleted until after I close my file handle).

Anyhow, if any process opens a handle to a file and does not specify FILE_SHARE_DELETE this means no other process can delete the file until the first process has closed it's handle. This is the usual reason you get those "file is locked by another process" errors. Note that processes can open handles to directories as well, so this also applies to deleting directories, even if they are empty.

As for why you can delete a file from a command prompt when you can't delete it from Explorer, I don't understand why this would happen. I would need to see it with my own eyes.

JR

3
  • I'm sorry I didn't vidcap it, but that's what happened. I still didn't close that explorer window. I noticed this behaviour a few times before, although I never made any thought on it.
    – Rook
    Jul 10, 2009 at 22:31
  • What are the chances explorer "released" it a second before I opened a cmd window ?
    – Rook
    Jul 10, 2009 at 22:32
  • If you're using Explorer to copy the file from the camera it's possible Explorer is temporarily leaving a lock on the file. I wonder if just switching the focus away from Explorer causes the lock to be released. Jul 11, 2009 at 8:34
1

John's right... you can't delete files that are in use, because... they are in use. It's the same reason you can't change your tires while driving to work. All sorts of unexpected things would happen.

If you want to know what process is using the file (so you can temporily kill the process so that it releases the lock on the file, use Process Explorer from Microsoft/Sysinternals.

  • Click the Find menu, and choose Find Handle or DLL...
  • Type the file name which you are unable to delete that is locked by some process
  • After typing the search phrase, click the Search button

If a file is in use by Windows, you can use MoveOnBoot by Gibin Software to delete the locked file after a reboot.

0

I'd guess that you have some third party extension on explorer that opens the file for a bit to scan the file, causing explorer to lock it. Is it only larger files that have this issue? Locks are locks in the windows world, no command prompt can delete a locked file (although as noted above there are ways to close the lock, then delete the file)

1
  • Yes, it was a relatively large file (video transferred from a camera). As I said to John, I've noticed this behaviour with explorer locking/cmd being able to delete the file a few times before, but only just now I thought of asking why is that so.
    – Rook
    Jul 10, 2009 at 22:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .