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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why won't this work????

Status
Not open for further replies.

jgoodman00

Programmer
Jan 23, 2001
1,510
I am using the following function to check if a file exists:

Function FileExists(strFile as String) as Boolean
FileExists = Dir(strFile) <> &quot;&quot;
End Function

Public FileLocation as Integer

Then within the startup module of my app, i have the following code:

Private Sub Form_Load()

If FileExists (&quot;D:\appname.exe&quot;) = True Then
FileLocation = 1
ElseIf FileExists (&quot;E:\appname.exe&quot;) = True Then
FileLocation = 2
ElseIf FileExists (&quot;F:\appname.exe&quot;) = True Then
FileLocation = 3
Else
MsgBox &quot;The CD cannot be found!!&quot;, VBCritical
End Sub


Does anyone have any idea where this is going wrong. If it returns a false on the first part of the statement, it successfully goes onto the second statement. However at this point it breaks out when it runs the FileExists function again, saying runtime error 52, bad filename or number!!! Help me please.... James Goodman
j.goodman00@btinternet.com
 
Well, I would have expected a different error (on my system I get 68), but maybe it's because there is no device with the letter &quot;E&quot;, or it's not a disk, CD-ROM drive, etc. Dir() requires that the device specified in the path exist, and it has to have a disk-type filing system.

We might be getting different error messages because of different operating systems. Rick Sprague
 
it seems really freaky, because if i were to insert the correct cd in the e drive, it will run no problem. If i then insert it into the f drive it returns this error. However, if i swap the order in which they occur (ie make the second elseif statement the f drive one) it will run on the f drive & not the e drive....This seems really strange. I was thinking it could be to do with it holding the FileExists variable to false & causing the error on the second statement, but watching the code indicates it does not. ARGGHHH, this looks like one of those really annoying problems which for no logical reason won't work....GRRR computers!!! James Goodman
j.goodman00@btinternet.com
 
There are no computer problems that aren't logical if you know enough detail. This isn't really all that obscure. Obviously, if you attempt to use Dir() with a file path that refers to an empty CD-ROM drive, you get the error. I revised my copy to include only drives that exist, including my CD-ROM drive. If the logic gets to testing that drive--in whichever part of the compound If statement--and the drive is empty, I get a &quot;Path not found&quot; error. You probably get a different error because you're using a different Windows version.

Clearly, what you need is to detect whether a drive is ready before you use Dir() on it. One easy way is to use the Microsoft Scripting Runtime library. If you can find this on your References list, check the box and click OK. You can then determine if a drive is ready with code like the following:

Function FileExists(strFile As String) As Boolean
Dim fs As Scripting.FileSystemObject
Dim d As Scripting.Drive

Set fs = New Scripting.FileSystemObject
Set d = fs.GetDrive(Left(strFile, 2))
If d.IsReady Then
FileExists = Dir(strFile) <> &quot;&quot;
Else
FileExists = False
End If
End Function

If you want the GetDrive method to work with a network UNC path, you'll have to modify its parameter, but I doubt you want that. Rick Sprague
 
i have found a method which works perfectly it would appear, but it is somewhat more complicated...Anyway here is the code i found to make it work (there is no way i wrote this!!!) :)

Public FileLocation As Integer



' Declare call to comdlg32.dll to open the common file dialog
Declare Function GetOpenFileName Lib &quot;comdlg32.dll&quot; Alias _
&quot;GetOpenFileNameA&quot; (pOpenfilename As OPENFILENAME) As Long

' Declare object passed to common dialog
Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

' Declare call to kernel32.exe to use the openfile to check file
' existence ( Access DIR function can fail when using UNC for server/share names )
Declare Function OpenFile Lib &quot;kernel32&quot; (ByVal lpFileName As String, _
lpReOpenBuff As OFSTRUCT, _
ByVal wStyle As Long) As Long

' Declare constants for passing to openfile
Public Const OFS_MAXPATHNAME = 128
Public Const OF_EXIST = &H4000

' Declare object used in openfile function
Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type

Function GetOpenFile(ByVal IN_INITIALDIR As String) As String

Dim OpenFile As OPENFILENAME
Dim LReturn As Long
Dim sFilter As String
Dim F_FILENAME As String

F_FILENAME = &quot;&quot;

