Solved

Is file currently open?

Posted on 1998-07-02
4
173 Views
Last Modified: 2010-05-03
Is there a way of determining if a file is currently open? I need to know when a large data file copied to my machine across an ISDN line is complete. My application is VB3 and running under NT and I think FileLen() may return a file size before the file is complete.
0
Comment
Question by:mark_s
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
alamo earned 100 total points
ID: 1464784
To see if the file is done being written open it "Lock Read" with error trapping on. If the open fails you can detect it, wait a little bit, and try again. You also need to error-trap your first read of the file, since in some network environments opens succeed but don't actually acquire the lock until the file is accessed.

"Lock Read" should be enough but if that doesn't always work, try "Lock Read Write" - file locking can be a tricky thing sometimes.

Hope this helps!
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1464785
mark_s,
Try to open the file. If you get an error of 55, the file is already open. The code was used in VB3.
Regards
Dalin.

   On error goto errorTrap
   lifh = FreeFile ' get the next available free file handle
   Open TheFile For Input As lifh ' try and open it for reading
   Close lifh ' be sure to close it
   Exit Function ' or sub

ErrorTrap:
   Select Case Err
         
   Case 52  'bad file name or number
         
          Case 53  'file not found
           
          Case 55  'file already open
         
       
          Case 75  'path/file access error
         
          Case 76  'path not found
       
          Case Else ' something else happened - so just report it
       
   End Select

End Function/sub

0
 
LVL 6

Expert Comment

by:alamo
ID: 1464786
Error 55 means your own program has the file open, it won't be returned if some other process has it open.

The error you are most likely to get back if another process has the file locked is 70 Permission denied, however I have seen others on certain networks (such as 76 Path/File access error, which you wouldn't expect in a case like this).
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1464787
Thank you, alamo, for the clarification.
I was just thinking of file opened by the app itself. My mistake.
Sorry for mis-understand your question, mark_s.
Regards
Dalin


0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question