Page 1 of 2

Check whether a given file is open or closed

Posted: Sat Jan 15, 2011 2:09 pm
by Little John
Hi all,

I need to know, whether or not a particular file is opened by another process.
So I wrote the following small function, which works correctly as far as I can see (tested on Windows XP x86):

Code: Select all

Procedure.i FileIsClosed (fileName$)
   ; returns #False if the file does not exist, or if it is opened by another process;
   ;         #True otherwise
   ;
   ; Note: By passing zero as 3rd parameter for CreateFile_(),
   ;       we request *exclusive* access to the respective file.
   ;       This fails if the file is already open.
   ; <http://msdn.microsoft.com/en-us/library/aa363858%28v=vs.85%29.aspx>
   ; <http://msdn.microsoft.com/en-us/library/aa363874%28v=vs.85%29.aspx>
   Protected handle
   
   handle = CreateFile_(@fileName$, #GENERIC_READ, 0, #Null, #OPEN_EXISTING, #FILE_ATTRIBUTE_NORMAL, #Null)
   If handle = #INVALID_HANDLE_VALUE
      ProcedureReturn #False
   Else
      CloseHandle_(handle)
      ProcedureReturn #True
   EndIf   
EndProcedure


;-- Demo
Define TestFile$ = "Test.txt"
   
; Test #1
CreateFile(0, TestFile$)
Debug FileIsClosed(TestFile$)   ; should display 0
   
; Test #2
CloseFile(0)
Debug FileIsClosed(TestFile$)   ; should display 1
Any suggestions for improvement are welcome (if necessary).

Regards, Little John

Re: Check whether a given file is open or closed

Posted: Mon Jan 17, 2011 9:17 pm
by Marco2007
Works fine!

Re: Check whether a given file is open or closed

Posted: Tue Jan 18, 2011 6:12 am
by Little John
Thanks for the feedback!

Regards, Little John

Re: Check whether a given file is open or closed

Posted: Tue Jan 18, 2011 12:12 pm
by A M S
Work fine, that's useful :wink:

Re: Check whether a given file is open or closed

Posted: Tue Jan 18, 2011 1:49 pm
by Little John
Thanks, I'm happy that it is useful for some of you.

Regards, Little John

Re: Check whether a given file is open or closed

Posted: Fri Sep 02, 2011 12:02 pm
by mskuma
I think this is another (similar) solution
http://www.purebasic.fr/english/viewtop ... 13&t=44269

if you add a filesize() call you can determine existence also.

Re: Check whether a given file is open or closed

Posted: Fri Sep 02, 2011 12:11 pm
by Kiffi
i know, the Thread is old, but what about this?

Code: Select all

Procedure.i FileIsClosed2 (fileName$)
  ProcedureReturn RenameFile(fileName$, fileName$)
EndProcedure

;-- Demo
Define TestFile$ = "Test.txt"
   
; Test #1
CreateFile(0, TestFile$)
Debug FileIsClosed2(TestFile$)   ; should display 0
   
; Test #2
CloseFile(0)
Debug FileIsClosed2(TestFile$)   ; should display 1
Greetings ... Kiffi

Re: Check whether a given file is open or closed

Posted: Fri Sep 02, 2011 3:16 pm
by SFSxOI
that would work kiffi, until the application that was using the file crashed because it expected a file with a certain name and your procedure changed it to a different name.

Re: Check whether a given file is open or closed

Posted: Fri Sep 02, 2011 3:21 pm
by Kiffi
SFSxOI wrote:... and your procedure changed it to a different name.
my procedure doesn't change the name.

Greetings ... Kiffi

Re: Check whether a given file is open or closed

Posted: Fri Sep 02, 2011 7:38 pm
by Little John
Kiffi wrote:

Code: Select all

[...]
RenameFile(fileName$, fileName$)
[...]
Hehe ... great idea, thank you! Image
This should even work cross-platform.

Regards, Lttle John

Re: Check whether a given file is open or closed

Posted: Fri Sep 02, 2011 10:26 pm
by SFSxOI
Kiffi wrote:
SFSxOI wrote:... and your procedure changed it to a different name.
my procedure doesn't change the name.

Greetings ... Kiffi
Oki dokee then, i guess the early morning and not enough coffee made me think it did, but I see now that it doesn't so I stand corrected. Thanks for posting it Kiffi :)

Re: Check whether a given file is open or closed

Posted: Fri Sep 02, 2011 11:16 pm
by IdeasVacuum
That is a brilliant method Kiffi 8)

Re: Check whether a given file is open or closed

Posted: Mon Dec 17, 2012 4:31 pm
by yrreti
That is a brilliant method Kiffi 8)
I second that! and it works great.

Thanks for sharing it.

Re: Check whether a given file is open or closed

Posted: Mon Dec 17, 2012 4:40 pm
by luis
But what's the practical use for something like this ?

I'm asking because:

Code: Select all

if FileIsClosed ("file.txt") ; ok the file was not in use, some time ago, inside that proc
    ; but now ? 
endif

Re: Check whether a given file is open or closed

Posted: Mon Dec 17, 2012 10:47 pm
by yrreti
Hi luis

As quoted above:

Code: Select all

I need to know, whether or not a particular file is opened by another process.
I had a problem with the following.
CopyFile in a loop, CopyDirectory, DeleteFile in a loop, and DeleteDirectory intermittently failing.
Result = 0 with no reason that I could see.
The above code could possibly be useful to help with troubleshooting why the failure was occurring.
(Possible file still in use.)
The weird thing is that since I added the check, I haven't seen any files still in use, but the intermittently failing stopped?
If it does occur again, then maybe I'll have more of an idea why these commands intermittently fail.
At least I'll know whether it is being caused by a file still in use or not.

In truth, I don't have much confidence in all four of those commands, and will possible post some code later if it does
occur again. It does seem to occur much more when copying files to and from a usb drive.
But again, not since adding that check? Weird isn't it.

P.S. I don't use it in that procedure. I just use that check line in the loop itself.
And in the case of CopyDirectory and DeleteDirectory, I use an ExamineDirectory loop method to check the files first.

(I have to leave for a bit, so can't reply for awhile)

yrreti