+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Forum Contributor
    Join Date
    10-24-2008
    Location
    England
    Posts
    146

    Checking if any files in folder are in another

    Hi how can i look at all the .mpg files in a folder and check if any of them exist in another folder.

    ive got this code but i cant get it to work properly.

    If anybody could help

    Code:
    MsgBox "checking files that already exits"
    Dim MyFile As String, Sep As String
    Dim oFSO, sFileName
    Dim sDoc As String
    Dim sPath As String
    MyFile = Dir("D:\" & "*.mpg")
     
          Do While MyFile <> ""
     
    sPath = "F:\MediaLib\Video\"
    If (Dir(sPath & MyFile) > "") Then
    MsgBox "Videos on this disc have already been uploaded to this system"
    Exit Sub
    End If
             
             MyFile = Dir()
    
          Loop
     
    MsgBox "continue"
    Exit Sub
    Many Thanks

  2. #2
    Forum Guru mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    2,815

    Re: Checking if any files in folder are in another

    I suspect that the problem is that you are using the DIR function twice and the second is resetting it so that the DIR() ar the end of the first loop is not looking back into the first folder.

    Why not drop the filenames of the found files in the two folders into separate columns in a sheet and compare there?
    Martin

    Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.

    If my solution has saved you time and/or money, please consider donating to Cancer Research UK.

  3. #3
    Forum Contributor
    Join Date
    10-24-2008
    Location
    England
    Posts
    146

    Re: Checking if any files in folder are in another

    that is an option but i was hoping i could do this purely in vba. have you got any ideas how or if it can be done.

    Many thanks

  4. #4
    Forum Guru mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    2,815

    Re: Checking if any files in folder are in another

    How about

    Code:
    Sub Test()
    
    Dim Folder1Array()
    
    ReDim Folder1Array(0)
    
    MsgBox "checking files that already exits"
    Dim MyFile As String, Sep As String
    Dim oFSO, sFileName
    Dim sDoc As String
    Dim sPath As String
    Dim Uploaded As Boolean
    
    MyFile = Dir("D:\" & "*.mpg")
    Do While MyFile <> ""
        ReDim Preserve Folder1Array(UBound(Folder1Array) + 1)
        Folder1Array(UBound(Folder1Array)) = MyFile
        MyFile = Dir()
    Loop
    
    
    sPath = "F:\MediaLib\Video\"
    MyFile = Dir(sPath & "*.mpg")
    Uploaded = False
    Do While MyFile <> ""
        For N = 1 To UBound(Folder1Array)
            If Folder1Array(N) = MyFile Then
                Uploaded = True
                Exit Do
            End If
        Next N
    Loop
    If Uploaded = True Then
    MsgBox "Videos on this disc have already been uploaded to this system"
    End If
    End Sub
    Martin

    Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.

    If my solution has saved you time and/or money, please consider donating to Cancer Research UK.

  5. #5
    Forum Contributor
    Join Date
    10-24-2008
    Location
    England
    Posts
    146

    Re: Checking if any files in folder are in another

    Hi im using your code above. Which i thank you emensly for and it works if there is a file on the F:\MediaLib\Video\ named the same as on the d:\, but if there is a file in F:\MediaLib\Video\ that is not on the d:\. The whole application stops responding.

    Any ideas?


    Many Thanks for your help

  6. #6
    Forum Contributor
    Join Date
    10-24-2008
    Location
    England
    Posts
    146

    Re: Checking if any files in folder are in another

    Can nobody help me?.
    Please any advise would be much appreciated.

    Many Thanks

  7. #7
    Forum Guru mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    2,815

    Re: Checking if any files in folder are in another

    My fault - missed a line out

    Code:
    ....
    
    Do While MyFile <> ""
        For N = 1 To UBound(Folder1Array)
            If Folder1Array(N) = MyFile Then
                Uploaded = True
                Exit Do
            End If
        Next N
        MyFile = Dir() 'Add this line in order to force loop to look at next file
    Loop
    
    ......
    Martin

    Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.

    If my solution has saved you time and/or money, please consider donating to Cancer Research UK.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0