Results 1 to 7 of 7

Thread: [RESOLVED] Check to see if multiple files exist

Hybrid View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    23

    Resolved [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.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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.
    Doctor Ed

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    23

    Re: Check to see if multiple files exist

    Quote Originally Posted by LaVolpe View Post
    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.

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Check to see if multiple files exist

    Quote Originally Posted by Night_ View Post
    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    23

    Re: Check to see if multiple files exist

    Quote Originally Posted by Doogle View Post
    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
  •  



Click Here to Expand Forum to Full Width