That's a fairly common problem with file deletion on Windows. On a typical installation, there *are* often open files - the usual suspects are a virus scanners and file system indexers/search engines; the explorer may also open files if the directory is display in the explorer. They register for file change notifications, and when files are created, they put them onto their todo list, and will eventually open them (usually fairly quickly after they got created). They then read them once, and deletion attempts will fail at that point.
If that's the cause, it is fairly difficult to work-around. The "proper" way is first to try DeleteFile (talking plain Win32 API here); if that fails with sharing violation, move the file to the trash folder, open it for deletion, SetFileInformation with a delete disposition, and close the file. In the latter case, the file will go away if the last handle is closed.
I've seen this with a VisualSmalltalk app, and a workaround that fixed the problem was to use shell32.dll SHFileOperation. (I have posted a detailed answer already on this thread, but it isn't showing up in gmane. Just in case it has got lost, I've repeated the original reply below) -- I have also seen this problem on Windows.
From what I remember, disabling the windows indexing service + any virus
checkers + Google desktop search, made it happen less often :)
Also, it was more likely to occur when an explorer, viewing the folders contents, was open. And, from what I recall, it was made worse if the folder contained a .thumbs file and so it happened more often on XP than on Win2000.
In the end we solved the problem by using the Shell.dll function - SHFileOperation - with the FO_DELETE , FOF_NOERRORUI, FOF_SILENT, FOF_NOCONFIRMATION flags; to do the folder delete. We also removed any read only, archive, flags from the files in the folder before calling the function).
You could probably use FFI to call the function, or else make a plugin. -- I should have mentioned that SHFileOperation will remove a folder AND its contents in one operation. There is no need to delete the folder's contents first. Also, when setting the folders files to not read-only; this is done RECURSIVELY. i.e. the folder's files, + the folder's child folders' files, + the folder's folders' folders' files etc..
Depending on the flags used, this operation may NOT put the deleted folder into the recycle bin. So beware, it can instantly remove a large chunk of your disk if you get the folder path wrong !