IsFileOpen (by other program?) [Multi.OS][x86,x64]

Share your advanced PureBasic knowledge/code with the community.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 349
Joined: Mon Jul 08, 2013 8:43 pm

IsFileOpen (by other program?) [Multi.OS][x86,x64]

Post by minimy »

Hi guys! im back again :)
This is a simple but very interesting lib to know if one file is opened by other program.
Well, i need this to know when an external program finish job with a file. Is nice if you are waiting for other programs.
I was looking for other ways to know it, reading size, and others but no work i spect. If some body know other ways to do this im very interested to learn how.

Enjoy it!.. PBforum the best forum!
feel free to improve or modify..

Code: Select all

; -------------------------------------------------------------
; -------------------------------------------------------------
;
;         lib name: IsFileOpen
;         code by : minimy 
;         date    : 2019.10.05
;         O.S.    : Multi (not tested..) tell me about please :)
;         CPU     : x86, x64

;         info    : check if file is open by other program
;         syntax  : check= IsFileOpen(mifile.s)
;         return  : 1 if file is open by other program, 0 if is not opened
;         
;         garanty : three steps ;-)
;         worktime: two beers..  __.oO
;         
; -------------------------------------------------------------
; -------------------------------------------------------------

Procedure IsFileOpen(filename.s)
  Protected.i file= OpenFile(#PB_Any,filename,#PB_File_Append)
  If file
    CloseFile(file)
    ProcedureReturn #False
  Else
    ProcedureReturn #True
  EndIf
EndProcedure

CompilerIf #PB_Compiler_IsMainFile
  CompilerIf #PB_Compiler_Processor= #PB_Processor_x86
    softname.s= "IsFileOpen() [x86]"
  CompilerElse
    softname.s= "IsFileOpen() [x64]"
  CompilerEndIf
  
  ;start check
    mifile.s= "C:\Users\Usuario\Desktop\Proy\TEST\test.txt"
    CreateFile(0,mifile)
    WriteStringN(0,"wtf?")
    
    Debug IsFileOpen(mifile) ;check if file is open by other program
    
    MessageRequester(softname,"File must be open by CreateFile(0,mifile) or other external program. Then return 1")
    
    CloseFile(0)
    
    Debug IsFileOpen(mifile)
    MessageRequester(softname,"File was closed. Now return 0")

CompilerEndIf

Evolution?
Ice age, stone age, iron age, bronze age, industrial age, modern, age, stupidity age..
:)
If translation=Error: reply="Sorry, Im Spanish": Endif
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: IsFileOpen (by other program?) [Multi.OS][x86,x64]

Post by Little John »

User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: IsFileOpen (by other program?) [Multi.OS][x86,x64]

Post by Sicro »

Under Windows XP: Both codes work.
Under Linux: Both codes don't work.

I have created a new code that should work under Windows, Linux and Mac: IsFileOpen.pbi
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
minimy
Enthusiast
Enthusiast
Posts: 349
Joined: Mon Jul 08, 2013 8:43 pm

Re: IsFileOpen (by other program?) [Multi.OS][x86,x64]

Post by minimy »

Hey, yes LJ your code work! nice!
When i loocking some thing similar no found your code.. good job dude! Thanks!! :wink:

Goof job to kiffi original autor. Nice date 2011 12:11 pm.. like good wine!!

Sicro.. about your code contribution.. Thanks!.. good job!.. I like your license.. Really is amazing! :mrgreen: But Kiffi did it before you [2011 12:11 pm].. keep your medals!! :lol:

Code: Select all

; MIT License
; 
; Copyright (c) 2019 Sicro
; 
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
; 
; The above copyright notice and this permission notice shall be included in all
; copies or substantial portions of the Software.
; 
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
; SOFTWARE.
I prefer my waranty license.. LOL :mrgreen: is more fun!

Code: Select all

;         garanty : three steps ;-)
;         worktime: two beers..  __.oO
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
minimy
Enthusiast
Enthusiast
Posts: 349
Joined: Mon Jul 08, 2013 8:43 pm

Re: IsFileOpen (by other program?) [Multi.OS][x86,x64]

Post by minimy »

Hey again, i need do a program to encode streams and is really important have a good detection of final stream signal. Ok.. when i use my code work as i expect. But when use the RenameFile() option no work. I use Win 10 x64.. weird thing..
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: IsFileOpen (by other program?) [Multi.OS][x86,x64]

Post by Sicro »

minimy wrote:I like your license.. Really is amazing! :mrgreen:
It is a popular open source license that offers a lot of freedom and is compatible with many countries.
https://choosealicense.com/licenses/mit/
https://choosealicense.com/no-permission/
But yes, the code is possibly not creative enough so that I have a copyright on the code. In that case, my license can just be ignored and everything is fine.
minimy wrote:But Kiffi did it before you [2011 12:11 pm].. keep your medals!! :lol:
Ideas are not protected without a patent and I only use the native PB command RenameFile() from Kiffi's code and Kiffi can't have copyright on this single command.
Additionally, in my code I have a link to his forum post to honor his idea.
minimy wrote:I prefer my waranty license.. LOL :mrgreen: is more fun!

Code: Select all

;         garanty : three steps ;-)
;         worktime: two beers..  __.oO
It's not a license, but this in your first post:
minimy wrote:feel free to improve or modify..
This is also worth to be mentioned here:
luis wrote: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
Source: viewtopic.php?p=398079#p398079

The file could be opened again by a program, if you execute a code inside the If block.
minimy wrote:But when use the RenameFile() option no work. I use Win 10 x64.
Kiffi's code doesn't work on your operating system?
minimy wrote:i need do a program to encode streams and is really important have a good detection of final stream signal.
We need more information, such as whether the encoding program terminates after the task.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Post Reply