move files from subfolder to subfolder within main folder based on first part of name file

Abdo

Board Regular
Joined
May 16, 2022
Messages
183
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hello
I have main folder 's name is "DATA" in this directory " "C:\Users\Abdo\Desktop\"so to become "C:\Users\Abdo\Desktop\DATA\"
so the DATA folders contains many folders 01 , 02 ,03 and each folder contains PDF & EXCEL files their names are SR 2345678, PR 542200001 , RTR 123430009, ZXC 5677888,DC 000001
so what I want
EXAMPLE : folder 01 should create folders within 01 folder based on file name , for instance file SR 2345678 should create folder's name based on first part of the files names are existed within 01 folder like this : SR and move all of the files contain SR in first part of file name from folder 01 to folder SR whether .xls,xlsm,xlsx.... & PDF based on first part of name the file .the first part of file name will be two or three letters and space after it . and so on for all of folders are existed within DATA folder.
so I appreciate if anybody could help by macro does it that .
thanks
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi @Abdo:

Try the following macro:
VBA Code:
Sub movefiles()
  Dim fsObj As Object, oFldr As Object, oSubF As Object, oFile As Object
  Dim sPath As String, sNewf As String
  
  sPath = "C:\Users\Abdo\Desktop\DATA\"   'initial folder

  Set fsObj = CreateObject("Scripting.FileSystemObject")
  Set oFldr = fsObj.GetFolder(sPath)
  For Each oSubF In oFldr.SubFolders
    For Each oFile In oSubF.Files
      sNewf = oSubF & "\" & Split(oFile.Name, " ")(0)
      If fsObj.FolderExists(sNewf) = False Then fsObj.CreateFolder sNewf
      fsObj.MoveFile oFile, sNewf & "\" & oFile.Name
    Next
  Next
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0
Hi
I'm not sure what my mistake to shows error "file is already existed"
 
Upvote 0
The folders already exist, for example, does the "SR" folder already exist?
Do the files already exist inside the "SR" folder?
At which line of the macro does it stop?
Can you put images of your folders and your files?
All the relevant information that you can put is welcome, that way the help will be easier.

-----------------------------------​

Perhaps you have files that do not have a space in the name.
Then try this:
VBA Code:
Sub movefiles()
  Dim fsObj As Object, oFldr As Object, oSubF As Object, oFile As Object
  Dim sPath As String, sNewf As String
  
  Application.DisplayAlerts = False
  sPath = "C:\trabajo\DATA\" ' "C:\Users\Abdo\Desktop\DATA\"   'initial folder

  Set fsObj = CreateObject("Scripting.FileSystemObject")
  Set oFldr = fsObj.GetFolder(sPath)
  For Each oSubF In oFldr.SubFolders
    For Each oFile In oSubF.Files
      If InStr(1, oFile.Name, " ") > 0 Then
        sNewf = oSubF & "\" & Split(oFile.Name, " ")(0)
        If fsObj.FolderExists(sNewf) = False Then fsObj.CreateFolder sNewf
        If fsObj.FileExists(sNewf & "\" & oFile.Name) = False Then
          fsObj.MoveFile oFile, sNewf & "\" & oFile.Name
        End If
      End If
    Next
  Next
End Sub

--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------​
 
Upvote 0
The folders already exist, for example, does the "SR" folder already exist?
no but I suppose after create shouldn't problem then should ignore when have already created.
Do the files already exist inside the "SR" folder?
after macro creation new folder SR , should move files contains SR to created folder's name SR .
At which line of the macro does it stop?
strange I try to know but the error doesn't show anymore !

Can you put images of your folders and your files?
the code doesn't work within DATA folder is main folder
1.PNG


should be after run macro create folders and move files to created folders as this picture , but the code doesn't do so .
2.PNG


when enter JUNE folder
3.PNG


and should be as this picture and the code does it without any problem.
4.PNG

but when enter MM folder

5.PNG


and should be as in this picture ,the code doesn't do anything
6.PNG

I hope this help you .
 
Upvote 0
the code doesn't work within DATA folder is main folder

Did you adjust the folder name on this line?
VBA Code:
  sPath = "C:\trabajo\DATA\" ' "C:\Users\Abdo\Desktop\DATA\"   'initial folder
If you didn't then you should, it should be like this and try the macro from post#7 again.
Check that this is spelled correctly: C:\Users\Abdo\Desktop\DATA\

VBA Code:
sPath = "C:\Users\Abdo\Desktop\DATA\"   'initial folder
 
Upvote 0
Did you adjust the folder name on this line?
Yes
Check that this is spelled correctly: C:\Users\Abdo\Desktop\DATA\
actually I use copy & paste to avoid error in writing .
currently I use this
VBA Code:
sPath = "C:\DATA\"
but doesn't happen for some cases have already explained !
 
Upvote 0
Files with no space between words will not be moved, only those with a space in the name.

------------------------------​

Look at my example, in the DATA folder I have two subfolders 01 and 02.

Here I show you the files in the DATA\01 subfolder, before the macro:
1686238371420.png


After the macro, Two subfolders "rtr" and "sr" in "01" subfolder:
1686238787704.png


In rtr subfolder:
1686238876812.png


--------------------------------
so the DATA folders contains many folders 01 , 02 ,03 and each folder contains PDF & EXCEL files
The macro works with the folders that are inside the DATA folder. That's what you asked for.
If your files are in another way, it won't work.

You must be very clear and specific with what you ask for.
And also your examples are not clear or complete. Look at my examples so that you can take them as a reference to explain your requirements.

Since the data provided is not clear or sufficient, and although I would like to continue with my help, I will no longer do so.

--------------
Respectfully
Dante Amor
--------------​
 
Upvote 0
Hi Dante
sorry!
I don't want to waste your time and thank you for your trying help me , indeed it's difficult to explain this case, but I do my best.
based on your picture yes this is part of my requirement ,but I want doing your macro for ll of the folders and subfolders in C:\DATA
so should search for all of files in any folder whether in DATA or any subfolder is exited in DATA folder or any subfolder is existed within subfolder in DATA folder
example when there is files in DATA folder like PIC1
1.PNG


then will create folders based on files names like this
2.PNG

as you see the first and last file will move to new created folder NO,SR , the code doesn't do that
as your picture in folder 01 contains files , but I also want when there is another folder contains files within 01 folder does same thing
example 01 folder contains folders' name is NEW and contains NG 12222, HJ 122222 then will create folders within NEW folder to become NG,HJ
if this case is not clear I will try attach picture .
thanks again
 
Upvote 0
the code must deal with
1- files are existed in DATA folder if there are no folders within DATA folder
2- files are existed in DATA folder and there are subfolders contains files within DATA folder together
3- files are existed in DATA folder and there are subfolders contains files within DATA folder and subfolders contains files within subfolders within DATA folder.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

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