MACRO to copy data.xls file from different subfolders to dest folder and rename the file

sanjay1982

New Member
Joined
May 2, 2013
Messages
6
I would like to copy every file named "data.xls" from every subfolder found in the folder c:\test\ and want to save in "dest" folder with file name "data_1.xls","data_2.xls"....so on so. Each sub folder is named as 01022013,02022013,03022013.....and all are contain the file "data.xls"
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
not tested
Code:
Sub a()
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   oldpath = "c:\test\"
   newpath = "c:\dest\"
   Set mainFolder = objFSO.GetFolder(oldpath)
   n = 1
   For Each mySubFolder In mainFolder.subfolders
     Name = "data.xls"
     Name1 = "data_" & n & ".xls"
     FileCopy mySubFolder & Name, newpath & Name1
     n = n + 1
   Next
End Sub
 
Upvote 0
@mr patel

dear its not wo.after running macro it gives run time error 53"file not found"

and after debugging its highlighted the following line

FileCopy mySubFolder & Name, newpath & Name1
 
Upvote 0
Code:
Sub a()
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   oldpath = "c:\test\"
   newpath = "c:\dest\"
   Set mainFolder = objFSO.GetFolder(oldpath)
   n = 1
   For Each mySubFolder In mainFolder.subfolders
     Name = "data.xls"
     Name1 = "data_" & n & ".xls"
     FileCopy mySubFolder [COLOR=#ff0000]& "\"[/COLOR] & Name, newpath & Name1
     n = n + 1
   Next
End Sub
 
Upvote 0
hello mr patel,
as i said that i have subfolders named as 01022013,02022013,03022013(these are the dates on which reports are generated) so can u add this at the end of file name like data_01022013,data_02022013,data_03022013 in place of data_1,data_2 ...
 
Upvote 0
i'm not sure about understanding, try
Code:
Sub a()
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   oldpath = "c:\test\"
   newpath = "c:\dest\"
   Set mainFolder = objFSO.GetFolder(oldpath)
   For Each mySubFolder In mainFolder.subfolders
     Name = "data.xls"
     Name1 = "data_" & mySubFolder & ".xls"
     FileCopy mySubFolder [COLOR=#ff0000]& "\"[/COLOR] & Name, newpath & Name1
   Next
End Sub
 
Upvote 0
its not wo and giving run time error '52' "bad file name or number"

actually in process of rename its added the path with original file name like NAME1=DATA_C:\TEST\01022013.XLS

Name1 = "data_" & mySubFolder & ".xls"

FileCopy mySubFolder & "\" & Name, newpath & Name1
 
Upvote 0
Code:
Sub a()
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   oldpath = "c:\test\"
   newpath = "c:\dest\"
   Set mainFolder = objFSO.GetFolder(oldpath)
   For Each mySubFolder In mainFolder.subfolders
     Name = "data.xls"
     p=instrrev(mySubFolder,"\")
     Name1 = "data_" & right(mySubFolder, len(mySubFolder)-p) & ".xls"
     FileCopy mySubFolder [COLOR=#ff0000]& "\"[/COLOR] & Name, newpath & Name1
   Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,004
Members
452,374
Latest member
keccles

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