Move files to diff folders based on filename

THEMAN

New Member
Joined
Nov 5, 2008
Messages
3
Hi All

Is there some code out there that can move all the excel files based on filename on my desktop to existing folder with the same name eg folder name is dog, files on my desktop is dog.xls and folder name is cat, files on my desktop is cat.xls

I have muitlple files and I don;t want to manually move them one by one.

Thanks in advanced.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
1) Do these folders already exist or do they need to be created based on all the Excel files found on your desktop?

2) What path are they being created in? (or exist in already?)

"C:\My Documents\Excel\Dog\"
"C:\My Documents\Excel\Cat\"
 
Upvote 0
This little macro will move all the XLS files from the desktop into a folder of the same name in the designated toPath.

If the folder doesn't exist already, the macro will create it.
Code:
Option Explicit

Sub MoveFiles()
'JBeaucaire  (2/6/2010)
'Moves files from the desktop into folders of their own
Dim fName As String, fromPath As String, toPath As String, Cnt As Long
On Error Resume Next

toPath = "C:\Test1\"
fromPath = "C:\Documents and Settings\Jerry\Desktop\"

Restart:
If Cnt > 1 Then Exit Sub
fName = Dir(fromPath & "*.xls")

Do While Len(fName) > 0
    If Cnt > 1 Then Exit Sub
    Cnt = 0
    If Len(Dir(toPath & Left(fName, Len(fName) - 4), vbDirectory)) = 0 Then
        MkDir toPath & Left(fName, Len(fName) - 4)
    End If
    Name (fromPath & fName) As (toPath & Left(fName, Len(fName) - 4) & "\" & fName)
    fName = Dir
Loop

Cnt = Cnt + 1
GoTo Restart

End Sub
 
Upvote 0
Thanks jbeaucaire that will be useful in future, however the folders already exist, move to the same folder name?
 
Upvote 0
Hi jbeaucaire,

Just tried your macro and it works great!
I have a similar task that I'm trying to accomplish but I'm still a newbie with VBA and very interested in learning more! Tried rewriting your code but didn't get it to work and still grasping the left() and right() functions.
The code is probably simpler than I'm trying to make it.

I have about 500 .pdf files in the fromPath. Here are some filename examples:
10012345.pdf
10022222.pdf
20012345.pdf
20011111.pdf
20112345.pdf
20212345.pdf

I will need to create subdirectories in the toPath according to just the first 3 digits of the filename and then moving (distributing) the file to that created directory. Thus from the example, Subfolder 100 will have 2 files, subfolder 200 will have the 2 files listed, and so on.

Any help would greatly be appreciated!
Rotciv
 
Upvote 0
Try this:
Code:
Option Explicit

Sub MoveFiles()
'JBeaucaire  (4/8/2010)
'Moves files from the desktop into folders of their own
'Create subfolders as needed based on first 3 characters of each filename
Dim fName As String, fromPath As String, toPath As String
Dim toSubPath As String, Cnt As Long
On Error Resume Next

toPath = "C:\Test1\"
fromPath = "C:\2010\"

Restart:
If Cnt > 1 Then Exit Sub
fName = Dir(fromPath & "*.pdf")

Do While Len(fName) > 4
    If Cnt > 1 Then Exit Sub
    Cnt = 0
    toSubPath = toPath & Left(fName, 3) & "\"
    If Len(Dir(toSubPath, vbDirectory)) = 0 Then MkDir toSubPath
    Name (fromPath & fName) As (toSubPath & fName)
    fName = Dir
Loop

Cnt = Cnt + 1
GoTo Restart

End Sub
 
Upvote 0
jbeaucaire,

I just had a chance to try it and it worked beautifully.
I'm trying to understand the function of your Cnt variable.
Not too sure, but it looks like it ends the macro once all the
files have been moved.

Thank you so much for your help!!!
 
Upvote 0
I'm sure there'e a more elegant way to deal with it, but the CNT variable is my way of dealing with how the DIR() listing gets messed up if you move any of the files that make up the current listing.

When a file is moved, the next DIR() comes up empty, so I need to restart the whole process. When I restart, CNT is incremented...usually from 0 to 1. If more files still exist to move, the macro continues and INSIDE the For/Next loop the CNT is set back to zero.

So, as long as there are files to move, the CNT will never be more than 1. But the first time it restarts and there are no more files to move, the For/Next loop is skipped and the CNT get incremented to 2. Since that is greater than 1, the macro aborts when it restarts again...the first line of code after Restart: aborts when the CNT is greater than 1.
 
Upvote 0
Jbeaucaire,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
I wanted to say that I have used your macro several times and it works nicely.<o:p></o:p>
<o:p></o:p>
I'm now in needing a different approach and I would appreciate if you can give me an option for that.<o:p></o:p>
<o:p></o:p>
I have an excel file where one of the columns contains the image related to each particular product.<o:p></o:p>
I also have a folder with thousands of images, including the images of the products in the excel file.<o:p></o:p>
<o:p></o:p>
What I need is moving to a new directory only the images that are referenced in the excel file.<o:p></o:p>
Could you please help me on this?<o:p></o:p>
<o:p></o:p>
Thanks in advance<o:p></o:p>
Bill<o:p></o:p>
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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