+ Reply to Thread
Results 1 to 4 of 4

copy files located in multiple directories that match a partial value

Hybrid View

  1. #1
    Registered User
    Join Date
    12-14-2012
    Location
    Rochester, NY
    MS-Off Ver
    Excel 2010
    Posts
    2

    copy files located in multiple directories that match a partial value

    I want to copy files located in multiple directories (source folders) that match a partial value in column 1 of sheet 1. The code i have works if all the files are in the source folder but will not locate further down in sub directories. Here is my code. Any help would help tremendously.

    Sample directory
    FROM
    E:\P25\1000\
    E:\P25\2000\ and so on to E:\P25\100000\
    To
    C:\temp\Data2\

    Sub P25option()
        Dim sSrcFolder As String, sTgtFolder As String, sFilename As String
        Dim c As Range, rPatterns As Range
        Dim bBad As Boolean
        
        sSrcFolder = "E:\P25"
        
        sTgtFolder = "C:\temp\Data2\"
    
    
        
        Set rPatterns = Sheet1.Range("A1:A2000").SpecialCells(xlConstants)
        For Each c In rPatterns
            sFilename = Dir(sSrcFolder & "*" & c.Text & "*")
            If sFilename = "" Then
                c.Interior.ColorIndex = 3
                bBad = True
            Else
                While sFilename <> ""
                    FileCopy sSrcFolder & sFilename, sTgtFolder & sFilename
                    sFilename = Dir()
                Wend
            End If
        Next c
        If bBad Then MsgBox "Some files were not found. " & _
            "These were highlighted for your reference."
    End Sub

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: copy files located in multiple directories that match a partial value

    This looks like it should work, untested:

    Option Explicit
    Dim Found As Boolean, c As Range, rPatterns As Range
    Dim sSrcFolder As String, sTgtFolder As String, sFilename As String
    
    Sub P25option()
    Dim bBad As Boolean
    
    sSrcFolder = "E:\P25"
    sTgtFolder = "C:\temp\Data2\"
    
        Set rPatterns = Sheet1.Range("A1:A2000").SpecialCells(xlConstants)
        For Each c In rPatterns
            Call LoopController(sSrcFolder, c.Text, False)
            If Not Found Then
                c.Interior.ColorIndex = 3
                bBad = True
            Else
                Found = False
            End If
        Next c
    
        If bBad Then MsgBox "Some files were not found. " & "These were highlighted for your reference."
    End Sub
    
    
    Sub LoopController(sSrcFolder As String, fname As String, Found As Boolean)
    Dim Fldr As Object, FL As Object, SubFldr As Object, f As String
    
        If Found = True Then Exit Sub
        f = Dir(sSrcFolder & "*" & fname)
        Do While Len(f) >= 0
            FileCopy sSrcFolder & f, sTgtFolder & f
            sFilename = Dir()
            Found = True
        Loop
        If Found = True Then Exit Sub
    
        Set Fldr = CreateObject("scripting.filesystemobject").Getfolder(sSrcFolder)
        For Each SubFldr In Fldr.SubFolders
            Call LoopController(SubFldr.Path, fname, False)
        Next
    
    End Sub
    Last edited by JBeaucaire; 12-17-2012 at 04:43 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    12-14-2012
    Location
    Rochester, NY
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: copy files located in multiple directories that match a partial value

    running into a Path/File access error

    FileCopy sSrcFolder & f, sTgtFolder & f

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: copy files located in multiple directories that match a partial value

    What are the values at the DEGUB moment of:

    sSrcFolder
    sTgtFolder
    f

+ Reply to Thread

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