OpenFile.lStructSize = Len(OpenFile)
OpenFile.hwndOwner = Screen.ActiveForm.hWnd
OpenFile.hInstance = Application.hWndAccessApp
sFilter = &quot;JPEG Files (*.jpg)&quot; & Chr(0) & &quot;*.JPG&quot; & Chr(0) & _
&quot;TIFF Files (*.tif)&quot; & Chr(0) & &quot;*.TIF&quot; & Chr(0) & _
&quot;Bitmap Files (*.bmp)&quot; & Chr(0) & &quot;*.BMP&quot; & Chr(0)
OpenFile.lpstrFilter = sFilter
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = String(255, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
OpenFile.lpstrFileTitle = OpenFile.lpstrFile
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = &quot;C:\&quot;
OpenFile.lpstrTitle = &quot;Find the document image file&quot;
OpenFile.flags = 0

If IN_INITIALDIR <> &quot;&quot; Then
OpenFile.lpstrInitialDir = IN_INITIALDIR
Else
OpenFile.lpstrInitialDir = &quot;C:\&quot;
End If

LReturn = GetOpenFileName(OpenFile)

If LReturn <> 0 Then
F_FILENAME = Left$(OpenFile.lpstrFile, InStr(OpenFile.lpstrFile, Chr(0)) - 1)
F_FILENAME = Trim(F_FILENAME)
End If

GetOpenFile = F_FILENAME

End Function
Function CheckFileExists(ByVal IN_FILENAME As String) As Integer

Dim iresult As Integer
Dim strucFname As OFSTRUCT
Dim strSearchFile As String

strSearchFile = IN_FILENAME

iresult = OpenFile(strSearchFile, strucFname, OF_EXIST)

'The above line causes OpenFile to search for the
'file Test on the server network path.
'Passing the OF_EXIST parameter tells the OpenFile
'function to search for the file, the file will not be
'opened or modified in any way.

CheckFileExists = iresult

End Function

Then the following code in the splash screen of my app:

Private Sub Form_Load()
On Error GoTo Form_Load_Err
lblVersion.Caption = &quot;Version &quot; & App.Major & &quot;.&quot; & App.Minor & &quot;.&quot; & App.Revision
lblProductName.Caption = App.Title

If CheckFileExists(&quot;d:\appname.exe&quot;) = 1 Then
FileLocation = 1
ElseIf CheckFileExists(&quot;e:\appname.exe&quot;) = 1 Then
FileLocation = 2
ElseIf CheckFileExists(&quot;f:\appname.exe&quot;) = 1 Then
FileLocation = 3
Else
MsgBox &quot;The CD could not be found. Please make sure it is inserted & try again&quot;, vbCritical
Unload Me
End If
Form_Load_Exit:
Exit Sub

Hopefully someone else will find this helpful......
James Goodman
j.goodman00@btinternet.com
 
Seems a bit complex just to check for the existence of a file. Why couldn't you:[tt]
Function FileExists(strFile as String) as Boolean
On Error GoTo InvalidSpec
If Dir(strFile) = &quot;&quot; Then
FileExists = False
Else
FileExists = True
End If
Exit Function
InvalidSpec:

'If strFile points to an invalid drive or folder,
'it's obvious that the file can't be accessed.
FileExists = False
End Function[/tt]


VCA.gif

Alt255@Vorpalcom.Intranets.com
 
I have to agree with Alt255 on this one . . . the error you were getting was probably caused by trying to read a CDROM drive that had no CDROM in it. Checking with DIR command and comparing the results with an empty string and then including an error trap in case the media does not exist seems to be the best solution.

Also, regarding the same problem, there is a faster wayt to compare the strings.(Note that this will only help if you are looping through a lot of checks . . . just changing it for 1 call will make almost no difference.) If you replace -

Dir(strFile) = &quot;&quot;

with -

Len(Trim$(Dir$(strFileName))) = 0

it will run each check over 6 times faster. This is because you are only trying to compare the length of the return rather that forcing the computer to compare each character. I know that it looks odd, but try it out some day . . . it really is faster. But as I said before, it will only make a real difference in your application if you are looping and performing the same check over and over.

 
Private Declare Function GetDriveType Lib &quot;kernel32&quot; Alias &quot;GetDriveTypeA&quot;
(ByVal nDrive As String) As Long

Private Sub Command1_Click()
On Error Resume Next
Dim lngDriveType As Long
Dim strDrive As String
Dim strFile As String
Dim blnCdFound As Boolean
blnCdFound = False
strFile = &quot;\CD\CD_OPEN.EXE&quot;
For i = Asc(&quot;a&quot;) To Asc(&quot;z&quot;)
lngDriveType = GetDriveType(Chr(i) & &quot;:&quot;)
If lngDriveType = 5 Then 'cd-drive
strDrive = Chr(i) & &quot;:&quot;
strRet = Dir(strDrive & strFile)
If Err.Number > 0 Then
strRet = &quot;&quot;
Err.Clear
End If
If strRet <> &quot;&quot; Then
'cd gevonden
blnCdFound = True
Exit For
End If
End If
Next i
If blnCdFound Then
ret = Shell(strDrive & strFile, vbNormalFocus)
Else
MsgBox &quot;Leg de juiste cd in 1 van de cd drives&quot;
End If
End Sub
Eric De Decker
vbg.be@vbgroup.nl

Licence And Copy Protection AxtiveX.

Download Demo version on my Site:
Promotions before 02/28/2001 (free source codebook),visite my site
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top