Solved

Opening a File to Read (Allready Open By Another Program)

Posted on 2003-11-13
11
304 Views
Last Modified: 2010-05-18
I am reading files from a directory , the files in this directory are being generated by another program (not written by me), I do not know what language it is written in. (but the program is pretty old)
This other program refreshes the same file name with new data, approx every 20 seconds.
The problem i am having is.. My vb program opens the file for input, reads the data and then closes. If i am in the middle of reading the file and the other program tries to rewrite the file, the other program bombs out with a system error.
Have tried.. renaming,copying etc (everything!!)  .. the file i need, but still strike the problem.
Am thinking i will have to look at the time of the file and calculate the safest time to read the file????

Is there any other way of overcoming the problem., other than the above idea

Appreciate any ideas!!!
0
Comment
Question by:turnerrob
  • 4
  • 2
  • 2
  • +1
11 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9743107
What function are you using to copy the file with?  You could try using the FileCopy API.

http://www.mentalis.org/apilist/CopyFile.shtml

Idle_Mind
0
 
LVL 11

Expert Comment

by:bingie
ID: 9743119
You mentioned copying, but did you copy the file to a new location and then read from the copy? You can copy the file, read the data and close the file, the other program shouldn't bomb this way.
0
 

Author Comment

by:turnerrob
ID: 9743146
I have tried using filecopy , and copy to another directory etc.
If i use filecopy and the other program tries to write over the top, it bombs out.
0
 
LVL 11

Accepted Solution

by:
bingie earned 43 total points
ID: 9743158
Copy the file:

  FileCopy strSource, strDest

      e.g.

  FileCopy "C:\temp\test.txt", "C:\test\test.txt"


2.  Then read from the file as you need

3.  Then delete the file
 
  Kill "strPath"

         
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 41 total points
ID: 9743168
It is very possible that the old program is attempting to open the file in an exclusive mode which is causing it to crash.

(Thinking)...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:turnerrob
ID: 9743181
I think this is the case, ie the old program is opening (exclusive)
0
 
LVL 7

Assisted Solution

by:_agj_
_agj_ earned 41 total points
ID: 9745299
hey...u cud find the filetime and check up how long it takes for the next update of the file. Using the data, u cud ezly get the safe time to
copy off.
0
 

Author Comment

by:turnerrob
ID: 9750554
_agj_
I agree this is my only option.
I only put the question, in case, there was some magic method of overcoming reading an (exclusive open) file.
Looks like there is none????
0
 
LVL 7

Expert Comment

by:_agj_
ID: 9761483
hey, if there were a way to read an exclusively open file, then wudnt it cease to be 'exclusive' :D
0
 

Author Comment

by:turnerrob
ID: 10168138
appreciate the help offered.
sorry i left the question open for so long
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Macro: print each sheet to pdf using ExportAsFixedFormat 11 41
Using "ScreenUpdating" 6 55
Copy a row 12 58
Search combo error "Data Type Mismatch in Criteria Expression" 2 52
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

895 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

14 Experts available now in Live!

Get 1:1 Help Now