Testing files on Windows are accesible

Help
2014-03-18
2014-03-18
  • Eric Davidson
    Eric Davidson
    2014-03-18

    On Windows 7 professional 64 bit, when a CSV file is opened by Excel it prevents anything else from reading the file. I'm trying to test for this situation, but the .file~new(inFileName)~canRead returns a True regardless of whether the file is open or not ( I also tried the ~canWrite method with the same result). Is there anything else I can test to see if a file is locked?

    This little read method is returns an Array of 0 items when the file is open in Excel
    ::Class READFile Public
    ::Method init
    ::attribute errMsg
    ::attribute RC
    ::Method read
    use strict arg inFileName
    self~RC = 0
    if ¬sysFileExists(inFileName) & .file~new(inFileName)~canRead -- make sure the file exists
    then do
    self~errMsg = "File" inFileName "does not exist or cannot be read - job abandoned"
    self~RC = 99
    Return ""
    end

    fi=.stream~new(inFileName)
    InDataArray = fi~arrayin -- read stream to array
    if RC <> 0
    then do
    self~RC = RC
    self~errMsg = "File" inFileName "unable to read - job abandoned"
    Return ""
    end
    fi~close
    drop fi -- release any storage held
    Return inDataArray

     
    • Mark Miesfeld
      Mark Miesfeld
      2014-03-18

      ERROR! The markdown supplied could not be parsed correctly. Did you forget to surround a code snippet with "~~~~"?

      The canRead and canWrite method are going to test the file attributes,
      which have nothing to do with the file being locked for reading or writing
      by another application.
      
      I don't think there is anything in ooRexx that can test if a file is locked
      by another application.
      
      --
      Mark Miesfeld
      
      
      
      On Tue, Mar 18, 2014 at 10:26 AM, Eric Davidson <wee-eck@users.sf.net>wrote:
      
      > On Windows 7 professional 64 bit, when a CSV file is opened by Excel it
      > prevents anything else from reading the file. I'm trying to test for this
      > situation, but the .file~new(inFileName)~canRead returns a True regardless
      > of whether the file is open or not ( I also tried the ~canWrite method with
      > the same result). Is there anything else I can test to see if a file is
      > locked?
      >
      > This little read method is returns an Array of 0 items when the file is
      > open in Excel
      > ::Class READFile Public
      > ::Method init
      > ::attribute errMsg
      > ::attribute RC
      > ::Method read
      >    use strict arg inFileName
      >    self~RC = 0
      >    if ¬sysFileExists(inFileName) & .file~new(inFileName)~canRead       --
      > make sure the file exists
      >       then do
      >          self~errMsg = "File" inFileName "does not exist or cannot be read
      > - job abandoned"
      >          self~RC = 99
      >          Return ""
      >       end
      >
      >    fi=.stream~new(inFileName)
      >    InDataArray = fi~arrayin            -- read stream to array
      >    if RC <> 0
      >       then do
      >          self~RC = RC
      >          self~errMsg = "File" inFileName "unable to read - job abandoned"
      >          Return ""
      >       end
      >    fi~close
      >    drop fi                             -- release any storage held
      > Return inDataArray
      >
      >
      > ---
      >
      > [Testing files on Windows are accesible](
      > https://sourceforge.net/p/oorexx/discussion/408478/thread/3a88d927/?limit=25#3134
      > )
      >
      >
      > ---
      >
      > Sent from sourceforge.net because you indicated interest in <
      > https://sourceforge.net/p/oorexx/discussion/408478/>
      >
      > To unsubscribe from further messages, please visit <
      > https://sourceforge.net/auth/subscriptions/>
      
       
      • Rick McGuire
        Rick McGuire
        2014-03-18

        ERROR! The markdown supplied could not be parsed correctly. Did you forget to surround a code snippet with "~~~~"?

        Well, doing an explicit open and testing the result of the open will
        certainly tell you that.
        
        Rick
        
        
        On Tue, Mar 18, 2014 at 1:39 PM, Mark Miesfeld <miesfeld@users.sf.net>wrote:
        
        > The canRead and canWrite method are going to test the file attributes,
        > which have nothing to do with the file being locked for reading or writing
        > by another application.
        >
        > I don't think there is anything in ooRexx that can test if a file is locked
        > by another application.
        >
        > --
        > Mark Miesfeld
        >
        >
        >
        > On Tue, Mar 18, 2014 at 10:26 AM, Eric Davidson <wee-eck@users.sf.net
        > >wrote:
        >
        > > On Windows 7 professional 64 bit, when a CSV file is opened by Excel it
        > > prevents anything else from reading the file. I'm trying to test for this
        > > situation, but the .file~new(inFileName)~canRead returns a True
        > regardless
        > > of whether the file is open or not ( I also tried the ~canWrite method
        > with
        > > the same result). Is there anything else I can test to see if a file is
        > > locked?
        > >
        > > This little read method is returns an Array of 0 items when the file is
        > > open in Excel
        > > ::Class READFile Public
        > > ::Method init
        > > ::attribute errMsg
        > > ::attribute RC
        > > ::Method read
        > >    use strict arg inFileName
        > >    self~RC = 0
        > >    if ¬sysFileExists(inFileName) & .file~new(inFileName)~canRead       --
        > > make sure the file exists
        > >       then do
        > >          self~errMsg = "File" inFileName "does not exist or cannot be
        > read
        > > - job abandoned"
        > >          self~RC = 99
        > >          Return ""
        > >       end
        > >
        > >    fi=.stream~new(inFileName)
        > >    InDataArray = fi~arrayin            -- read stream to array
        > >    if RC <> 0
        > >       then do
        > >          self~RC = RC
        > >          self~errMsg = "File" inFileName "unable to read - job abandoned"
        > >          Return ""
        > >       end
        > >    fi~close
        > >    drop fi                             -- release any storage held
        > > Return inDataArray
        > >
        > >
        > > ---
        > >
        > > [Testing files on Windows are accesible](
        > >
        > https://sourceforge.net/p/oorexx/discussion/408478/thread/3a88d927/?limit=25#3134
        > > )
        > >
        > >
        > > ---
        > >
        > > Sent from sourceforge.net because you indicated interest in <
        > > https://sourceforge.net/p/oorexx/discussion/408478/>
        > >
        > > To unsubscribe from further messages, please visit <
        > > https://sourceforge.net/auth/subscriptions/>
        >
        >
        >
        > ---
        >
        > [Testing files on Windows are accesible](
        > http://sourceforge.net/p/oorexx/discussion/408478/thread/3a88d927/?limit=25#3134/6aff
        > )
        >
        >
        > ---
        >
        > Sent from sourceforge.net because you indicated interest in <
        > https://sourceforge.net/p/oorexx/discussion/408478/>
        >
        > To unsubscribe from further messages, please visit <
        > https://sourceforge.net/auth/subscriptions/>
        >
        
         
        • Eric Davidson
          Eric Davidson
          2014-03-18

          Thanks for your swift response, sorry for the code issue - it was comments that had split across multiple lines in the window. Hopefully now corrected.

          I could test that there were items in the returned array - if I knew that the file must not be empty