Copy and rename files without overwriting files

miguel78

New Member
Joined
Dec 16, 2016
Messages
1
Hello,

I need some help to adjust this code found on the internet.

I would like that whenever I run the code, new files on the destination folder are never overwritten. The solution seems easy, but I tried a lot and I'm inexperienced in VBA. Please help me.

File attached.

Thank you.

Code:
[/COLOR]<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Sub gerafiles()
    Dim src As String, dst As String, fl As String
    Dim rfl As String
    Dim lngMyRow As Long
    Dim lngLastRow As Long
        
    Application.ScreenUpdating = True
    
    lngLastRow = Range("B:D").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
    'Source directory
    src = Range("B3")
    'Destination directory
    dst = Range("D3")
    For lngMyRow = 6 To lngLastRow
        'File name
        fl = Range("B" & lngMyRow)
        'Rename file
        rfl = Range("D" & lngMyRow)
        On Error Resume Next
        FileCopy src & "\" & fl, dst & "\" & rfl
        
        On Error GoTo 0
    Next lngMyRow
    
    Application.ScreenUpdating = True
    
    MsgBox "OK"
    
End Sub</code>[COLOR=#333333]
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Code:
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Sub gerafiles()
    Dim src As String, dst As String, fl As String
    Dim rfl As String
    Dim lngMyRow As Long
    Dim lngLastRow As Long
        
    Application.ScreenUpdating = True
    
    lngLastRow = Range("B:D").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
    'Source directory
    src = Range("B3")
    'Destination directory
    dst = Range("D3")
    For lngMyRow = 6 To lngLastRow
        'File name
        fl = Range("B" & lngMyRow)
        'Rename file
        rfl = Range("D" & lngMyRow)
        On Error Resume Next
[B]If Dir(""&dst&"\"&rfl&"") <code><> "" Then
Next lngMyRow </code>[/B]
[B]Else[/B]        
FileCopy src & "\" & fl, dst & "\" & rfl
        
        On Error GoTo 0
    Next lngMyRow
    
    Application.ScreenUpdating = True
    
    MsgBox "OK"
[B]End if[/B]    
End Sub</code>
Try this.
Make sure you make some backups before fiddling around with this, or you might lose important data!
This code should check if the file already exists, and if so skip saving that file and go to the next one
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

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