View Single Post
Old 17th March 2010, 10:57   #15
Animaether
Major Dude
 
Join Date: Jun 2001
Posts: 1,173
while looking for a better method to figure out if a file is locked (without using the heavy LockedList function), I stumbled upon this thread which seems to suggest a method I'm currently using as well - and which had a grave error.

Do -not- use..
code:

FileOpen $R0 "C:\file.txt" w



Instead, use (in lieu of something better)...
code:

FileOpen $R0 "C:\file.txt" a



The reason is that opening a file in -Write- mode will clear its contents if the file opened okay. So after you have determined that the file is not locked: congratulations, you also killed it
Not a problem if you plan on overwriting it anyway - huge potential for problems, though.

Unfortunately, as Anders points out, this won't help when the file is open for shared write (probably a rare situation).
In addition, you end up 'touching' the file (changing its last modified timestamp, etc.).

Just trying to move/rename a file isn't panacea either - I've currently got a file locked ($SYSDIR\someDLL.dll).. I can't open it for append, can't overwrite it ..but I can rename it just fine.

I suppose one option would be to...
1. make a backup copy
2. try to overwrite the file
3a. restore the backup if the overwrite was successful - file was not locked
3b. delete the backup if the overwrite was unsuccessful - file was locked.

This, too, seems a bit 'dirty' though.
Animaether is offline   Reply With Quote