greetings all....
I am in the process of trying to figure out if a file is open before i attempt to access it.
from the dictionary, I see that openfiles is what I want to use; however, I am not sure how to apply it to files that was not opened from LC.
could someone point me in the right direction?
thank you in advance.
check to see if file is open before opening
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
check to see if file is open before opening
- Rich
LiveCode 4.6.4
Dell Latitude E6400 running Windows XP SP3 / Mac Pro, Macbook Pro & Mac Mini running OS X 10.6.4
LiveCode 4.6.4
Dell Latitude E6400 running Windows XP SP3 / Mac Pro, Macbook Pro & Mac Mini running OS X 10.6.4
Re: check to see if file is open before opening
You can do it by checking for errors:
open file "notMyFile.txt"
if the result is not empty then -- couldn't open it
... do whatever you need
end if
Or you can use the "try" structure:
try
open file "notMyFile.txt"
catch tErr
answer tErr -- or whatever
end try
I don't know of any native way to check the file's status without first trying to open it. There's probably a shell command that would do it though.
open file "notMyFile.txt"
if the result is not empty then -- couldn't open it
... do whatever you need
end if
Or you can use the "try" structure:
try
open file "notMyFile.txt"
catch tErr
answer tErr -- or whatever
end try
I don't know of any native way to check the file's status without first trying to open it. There's probably a shell command that would do it though.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: check to see if file is open before opening
Hi Jacque,
I couldn't get your suggestions to work. However, the openFiles function contains a list of files that are currently open in LiveCode.
Best regards,
Mark
I couldn't get your suggestions to work. However, the openFiles function contains a list of files that are currently open in LiveCode.
Best regards,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
Re: check to see if file is open before opening
The OP wants to know if a file is open that has not been opened in LC. The only way I know to do that is to try and open it. If it is already open by another app, and if the original app has locked it in use, the result will contain "can't open file". Some files are opened by apps without a lock; those will open sucessfully in LC. BBEdit, for example, allows other apps to open the same file it already has open.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
-
- VIP Livecode Opensource Backer
- Posts: 10043
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: check to see if file is open before opening
In recent years I've started making good use of LiveCode's sysError function to get more specific info about file I/O errors than LiveCode's result provides by itself, e.g.:jacque wrote:The OP wants to know if a file is open that has not been opened in LC. The only way I know to do that is to try and open it. If it is already open by another app, and if the original app has locked it in use, the result will contain "can't open file".
Code: Select all
open file tMyFile
if the result is not empty then
answer the result &" (" & sysError() &")"
end if
The really diligent programmer can include a lookup table of OS codes to provide more specific feedback if needed, or to provide additional options to the user based on the specific circumstances.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn