[RESOLVED] How to check if a named file exists on a machine
I want my program to start by checking that a number of files, of known location and extension type(s) is present on the PC before going on to run the app. These files will be the associated dependency files, plus certain others which previous instances of the program may have generated. Their extensions could be anything, including .dll, .ocx., .exe, .txt, .doc etc.
If a listed file is not present, I will use a message box to inform the user. My thought is to incorporate this code at the form load event. The message(s) will prompt the user to load the necessary files.
The following code seems to work so far. It requires placing a rich text box on the form (which can be small, and non-visible) and trying to load the desired file into it. If the file is not present, the On Error process brings up the Message Box :-
Dim PATH as String
PATH = "C:\Windows\System\abcde.xyz"
On Error GoTo Line10
RichTextBox1.LoadFile PATH, rtfText
GoTo Line20
Line10: MsgBox "FILE OPEN ERROR. CHECK PATH " & PATH
Line20:
(progam then continues to check for next file etc. etc. ........)
Is there please a more elegant way to achieve this, and one which will work with absolutely any extension type? I have not yet found an extension type which the above code does not deal with, but maybe there will be a few?
camoore
Wales, UK
Re: How to check if a named file exists on a machine
See if this helps you :)
http://www.vbforums.com/showthread.php?t=349990
Also one more way which is not mentioned in that thread...
vb Code:
Function DoesExist(FileName As String) As Boolean
On Error GoTo Error
'~~> Get the "FileName" attributes, and make sure what
'~~> "FileName" isnt a directory
DoesExist = (GetAttr(FileName) And vbDirectory) = 0
Error:
'~~> Return False if an error occurs
DoesExist = False
End Function
Re: How to check if a named file exists on a machine
Thank you very much, Koolsid, for your such rapid reply and for the pointer. I was sure there must be a more elegant method than mine. However, I did find yet another use for the rich text box. This seems a most useful device about which there is very little mention in all my VB textbooks.
I will mark the thread closed.
camoore
Wales, UK