Macro to retrieve the names of files

I need a macro that will do the following:

MyFolder1 has 5 excel files in it.  They are named:

File1
File2
File3
File4
File5

MyFolder2 currently has 1 file in it.  It is named:
Testfile.

Using a macro in the Testfile file, I need to make copies of the Testfile file, give each one the same name as the files in MyFolder1 and place the newly named files in MyFolder2 so that I wind up with the following in MyFolder2:

File1
File2
File3
File4
File5

And, no I can't just make copies of the files in MyFolder1.  I need to make copies of the Testfile file and rename them.

So what I'm looking for is how to cycle through MyFolder1, get the names of the files programmatically, create copies of the Testfile so that I wind up with 5 of them, rename them to match the names of the files in MyFolder1 and place the newly-created files in MyFolder2.  Thank you.
dbfromnewjerseyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
what's the file extension for the files in folder: MyFolder1 ? is it .xls or .xlsx etc?

is there only 5 files to be replicated in folder MyFolder1 based on the content of file: Testfile in MyFolder2?

are those mentioned file names above are fixed?
0
dbfromnewjerseyAuthor Commented:
They are all .xlsm

For illustration, I said there 5 files. There are actually many more but I can always change the count in the loop.   The source files are in MyFolder1.  The replicated files will be in MyFolder2.  The file used for the replicatons is in MyFolder2.

I indicated the exact names of the files.
0
Ryan ChongCommented:
since the file names to be replicated are prefixed, I have removed the dependencies in scripts to look for the file names in MyFolder1. Hence, this is the scripts that seems working for me.

You may customize this accordingly.
Sub Button1_Click()
    src = "C:\Users\test1\Downloads\MyFolder1\"
    dest = "C:\Users\test1\Downloads\MyFolder2\"
    srcFile = "C:\Users\test1\Downloads\MyFolder2\Testfile.xlsm"
    Call replicateFiles(src, dest, srcFile)
End Sub

Public Sub replicateFiles(ByVal source As String, ByVal destination As String, ByVal sourceFile As String, Optional loopCnt As Integer = 5, Optional FileName As String = "File")
    Dim f As String, sFile As String
    If Dir(source, vbDirectory) <> "." Then
        MsgBox "Source path not found", vbCritical, "Path not found"
        Exit Sub
    End If
    If Dir(destination, vbDirectory) <> "." Then
        MsgBox "Destination path not found", vbCritical, "Path not found"
        Exit Sub
    End If
    If Dir(sourceFile) = "" Then
        MsgBox "Source file not found", vbCritical, "File not found"
        Exit Sub
    End If
    sFile = Dir(sourceFile)
    
    'Replicate files
    For i = 1 To loopCnt
        f = destination & FileName & i & Mid(sFile, InStrRev(sFile, "."))
        'Delete old files if exist
        If Dir(f) <> "" Then Kill f
        FileCopy sourceFile, f
    Next i
    
    MsgBox "Done!"
    
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ryan ChongCommented:
or do you want based on what files (with file extension .xlsm?) that available in MyFolder1 and "replicate" it to MyFolder2 accordingly?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.