Duplicate file (filecopy) for certain number of times

SunValley

New Member
Joined
Jul 12, 2023
Messages
45
Office Version
  1. 365
Platform
  1. Windows
Hello, I have a formula that renames files named in column A, for column B, in a given folder. I'm trying to modify this formula, so that instead of using rename, it just duplicates files (filecopy) that are in column A, by the number of times that are in column B, on the same line. I would also like to keep the default folder for the source files, like the VBA rename I made available.
Also, if the number contained in column B is 0 or less, do nothing to avoid errors (unless VBA automatically recognizes that if it is 0 or less, it shouldn't do any duplication or delete anything).

VBA Code:
Sub DuplicateFile()
  Dim xDir As String, xFile As String
  Dim xRow As Long
  Dim ps As String
 
  xDir = "C:\Users\abc"
  xFile = Dir(xDir & Application.PathSeparator & "*")
  ps = Application.PathSeparator
 
  Do Until xFile = ""
    xRow = 0
    On Error Resume Next
    xRow = Application.Match(xFile, Range("A:A"), 0)
    If xRow > 0 Then
      Name xDir & ps & xFile As xDir & ps & Cells(xRow, "B").Value
    End If
    xFile = Dir
  Loop
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I managed to do the VBA

VBA Code:
Sub Duplicate()
    Dim name As String
    Dim times As Integer
    Dim i As Integer
    Dim j As Integer
    Dim point As Integer
    Dim base As String
    Dim extension As String
    


j = Cells(Rows.Count, 1).End(xlUp).Row




For i = 2 To j




    name = Cells(i, 1).Value
    


    times = Cells(i, 2).Value
    


    If Dir(aome) = "" Then
        MsgBox "not found."
        Exit Sub
    End If
    


    point = InStrRev(name, ".")
    


    base = Left(name, point - 1)
    


    extension = Mid(name, point)
    


    For k = 1 To times
        
        FileCopy name, base & "(" & k & ")" & extension
        
    Next k
    
Next i




End Sub
 
Upvote 0
Correction:

VBA Code:
Sub Duplicate()
    Dim name As String
    Dim times As Integer
    Dim i As Integer
    Dim j As Integer
    Dim point As Integer
    Dim base As String
    Dim extension As String
    Dim k As Integer


j = Cells(Rows.Count, 1).End(xlUp).Row


For i = 2 To j


    name = Cells(i, 1).Value
   

    times = Cells(i, 2).Value

    If Dir(name) = "" Then
        MsgBox "not found."
        Exit Sub
    End If
   

    point = InStrRev(name, ".")

    base = Left(name, point - 1)

    extension = Mid(name, point)
   


    For k = 1 To times
       
        FileCopy name, base & "(" & k & ")" & extension
       
    Next k
   
Next i

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,148
Messages
6,123,300
Members
449,095
Latest member
Chestertim

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