Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Test for file open by another app ? 1

Status
Not open for further replies.

DMichaels

Mechanical
Mar 5, 2002
22
0
0
US
Is there an easy way to test to see if a file is currently open by another application ? I am working on a VB project that requires the file to not be open before I access it. (I am using DSOFile (from Microsoft) to access document properties)
 
Replies continue below

Recommended for you

Hi,
This is not a Professional solution !.
You know that if a file is already opened, it could not be deleted. So you can copy the file to a temporary one and try deleting the original file , if the delete operation failed, this mean that the file is used by another app. , if delete success rename the Temp. file to real name and countinue working on it .
 
DimSH;
I want to thank you for the tip. Nobody else was willing to try this one, but you were ! Although I think it's a little risky to be deleting Soldworks files while someone has them open, you were the only one that responded. Thanks again.
Don Michaels
 
You could try this:

on error resume next
Open "MYFILE.TXT" For Output Lock Read Write As #iHandle
if err.number <> 0 then
'THE FILE IS IN USE
else
CLOSE #iHandle
KILL file.txt&quot;
endif
on error goto 0

What the above snippet does is to try an lock a file. This will fail if the fail is already locked by another application. You need to keep in mind that some applications don't lock the files, so there is no indication that the file is in use.


Troy Williams
fenris@hotmail.com
 
If you are opening the file:
Open the file and check to see if it's Read-Only.
DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
Just go ahead and kill the file and trap the error if it happens:

--Start of snippit

Public Sub RemoveFile (strFileName as String)
On error goto fileError
Kill strFileName
goto hurrah

fileError:
if err.number = 75 then
'file/path error - probably in use

else
'something else went kaka-kookoo, mebbe we'll throw
' it back up the call stack and make it someone
' elses problem
err.Raise err.Number, err.Source, _
err.Description, err.HelpFile, err.HelpContext
end if
err.clear

hurrah:

end sub
---END of snippit

No true coder likes goto statements, but Uncle Bill makes us use 'em anyways. If the file isn't locked, then the app doesn't really have it 'open'. It just read it, and will probably re-write it completely on a save.
 
Status
Not open for further replies.
Back
Top