Solved

Is file currently open?

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now