- NSIS Discussion
- Check if file in use
Archive: Check if file in use
is99
17th July 2007 08:56 UTC
Check if file in use
Hi
How can I check if some file is currently in use?
I mean if for example its EXE file then I can check using proceses if its running or not, but what if this is some TXT file that is simply open (lets say a user opened it in Notepad) how can I know that its open ?
Afrow UK
17th July 2007 12:36 UTC
!include LogicLib.nsh
...
ClearErrors
FileOpen $R0 "C:\file.txt" w
${If} ${Errors}
# File is locked.
${Else}
FileClose $R0
${EndIf}
If you want to know what program is locking the file, use the LockedList plug-in (Windows NT4+ only).
Stu
is99
17th July 2007 12:48 UTC
Nice idea 10x
And if this is a binary file like EXE or DLL will this work? I mean if I open and close binary file it stays as it was and not been damaged or something?
Anders
17th July 2007 15:28 UTC
Please not that the file could be open for shared write and that the example will not work in those cases
is99
17th July 2007 15:32 UTC
I don't think that im my case there will be share write, but will it work with binary files?
Afrow UK
17th July 2007 22:48 UTC
It should do. To try that, duplicate the code so the installer tries to open the unlocked file twice and the 2nd open should fail.
The other option is to attempt a rename and then check for errors.
Stu
is99
26th July 2007 14:33 UTC
Something wrong is going on :)
I took a text file and tried the first option (to open a file for writing), unfortunatly even when this text file was open in Notepad during the run I still got no error in NSIS.
So I told myself maybe its share writing or something and moved to a second option (to rename a file) and it also shove no error !!!!
Even worst then that what happend is that I had 1.txt file and I asked to rename it to 1.old, I opened this 1.txt in Notepad again and run the NSIS code and again no erorr and it actually created a 1.old file and left the old one too, so basicly it did copy instead of rename
Any ideas what can be done ?
Thanks.
Afrow UK
26th July 2007 14:35 UTC
When it is open in notepad can you delete/rename/move the file in explorer? I certainly can.
Stu
is99
26th July 2007 14:41 UTC
First of all I never tried it till now, and actually it looks a bit strange that I can dlete a file that is currently open.
But anyway back to my problem, so if non of these options work then how can I check in NSIS that file is currently in use ?
Afrow UK
26th July 2007 14:57 UTC
If you can delete a file that notepad has open then notepad probably isn't locking it. If you want to test this theory, try WhoUses.exe:
http://www.codeguru.com/cpp/w-p/syst...cle.php/c2827/
Stu
ChesterBr
4th September 2007 20:38 UTC
Just to clarify, Notepad does not keep a file open. What it does when you open a file is:
a) Open the file
b) Read all of its contents in memory (swap file will implicitly be used here if file is too big)
c) Close the file, only remembering its local name
When you are editing the "file" (i.e., the in-memory data), the file can be freely deleted, moved, etc.
If you save the file, it reopens the file, writes back all data and closes it. If the file is not there anymore, the save becomes a "save as", prompting you for a new file name.
So Notepad is not a great tool to check if a file is open. Word (or OpenOffice Write), however, will do the trick, since they keep the file open while editing (even text files).
Anyway, does anyone has any example checking if a program is open? I would like my uninstaller to make sure the program is closed (or even close it) before uninstalling, otherwise, the program (and its companion DLLs) are not uninstalled.
Thanks!
Afrow UK
4th September 2007 23:08 UTC
Use the LockedList plug-in which I wrote using the same (undocumented) API's that WhoUses.exe uses. I plan to add some more simple functionality as well, i.e. an option to list any running programs that appear on the task bar, and any modules that are in use that match a module name (currently a full module path is required).
Stu
ChesterBr
4th September 2007 23:39 UTC
Great, I will take a look. Thank you very much!
kalverson
6th September 2007 13:52 UTC
Originally posted by Afrow UK
Use the LockedList plug-in which I wrote using the same (undocumented) API's that WhoUses.exe uses. I plan to add some more simple functionality as well, i.e. an option to list any running programs that appear on the task bar, and any modules that are in use that match a module name (currently a full module path is required).
Stu
Hi,
I was not able to compile the plug-in due to a missing header file exdll.h
This file was not included in the zip file.
Ken
Animaether
17th March 2010 12:57 UTC
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..
FileOpen $R0 "C:\file.txt" w
Instead, use (in lieu of something better)...
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 :D
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.