|
-
Jan 2nd, 2011, 05:44 PM
#1
Thread Starter
Junior Member
[RESOLVED] Check to see if multiple files exist
I want to check if all the files needed for my program exist where they should but I'm having some trouble doing so. Here's the code I'm using:
Code:
Private Sub Form_Load()
Dim FileName(4) As String
FileName(0) = "file.bmp"
FileName(1) = "file2.bmp"
FileName(2) = "file3.bmp"
FileName(3) = "file4.bmp"
strPath = App.Path & "/files/"
For i = 0 To 3
If Dir$(strPath) <> FileName(i) Then
CheckPath = True
Else
CheckPath = False
End If
Next
If CheckPath = True Then
frmMain.Show
Else
MsgBox "One or more files are missing."
End If
End Sub
I've also tried a different method and it didn't work (listing all the file names in an if statement ( If Dir$(strPath) <> File or Dir$(strPath) <> File2...)
It should tell the user if a file (doesn't have to specify) is missing so if file2 and file3 exist, but file1 doesn't, it should say "One or more files are missing."
Last edited by Night_; Jan 2nd, 2011 at 05:59 PM.
-
Jan 2nd, 2011, 07:31 PM
#2
Re: Check to see if multiple files exist
Maybe case-sensitivity is in play
Code:
For i = 0 To 3
If StrComp(Dir$(strPath & FileName(i)), FileName(i), vbCompareText) <> 0 Then Exit For
Next
If i > 3 Then
frmMain.Show
Else
MsgBox "One or more files are missing."
End If
Also use Option Explicit. I don't see any variables declared, except FileName
Edited: Normally I say don't use forward slashes either, but the O/S is kind enough to allow both styles in most cases
Last edited by LaVolpe; Jan 3rd, 2011 at 10:25 AM.
-
Jan 2nd, 2011, 08:20 PM
#3
Re: Check to see if multiple files exist
Before you give up the ship, show the name of the file that is missing in the Message box. Then report back to the forum what appears.
-
Jan 2nd, 2011, 11:24 PM
#4
Thread Starter
Junior Member
Re: Check to see if multiple files exist
 Originally Posted by LaVolpe
Maybe case-sensitivity is in play
Code:
For i = 0 To 3
If StrComp(Dir$(strPath), FileName(i), vbCompareText) <> 0 Then Exit For
Next
If i > 3 Then
frmMain.Show
Else
MsgBox "One or more files are missing."
End If
Also use Option Explicit. I don't see any variables declared, except FileName
Edited: Normally I say don't use forward slashes either, but the O/S is kind enough to allow both styles in most cases
I had Option Explicit at the very top of the file and I had other variables declared in a module but I forgot to copy them with the code. I put FileName in the code instead of the module just to try something which didn't work out too well.
Anyways, unless I'm doing something wrong (considering it's almost midnight), your code doesn't work for me for some reason. It says "One or more files are missing." even if they're all there.
-
Jan 3rd, 2011, 08:32 AM
#5
Re: Check to see if multiple files exist
 Originally Posted by Night_
Anyways, unless I'm doing something wrong (considering it's almost midnight), your code doesn't work for me for some reason.  It says "One or more files are missing." even if they're all there.
You're either doing something wrong or ... When posting, show us your actual code. Pseudo code does no good other than frustrate and confuse those that are trying to help
-
Jan 3rd, 2011, 12:46 AM
#6
Re: Check to see if multiple files exist
If you're not interested in which file(s) may be missing then I'd modify your code
Code:
Private Sub Form_Load()
Dim FileName(4) As String
FileName(0) = "file.bmp"
FileName(1) = "file2.bmp"
FileName(2) = "file3.bmp"
FileName(3) = "file4.bmp"
strPath = App.Path & "\files\"
Do
If UCase(Dir$(strPath & FileName(i))) = UCase(FileName(i)) Then
CheckPath = True
i = i + 1
Else
CheckPath = False
End If
Loop Until i > 3 Or CheckPath = False
If CheckPath = True Then
frmMain.Show
Else
MsgBox "At least one File is missing."
End If
Unload Me
End Sub
Note
Code:
If UCase(Dir$(strPath & FileName(i))) = UCase(FileName(i)) Then
you need to check for each file specifically. The 'UCase(....)' makes the check independent of case.
EDIT: I see that FileName is defined as having 5 elements ( 0 to 4) but only 4 are used (0 to 3).
EDIT2:
If you wanted to report which files are missing (which, IMHO, is a better option) you could modify the code to this:
Code:
Private Sub Form_Load()
Dim strError As String
Dim CheckPath As Integer
Dim FileName(4) As String
FileName(0) = "file.bmp"
FileName(1) = "file2.bmp"
FileName(2) = "file3.bmp"
FileName(3) = "file4.bmp"
strPath = App.Path & "\files\"
Do
If UCase(Dir$(strPath & FileName(i))) = UCase(FileName(i)) Then
CheckPath = CheckPath + 1
Else
strError = strError & FileName(i) & vbCrLf
End If
i = i + 1
Loop Until i > 3
If CheckPath = 4 Then
frmMain.Show
Else
MsgBox "The Following File(s) are Missing From " & vbCrLf & _
strPath & vbCrLf & strError
End If
Unload Me
End Sub
Note that CheckPath is changed to an Integer variable and is the count of files found. If all 4 are found then frmMain is loaded, otherwise the MessageBox will display all those files not found (which are held in strError).
Last edited by Doogle; Jan 3rd, 2011 at 01:19 AM.
-
Jan 3rd, 2011, 09:43 AM
#7
Thread Starter
Junior Member
Re: Check to see if multiple files exist
 Originally Posted by Doogle
If you're not interested in which file(s) may be missing then I'd modify your code
EDIT2:
If you wanted to report which files are missing (which, IMHO, is a better option) you could modify the code to this:
Code:
Private Sub Form_Load()
Dim strError As String
Dim CheckPath As Integer
Dim FileName(4) As String
FileName(0) = "file.bmp"
FileName(1) = "file2.bmp"
FileName(2) = "file3.bmp"
FileName(3) = "file4.bmp"
strPath = App.Path & "\files\"
Do
If UCase(Dir$(strPath & FileName(i))) = UCase(FileName(i)) Then
CheckPath = CheckPath + 1
Else
strError = strError & FileName(i) & vbCrLf
End If
i = i + 1
Loop Until i > 3
If CheckPath = 4 Then
frmMain.Show
Else
MsgBox "The Following File(s) are Missing From " & vbCrLf & _
strPath & vbCrLf & strError
End If
Unload Me
End Sub
Note that CheckPath is changed to an Integer variable and is the count of files found. If all 4 are found then frmMain is loaded, otherwise the MessageBox will display all those files not found (which are held in strError).
Thanks, that works great.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|