Verified if file is open or close

Good evening group. I have the Path of a file … can I check if it is open?

I assume you mean if it is currently opened by some other app/process? No, not directly. You can check if it is locked (FolderItem.Locked), which probably means it’s opened by another app, but not guaranteed to be the case.

The only reliable way I know is to search the running processes to see if any have a hook to the file. How you do this varies with platform and is not something Xojo does natively (will need to use APIs or shell statements).

lsof on macOS and Linux

not sure about windows
anyone ?

edit : something like

 lsof /Users/npalardy/Desktop/fastreadproject.sqlite 

COMMAND     PID     USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME
TablePlus 86426 npalardy   11u   REG    1,7    24576 29678601 /Users/npalardy/Desktop/fastreadproject.sqlite

1 Like

Var f As FolderItem
Var dlg As New SaveFileDialog
Dim tisx As TextOutputStream
.
.
.
f = dlg.ShowModal

tisx = f.CreateTextFile

tisx.WriteLine("") ‘’–<<< Error if file is opened

My problem is that when I save the file, it tells me that it exists and if I want to overwrite it … but if the file is open, the first WriteLine gives me an error.

you can catch that error and tell whoever is using the program that some other app is using the file so you cant overwrite it
Something like

Var f As FolderItem
Var dlg As New SaveFileDialog
Dim tisx As TextOutputStream
.
.
.
f = dlg.ShowModal
try
   tisx = f.CreateTextFile
 
   tisx.WriteLine("") ‘’–<<< Error if file is opened
catch fox as IOException
    msgbox "The file is in use and cant be overwritten"
end try

a command like lsof on macOS and Linux can tell you WHICH program has the file open

I use Windows. I tried but it doesn’t catch the error.

We answered your initial question, giving you some things to look at, but then you just come back whining that your unchanged code doesn’t work. Did you even look into our suggestions? If you can’t overwrite it, then it’s definitely locked, which “If f.Locked” would have told you.

It will if you resume the debug. The default behaivor of a debug run is to “Break on Exceptions”, which is what it’s doing here. Click the play/resume button and you’ll see it catch the exception.

I am not an expert programmer, things that may be trivial for you, are not for me. I tried with the LOCKED command but it still doesn’t work. I decided to post the code to try to figure out where I’m wrong …

Including this information would have been helpful for us.

What exactly did you try with the Locked command? It should have been something like so:

.
.
f = dlg.ShowModal
If f.Locked Then
 MessageBox "File is open in another application and can not be overwritten"
 Return
End If

yes I wrote exactly like this, I thought it worked, but it always tells me that the file is closed and therefore, when I go to write, the error returns because it was open instead. I tried to catch the event also with Try … Catch and As NilObjectException and npalardy’s suggestions, but it doesn’t work.

f = dlg.ShowModal

if f.Locked then
messagebox " fiLE open " + f.NativePath
return
else
messagebox " fil closed" + f.NativePath
end if

tisx = f.CreateTextFile

tisx.WriteLine("") ‘’’’ ERROR

press resume
:slight_smile:

the debugger stops there to because you have “break on exceptions” set to true in the project menu
this is a good thing so you can then watch in the debugger as you step through your code

unfortunately I’m pretty sure LOCKED is testing the macOS Finder LOCKED attribute (not sure what the counterpart on Windows is)

I dont think it checks if a file is opened for exclusive access by another process
While both will result in a file that you cant overwrite they have very different causes

I like this one! :grin:

1 Like

Which means you need to provide more details as to what you’re trying to accomplish, otherwise we just assume you have a valid reason for asking a particular question.

An error does not necessarily mean the file is open. It could be something else. Do you know that this file is open in another app? Is this just on your system? Are you creating this situation on purpose just so you can test how to handle it? If alll the answers is no, then the file being open may not actually be the issue, and all our advice so far means nothing.

Start by figuring out why your TextOutputStream (tisx) is nil.

As I wrote before, the error gives itself when the file I am going to write is open. If the file is closed, it creates another one and asks me to overwrite it … when it’s open, it asks me to overwrite it but can’t do it because it’s already open

Mr Chaywesley ho provato a seguire il tuo suggerimento, ho scritto cosi e funziona…(ora mi devi spiegare il perchè). Con questo codice, se il file è chiuso lo sovrascrive, se invece è aperto mi avvisa che è aperto e RETURN senza scrivere nulla, dandomi la possibilità di chiudererlo.

if tisx=NIL then
messagebox "File alredy Open "
Return
else
'messagebox " OK, i’m write the file "
end if

Google Translate to:
Mr Chaywesley I tried to follow your suggestion, I wrote this and it works … (now you have to explain to me why). With this code, if the file is closed it overwrites it, if it is open it warns me that it is open and RETURN without writing anything, giving me the possibility to close it.

NB: wrinting nothing is a bad idea:


tisx.WriteLine("")

If it works, you only do not have an error or a crash, but you do not know if something was written to the file.

Also, using the Documentation code (when it exists) is a good idea.

I am happy your code works now.