+ Reply to Thread
Results 1 to 6 of 6

VBA to copy files that contain certain text within filename from one folder to another

Hybrid View

NeilM VBA to copy files that... 07-23-2013, 08:41 PM
patel45 Re: VBA to copy files that... 07-24-2013, 02:54 AM
NeilM Re: VBA to copy files that... 07-24-2013, 07:03 PM
patel45 Re: VBA to copy files that... 07-25-2013, 02:04 AM
NeilM Re: VBA to copy files that... 07-25-2013, 02:38 AM
aragorn25 Re: VBA to copy files that... 03-27-2015, 06:28 AM
  1. #1
    Registered User
    Join Date
    05-17-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2007
    Posts
    15

    VBA to copy files that contain certain text within filename from one folder to another

    Hi All,

    I need a VBA that can go through a certain folder and copy files that contain a certain text into another folder. For example.

    I have 100 PDF files in 'FOLDER A'

    I need to go through 'FOLDER A' and find the PDF files that contain the word 'Sydney' and copy it in folder called 'FOLDER - Sydney'. Then find the files that contain the word 'Melbourne and copy it into 'FOLDER - Melbourne', and so on untill i have completed the string requirements.

    If you can help me with this it would be great!!!!


    Thanks Guys!



    Below is a VBA i tried to manipulate, however it did not work and dont know how to loop it into another target folder.

    sSrcFolder = ActiveSheet.Cells(2, 1) - Folder with all files
    sTgtFolder = ActiveSheet.Cells(2, 2) - Folder to copy into
    Set rPatterns = ActiveSheet.Range("E2:E15")
    - list of words i was looking for.

    Sub CopyFiles_Containing()
        Dim sSrcFolder As String, sTgtFolder As String, sFilename As String
        Dim c As Range, rPatterns As Range
        Dim bBad As Boolean
        
        sSrcFolder = ActiveSheet.Cells(2, 1)
        sTgtFolder = ActiveSheet.Cells(2, 2)
        
        Set rPatterns = ActiveSheet.Range("E2:E15").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
    Last edited by Leith Ross; 07-24-2013 at 10:55 PM. Reason: Added Code Tags

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: VBA to copy files that contain certain text within filename from one folder to another

    Sub copyfile()
    Set fs = CreateObject("Scripting.FileSystemObject")
    oldpath = "C:\FOLDER A"
    newpath = "C:\FOLDER - Sydney"
    Set f = fs.getfolder(oldpath)
    Set NFile = f.Files
    For Each pf1 In NFile
      NameFile = pf1.Name
      If InStr(NameFile, "Sydney") > 0 Then
        filecopy oldpath & "\" & NameFile, newpath & "\" & NameFile
      End If
    Next
    End Sub
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    05-17-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: VBA to copy files that contain certain text within filename from one folder to another

    Brilliant!

    Thanks Patel45. Also

    If i wanted to add more to this. say that now If i wanted to move all files that contain 'Melbourne' to 'FOLDER - Melbourne' (and so on)within the same VBA how do i add on the other requirements.

    instead of just Sydney i want add on other requirements.

    Sydney to FOLDER - Sydney
    Melbourne to FOLDER - Melbourne
    Brisbane to FOLDER Brisbane
    etc.

    If you can advise of the code to set for each location, i cna apply that to the rest.

    Thanks Patel Appeciate it!

    Neil.

  4. #4
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: VBA to copy files that contain certain text within filename from one folder to another

    Sub copyfile()
    Set fs = CreateObject("Scripting.FileSystemObject")
    oldpath = inputbox ("Source path ")
    newpath =  inputbox ("Destination path ")
    wordtofind = inputbox ("Word to find ")
    Set f = fs.getfolder(oldpath)
    Set NFile = f.Files
    For Each pf1 In NFile
      NameFile = pf1.Name
      If InStr(NameFile, wordtofind) > 0 Then
        filecopy oldpath & "\" & NameFile, newpath & "\" & NameFile
      End If
    Next
    End Sub

  5. #5
    Registered User
    Join Date
    05-17-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2007
    Posts
    15

    Re: VBA to copy files that contain certain text within filename from one folder to another

    You have been a great help Patel45!

    Thank you.

  6. #6
    Registered User
    Join Date
    08-29-2013
    Location
    Poland
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: VBA to copy files that contain certain text within filename from one folder to another

    What in case |I have a list of files not one word?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] VBA Code open files in folder, copy text to workbook-Next time folder opened copy only new
    By Bikeman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-02-2013, 07:59 PM
  2. excel macro to copy data from multi text files in a folder to a single work sheet
    By dil_se in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-16-2012, 08:10 AM
  3. open all files in a folder with fixed number of characters in filename
    By Telemick in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 01-18-2012, 03:31 AM
  4. [SOLVED] Copy Files to other folder
    By TISR in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-01-2006, 03:50 AM
  5. How to copy 30 csv files from a folder to another folder
    By ddiicc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-17-2005, 05:05 AM

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