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

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
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,214,833
Messages
6,121,865
Members
449,052
Latest member
Fuddy_Duddy

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