+ Reply to Thread
Results 1 to 2 of 2

Move to folder based on file name

Hybrid View

  1. #1
    Registered User
    Join Date
    01-22-2014
    Location
    San Diego
    MS-Off Ver
    2010
    Posts
    23

    Move to folder based on file name

    I have a task at work and I’m not sure how to do what I want to do. I mail merge tons of letters and must file them into respective folders.

    I can move files fine using

    Sub MOVEFILES()
    
        Dim strFolderA As String
        Dim strFolderB As String
        Dim strFile As String
        Dim Cnt As Long
        Dim test As String
        
        test = 9
        '//Change the path to the source folder, accordingly
        strFolderA = "C:\Users\dtrowbri\Desktop\Test2\"
        
        '//Change the path to the destination folder, accordingly
        strFolderB = "C:\Users\dtrowbri\Desktop\Test\"
        
        'If Right(strFolderA, 1) <> "\" Then strFolderA = strFolderA & "\"
        'If Right(strFolderB, 1) <> "\" Then strFolderB = strFolderB & "\"
        
        '//To filter for .xlsx files, change "*.*" to "*.xlsx"
        strFile = Dir(strFolderA & "*.*")
        
        Do While Len(strFile) > 0
            
            Cnt = Cnt + 1
            Name strFolderA & strFile As strFolderB & strFile
            strFile = Dir
        Loop
        
        MsgBox Cnt & " file(s) have been transfered to " & strFolderB, vbInformation
            
    End Sub
    Now here’s the juicy part.

    I have files named 2 ways

    AB_######_name.pdf ‘where ###### is unique and
    ABC_######_name.pdf ‘ where again ###### is unique


    Our folder system is like this:
    C:\AB_######_name\Correspondence\ ‘THIS IS THE TARGET FOLDER FOR THE LETTER or
    C:\ABC_######_name\Correspondence\ ‘THIS IS THE TARGET FOLDER FOR THE LETTER



    The ###### in the Folder Name would be correspond to the PDF filename.

    Now how do I go ahead and move the filename to the correct folders. Sometimes the correspondence folder is not created, so I have to create it manually.
    Last edited by dusto; 01-22-2014 at 03:09 PM.

  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: Move to folder based on file name

    I'm thinking this should work. The function MakeFolders is from my free code site, it will create the folder(s) as needed.
    Option Explicit
    
    Sub MOVEFILES()
    'Jerry Beaucaire  www.ExcelForum.com    1/22/2014
    Dim fPATH As String, fNAME As String, fTYPE As String, fDEST As String
    Dim Cnt As Long
        
    fPATH = "C:\Path\To\Source\Files\"          'remember the final \ in this string
    fTYPE = ".PDF"                              'extension, filetype to find and move
    
    fNAME = Dir(fPATH & "*" & fTYPE)            'get first filename
            
    Do While Len(fNAME) > 0
        Cnt = Cnt + 1                           'increment counter
        fDEST = "C:\" & Replace(fNAME, fTYPE, "") & "\Correspondence\"      'create destination string
        MakeFolders (fDEST)                     'create folders if needed
        
        Name fPATH & fNAME As fDEST & fNAME     'move the file
        fNAME = Dir                             'get next filename
    Loop                                        'lather, rinse, repeat
    
    MsgBox Cnt & " file(s) have been transfered to their respective correspondence folders", vbInformation
            
    End Sub
    
    
    Function MakeFolders(MyStr As String)
    'Author:    Jerry Beaucaire
    'Date:      7/14/2010
    'Summary:   Create directories and subdirectories based on the text strings fed to the function
    '           This version is to be called by other macros
    '           10/19/2010 - International compliant
    Dim MyArr As Variant, pNum As Long
    Dim pBuf As String, Delim As String
    
        On Error Resume Next
        Delim = Application.PathSeparator
    
        MyArr = Split(MyStr, Delim)
        pBuf = MyArr(LBound(MyArr)) & Delim
        For pNum = LBound(MyArr) + 1 To UBound(MyArr)
            pBuf = pBuf & MyArr(pNum) & Delim
            MkDir pBuf
        Next pNum
    
    End Function
    Last edited by JBeaucaire; 12-27-2019 at 04:05 AM.
    _________________
    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!)

+ 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. Copy file from 1 folder to another folder
    By chidurala_shree in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-23-2013, 08:20 AM
  2. copy file to new folder based on cell value and display message box
    By kboy1289 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-05-2013, 03:38 PM
  3. search for a file in a folder based on data, copy and paste it in another folder
    By kboy1289 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2013, 11:47 AM
  4. Seach in folder and copy data from file based on sheet names in master workbook
    By Hassø in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-06-2012, 09:25 AM
  5. [SOLVED] Create a new folder based on a cell name or value and save copy onto that folder
    By Le_Tiago in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-20-2012, 01:33 PM

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