Results 1 to 9 of 9

Thread: Check for open file

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Location
    Ontario, Canada
    Posts
    658

    Check for open file

    Hello,

    Is there a way to verify if a file is already open?

    I know you can access a file with a file handle
    Code:
    IF FILE(cPath)		
       FileHandle = FOPEN(cPath, 1)
    ELSE  // If not create it.
       FileHandle = FCREATE(cPath) 
    ENDIF
    		
    IF FileHandle < 0     // Check for error opening file
       WAIT 'Cannot open or create output file.' WINDOW NOWAIT
    ENDIF
    The above just indicates that the file can't be opened but no reason is available.
    In order to provide more feedback to the user (or to suggest a course of action - such as closing the file), I want to determine if the user has the file open.

    Is this possible?

    Thanks,
    Cory

  2. #2
    Lianja Team chris.mavin's Avatar
    Join Date
    Feb 2012
    Location
    London UK
    Posts
    29
    Hi Cory,

    There is no operating system agnostic way to determine if a text file is already open.

  3. #3
    Lianja Team yvonne.milne's Avatar
    Join Date
    Feb 2012
    Location
    Berkshire, UK
    Posts
    1,713
    Hi Cory,

    Is it actually a specific file you need to open? Is it a scenario where you could create a temporary file (TMPNAM() will give you a temporary file name in the temporary files directory) so that multiple users aren't trying to access the same file?

    Regards,

    Yvonne

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Ontario, Canada
    Posts
    658
    Hi Chris,

    Thanks for the reply although I'm surprised this doesn't exist, especially since other languages seem to have the ability to check for open files.

    Hi Yvonne,

    It is a specific file that is being accessed for a report.
    This way the user can view it immediately after is created or whenever they want to.

    I have it set to reuse the report if it already exists so that only one version of the report exists.

    Cory

  5. #5
    Lianja Team yvonne.milne's Avatar
    Join Date
    Feb 2012
    Location
    Berkshire, UK
    Posts
    1,713
    Hi Cory,

    If it's the CSV report files we are talking here and the CSV is already open in Excel, the fopen() will return a filehandle of -1, ferror() will return -1, errno() will return 13 and strerror(errno()) will return 'Permission denied'. That doesn't tell you who has the file open, or whether the 'Permission denied' is due to another cause.

    Regards,

    Yvonne

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Ontario, Canada
    Posts
    658
    Hi Yvonne,

    Thank you for providing the return values for the different error functions.

    Yes, in this case it is related to the csv files but in general it could be any file.
    I don't think its wise to assume that a file is already open with only a FileHandle return of -1. As I understand it, the -1 only means that an error occurred when Lianja was trying to open the file. Couldn't it also mean that it doesn't exist, or is on another directory, or is in use by another application, etc.?

    Cory

  7. #7
    Hi Cory,

    On Linux there are tools like lsof and fuser that can give you pretty much any information you want about open files. I am aware of graphical tools that do the same for Windows (Process Explorer comes to mind) but off the top of my head I don't know of one that will do it on the command line.

    Dave

  8. #8
    Senior Member
    Join Date
    Jul 2013
    Location
    Ontario, Canada
    Posts
    658
    Hi Dave,

    I appreciate your feedback and the feedback received from everyone else.

    Cory

  9. #9
    Lianja Development Team barrymavin's Avatar
    Join Date
    Feb 2012
    Location
    UK, USA, Thailand
    Posts
    6,731
    Blog Entries
    18
    What Chris said was there is no operating system agnostic way of doing this.

    Also this has nothing to do with databases and tables. This is only to do with text files that you are creating programmatically.

    In Linux you can open a file up multiple times. This is a Linux feature. Nothing to do with Lianja. Windows will not allow a file to be opened shared unless specifically opened with sharing permissions. Obviously you don't want that when creating text files.

    The error messages returned as described by Yvonne are all you get from windows.

    You mention other languages do this and that. Well I don't know of any. If a file is currently open non shareable as is the case in point then you can't open it and "permission denied" refers to the "Incompatible attempt to open when already open in non shared mode".
    Principal developer of Lianja, Recital and other products

    Follow me on:

    Twitter: http://twitter.com/lianjaInc
    Facebook: http://www.facebook.com/LianjaInc
    LinkedIn: http://www.linkedin.com/in/barrymavin

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Journey into the Cloud
Join us