Copy files from one folder to another using VBA.

Kylen

New Member
Joined
Jan 30, 2015
Messages
48
Hi everyone,
I need to copy files from one folder to another using VBA.

I have a folder with a lot of csv files with different names. I want to copy files when part of the filename is the same as value in cell D2. It will be up to 4 files that contain that value in D2. I want to create a new subfolder with the same name as in cell D2 and copy matching files to it.
Let’s say all csv files are in C:\test\ and I want to copy them to C:\test\”D2 value”\

Is there anyone that know if it’s possible or how to do it? :confused:

Using Excel 2010.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
how about

v = Shell("CMD /C ROBOCOPY C:\test\*" & range("D2").Value2 & "*.csv C:\test\" & range("D2").Value2, 1)

HTH
 
Upvote 0
Hi Fazza,
I can’t get it to work with your line but I’m not sure how to do it either. I guess I need to add something more to the script?
//Kylen
 
Upvote 0
Though it looks OK to me, I didn't test it. Nothing needed other than that line.

I'll test it next week: maybe I'm missing a character. Such as : or \. Or some simple blunder.. :-)
 
Upvote 0
A missing space. This tested OK.

v = Shell("CMD /C ROBOCOPY C:\test\ *" & Range("D2").Value2 & "*.csv C:\test\" & Range("D2").Value2, 1)

If not OK for you, please post sample of file names & "D2" entry. Thanks
 
Upvote 0
Hi,

It's working fine but is it possible to modify so it doesn't create a folder if it can't find a file that contain that name? It creates a folder even if there is no file to copy.
Is it possible to do the same further down in column D, D2, D3 D4 and down until no value in cell?

Many thanks for you help! :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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