Check whether a given file is open or closed

Windows specific forum
Little John
Addict
Addict
Posts: 4770
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Check whether a given file is open or closed

Post 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
Marco2007
Enthusiast
Enthusiast
Posts: 648
Joined: Tue Jun 12, 2007 10:30 am
Location: not there...

Re: Check whether a given file is open or closed

Post by Marco2007 »

Works fine!
PureBasic for Windows
Little John
Addict
Addict
Posts: 4770
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Check whether a given file is open or closed

Post by Little John »

Thanks for the feedback!

Regards, Little John
A M S
User
User
Posts: 58
Joined: Fri Aug 14, 2009 2:26 pm
Location: Afghanistan

Re: Check whether a given file is open or closed

Post by A M S »

Work fine, that's useful :wink:
Little John
Addict
Addict
Posts: 4770
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Check whether a given file is open or closed

Post by Little John »

Thanks, I'm happy that it is useful for some of you.

Regards, Little John
mskuma
Enthusiast
Enthusiast
Posts: 573
Joined: Sat Dec 03, 2005 1:31 am
Location: Australia

Re: Check whether a given file is open or closed

Post 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.
User avatar
Kiffi
Addict
Addict
Posts: 1484
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Check whether a given file is open or closed

Post 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
Hygge
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Check whether a given file is open or closed

Post 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.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
User avatar
Kiffi
Addict
Addict
Posts: 1484
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Check whether a given file is open or closed

Post by Kiffi »

SFSxOI wrote:... and your procedure changed it to a different name.
my procedure doesn't change the name.

Greetings ... Kiffi
Hygge
Little John
Addict
Addict
Posts: 4770
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Check whether a given file is open or closed

Post 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
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Check whether a given file is open or closed

Post 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 :)
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Check whether a given file is open or closed

Post by IdeasVacuum »

That is a brilliant method Kiffi 8)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: Check whether a given file is open or closed

Post by yrreti »

That is a brilliant method Kiffi 8)
I second that! and it works great.

Thanks for sharing it.
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Check whether a given file is open or closed

Post 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
"Have you tried turning it off and on again ?"
A little PureBasic review
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: Check whether a given file is open or closed

Post 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
Post Reply