Old 17th July 2007, 07:56   #1
is99
Member
 
Join Date: Mar 2006
Posts: 69
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 ?
is99 is offline   Reply With Quote
Old 17th July 2007, 11:36   #2
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
code:
!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
Afrow UK is offline   Reply With Quote
Old 17th July 2007, 11:48   #3
is99
Member
 
Join Date: Mar 2006
Posts: 69
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?
is99 is offline   Reply With Quote
Old 17th July 2007, 14:28   #4
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,153
Please not that the file could be open for shared write and that the example will not work in those cases

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 17th July 2007, 14:32   #5
is99
Member
 
Join Date: Mar 2006
Posts: 69
I don't think that im my case there will be share write, but will it work with binary files?
is99 is offline   Reply With Quote
Old 17th July 2007, 21:48   #6
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
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
Afrow UK is offline   Reply With Quote
Old 26th July 2007, 13:33   #7
is99
Member
 
Join Date: Mar 2006
Posts: 69
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.
is99 is offline   Reply With Quote
Old 26th July 2007, 13:35   #8
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
When it is open in notepad can you delete/rename/move the file in explorer? I certainly can.

Stu
Afrow UK is offline   Reply With Quote
Old 26th July 2007, 13:41   #9
is99
Member
 
Join Date: Mar 2006
Posts: 69
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 ?
is99 is offline   Reply With Quote
Old 26th July 2007, 13:57   #10
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
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
Afrow UK is offline   Reply With Quote
Old 4th September 2007, 19:38   #11
ChesterBr
Junior Member
 
Join Date: Sep 2007
Posts: 2
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!
ChesterBr is offline   Reply With Quote
Old 4th September 2007, 22:08   #12
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
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
Afrow UK is offline   Reply With Quote
Old 4th September 2007, 22:39   #13
ChesterBr
Junior Member
 
Join Date: Sep 2007
Posts: 2
Great, I will take a look. Thank you very much!
ChesterBr is offline   Reply With Quote
Old 6th September 2007, 12:52   #14
kalverson
Senior Member
 
Join Date: May 2007
Location: Maple Grove, Minnesota
Posts: 134
Quote:
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
kalverson is offline   Reply With Quote
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
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump