Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Testing file usage

Status
Not open for further replies.

jcmercure

Programmer
Oct 22, 2003
20
US
Hello -

I have a VB6 utility that I am writing that cleans up files that are only needed temporarily. The utility is scheduled to run every two minutes although we would prefer it run more frequently.

The problem is, when the utility runs, any given file may be in use and therefore can't be deleted.

I am currently attempting to delete the file and trap any error that occurs, making the assumption that a lock/permission issue exists if the file is in use. In such cases, I loop a number of times until the file can be deleted and failing that I leave the file to be deleted later.

Most often this works fine, but in some cases the file seems to be deleted while the file is in use. This doesn't make sense to me but it is happening.

So my question is, is ther a better way to test if the file is in use. Maybe an API call?

Thank you.

- John Mercure
 
Could it be that whatever application that is creating the temporary files is also deleting them in some cases?
 
TomKane -

No, the process tha creates the files is definately not deleting them. It is not running at this point, but other process may be. These process also do not delete the files, only my process is doing this.

- John
 
Make sure the other program has the file open for exclusive access. If they have it open then if you try something like: Kill "C:\Test.txt" then it should throw an error 70 (Permission Denied) or something like that.

If they are opening text files for reading or writing then they should be opening them something like this:

Open "C:\Test.txt" For Output Lock Write As #1
Open "C:\Test.txt" For Input Lock Write As #1

Swi
 
SWI -

Error 70 is indeed what I get when the utiltiy works as expected. I just explain why the other times the files get deleted.

I have no control over the other processes and therefore can't how the files are opened by them. They are 3rd party applications.

- John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top