coy files from subfolders

troy79

New Member
Joined
Dec 26, 2019
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi.

I have the below VBA script that will copy .csv files from multiple subfolders into 1 folder. It works great if the file names are unique but if I have any that are the same it won't generate a copy.
Can anyone help me modify the script to allow it bring in duplicates csv files.

example below:
Scrape Results 02-03 - 0114PM
Scrape Results 02-03 - 0114PM (so the second file it which has same name can it put a (1) on the end instead of ignoring it)

Code:

VBA Code:
Sub copy_files_from_subfolders()
Dim FSO As Object, fld As Object
Dim fsoFile As Object
Dim fsoFol As Object

SourcePath = "C:\Users\dan\Desktop\zz"
targetPath = "C:\Users\dan\Documents\splitmerge\"

If Right(SourcePath, 1) <> "\" Then SourcePath = SourcePath & "\"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set fld = FSO.getfolder(SourcePath)
If FSO.folderexists(fld) Then
For Each fsoFol In FSO.getfolder(SourcePath).subfolders
For Each fsoFile In fsoFol.Files
If Right(fsoFile, 3) = "csv" Then
fsoFile.Copy targetPath
End If
Next
Next
End If


End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
U cant trial this bit of code addition. HTH. Dave
Code:
For Each fsofile In fsoFol.Files
If Right(fsofile, 3) = "csv" Then
If FSO.fileexists(targetPath & fsofile.Name) Then
fsofile.Copy targetPath & "(1)"
Else
fsofile.Copy targetPath
End If
End If
Next
ps. not sure if this works as I think it should be copyfile with source and destination paths?
 
Upvote 0

Forum statistics

Threads
1,214,846
Messages
6,121,905
Members
449,054
Latest member
luca142

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