Excel VBA Move files to subfolder based folder partial name

bmacias

Board Regular
Joined
Sep 11, 2002
Messages
215
Hi.

I have several files in my source folder = "C:\test\" my target folder = "C:\iln". Within the "iln" I have sub-folders partially named after the files in the source folder (i.e. the source file is called "District Overall Score Ranking - EDINBURG" and the subfolder in the "iln" directory is named "C:\iln\EDINBURG") the name convention follows the same pattern.

What is the proper VBA code to move the files to their appropriate subfolders?

Thank you in advanced for your help.

Cheers,

Ben
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try this macro:
VBA Code:
Public Sub Move_Files()

    Dim sourceFolder As String, destFolder As String
    Dim fileName As String, f As String
    
    sourceFolder = "C:\test\"
    destFolder = "C:\iln\"
    
    If Right(sourceFolder, 1) <> "\" Then sourceFolder = sourceFolder & "\"
    If Right(destFolder, 1) <> "\" Then destFolder = destFolder & "\"
    
    fileName = Dir(sourceFolder)
    While fileName <> vbNullString
        f = Split(fileName, ".")(0)
        Name sourceFolder & fileName As destFolder & Split(f, " - ")(1) & "\" & fileName
        fileName = Dir
    Wend
    
End Sub
 
Upvote 0
Solution
Try this macro:
VBA Code:
Public Sub Move_Files()

    Dim sourceFolder As String, destFolder As String
    Dim fileName As String, f As String
   
    sourceFolder = "C:\test\"
    destFolder = "C:\iln\"
   
    If Right(sourceFolder, 1) <> "\" Then sourceFolder = sourceFolder & "\"
    If Right(destFolder, 1) <> "\" Then destFolder = destFolder & "\"
   
    fileName = Dir(sourceFolder)
    While fileName <> vbNullString
        f = Split(fileName, ".")(0)
        Name sourceFolder & fileName As destFolder & Split(f, " - ")(1) & "\" & fileName
        fileName = Dir
    Wend
   
End Sub
Thank you sir. I kid you not, I spent 8 hours trying to come-up with the code to no avail. This works perfectly!
 
Upvote 0

Forum statistics

Threads
1,215,479
Messages
6,125,043
Members
449,206
Latest member
Healthydogs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top