Solved

Is file currently open?

Posted on 1998-07-02
4
157 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
  • 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel Vlookup to move data back to source. 4 90
to transfer string from C lanaguage to VBA 4 64
Access query that references subform 5 47
Added a column screws up code 5 57
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

770 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