+ Reply to Thread
Results 1 to 7 of 7

Compare two folder files - whether they exit or not and if exit then copy to another foler

Hybrid View

  1. #1
    Registered User
    Join Date
    05-22-2011
    Location
    chennai
    MS-Off Ver
    Excel 2003
    Posts
    71

    Compare two folder files - whether they exit or not and if exit then copy to another foler

    Hi Everyone

    I'm trying find out whether one folder(folder 1) files are exist in another folder(folder 2) or not - if exits then copy that file to another folder(folder 2) - I tried but i'm getting some errors -
    Please me on the this

    Sub compare()
    Dim ofso As Object
    Set ofso = CreateObject("Scripting.FileSystemObject")
    Dim cSourceDir As Object
    'Dim cDestDir As Object
    Dim ofile As Object
    
    Set cSourceDir = ofso.GetFolder("C:\folder1")
    Const cDestDir = "C:\folder2\"
        
        Set oFolder = ofso.GetFolder(cSourceDir)
       
    
    Dim sdestpath As Object
       For Each ofile In oFolder.Files
             sdestpath = Replace(ofile.Path, cSourceDir, cDestDir, 1, -1, 1)
          If Not ofso.FileExists(sdestpath) Then
             MsgBox ("File missing = " & sdestpath)
              FileCopy "C:\folder1\" & ofile, "C:\folder2\" & ofile
             Wscript.Quit
          End If
       Next
     
       'For Each oSubFolder In oFolder.SubFolders
       '   ScanFolder (oSubFolder)
       'Next
    
    End Sub
    
    Thanks
    Thiru

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Compare two folder files - whether they exit or not and if exit then copy to another f

    Try this...

    Sub compare()
        
        Dim ofso   As Object
        Dim ofile  As Object
        Const cSorcDir As String = "C:\folder1\"
        Const cDestDir As String = "C:\folder2\"
        
        Set ofso = CreateObject("Scripting.FileSystemObject")
        
        For Each ofile In ofso.GetFolder(cSorcDir).Files
            
            If Len(Dir$(cDestDir & ofile.Name)) = 0 Then
                MsgBox ofile, , "File Missing"
                FileCopy ofile, cDestDir & ofile.Name
            End If
            
        Next
        
    End Sub

  3. #3
    Registered User
    Join Date
    05-22-2011
    Location
    chennai
    MS-Off Ver
    Excel 2003
    Posts
    71

    Re: Compare two folder files - whether they exit or not and if exit then copy to another f

    Hi one more help

    I trying to do the file copy process by according to column value (directory path) i.e cSorcDir = A column and cdestdir = B column. And below is the code which i tried but it didn't works - please help me on this too
    Sub compare()
        
    Dim ofso   As Object
        Dim ofile  As Object
        
        For i = 1 To 2
        Const cSorcDir As String = Sheets(Sheet1).Range("A", i)
        Const cdestdir As String = Sheets(Sheet1).Range("B", i)
        nwpt = cSorcDir
        ngpt = cdestdir
        Call ScanFolder(nwpt, ngpt)
        Next
    End Sub
    Function ScanFolder(cSorcDir, cdestdir)
        Set ofso = CreateObject("Scripting.FileSystemObject")
        
        For Each ofile In ofso.GetFolder(cSorcDir).Files
            
            If Len(Dir$(cdestdir & ofile.Name)) = 0 Then
                MsgBox ofile, , "File Missing"
                FileCopy ofile, cdestdir & ofile.Name
            End If
            
        Next
        
    End Function
    Thanks
    Thiru

  4. #4
    Registered User
    Join Date
    05-22-2011
    Location
    chennai
    MS-Off Ver
    Excel 2003
    Posts
    71

    Re: Compare two folder files - whether they exit or not and if exit then copy to another f

    Thank yo soooooo much AlphaFrog - It works like Charm.

    Thank you again Have a Nice Day

    Thanks
    Thiru

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Compare two folder files - whether they exit or not and if exit then copy to another f

    Sub compare()
        
        Dim i As Long
        
        With Sheets("Sheet1")
            For i = 1 To 2
                ScanFolder .Range("A" & i).Value, .Range("B" & i).Value
            Next i
        End With
        
    End Sub
    
    
    Sub ScanFolder(strSorcDir As String, strDestDir As String)
        
        Dim ofso   As Object
        Dim ofile  As Object
        
        Set ofso = CreateObject("Scripting.FileSystemObject")
        
        For Each ofile In ofso.GetFolder(strSorcDir).Files
            
            If Len(Dir$(strDestDir & ofile.Name)) = 0 Then
                MsgBox ofile, , "File Missing"
                FileCopy ofile, strDestDir & ofile.Name
            End If
        Next
        
    End Sub

  6. #6
    Registered User
    Join Date
    05-22-2011
    Location
    chennai
    MS-Off Ver
    Excel 2003
    Posts
    71

    Re: Compare two folder files - whether they exit or not and if exit then copy to another f

    Alphafrog

    Really thank you so much Works PERFECTLY- I don't have a word to praise you.

    Thanks you sooooooooooooooooooooooooooooooooooooooooooooo Muuuuuuuuuuuuuuuuuuuuuuuuuuuuuh

    Thanks
    Thiru

  7. #7
    Registered User
    Join Date
    05-22-2011
    Location
    chennai
    MS-Off Ver
    Excel 2003
    Posts
    71

    Re: Compare two folder files - whether they exit or not and if exit then copy to another f

    Alphafrog

    Really thank you so much Works PERFECTLY- I don't have a word to praise you.

    Thanks you sooooooooooooooooooooooooooooooooooooooooooooo Muuuuuuuuuuuuuuuuuuuuuuuuuuuuuh

    Thanks
    Thiru

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.6.0 RC 1