Copy and Rename files

TaskMaster

Board Regular
Joined
Oct 15, 2020
Messages
75
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi all,

I have the following vba that works as expected where it moves files from one location to another. I would like to build on this by renaming these in the destination folder from .txt to .csv is this possible?

VBA Code:
Public Sub Copy_Files2()

    Dim Folder As String
    Dim Dest As String
    Dim LastMonth As Date
    
    LastMonth = DateSerial(Year(Date), (Month(Date) - 1), Day(Date))
    Folder = "\\users\TM\Desktop\Test\"
    Dest = "\\users\TM\Desktop\Test\" & Format(LastMonth, "mm. ") & Format(LastMonth, "mmmm") & "\Data\Reports"
    
    Check_and_Copy_Files Folder & "330016.txt", Dest
    
    Check_and_Copy_Files Folder & "330008.txt", Dest
    
    Check_and_Copy_Files Folder & "330009.txt", Dest
    
    Check_and_Copy_Files Folder & "330010.txt", Dest
    
    Check_and_Copy_Files Folder & "330011.txt", Dest
    
    Check_and_Copy_Files Folder & "330013.txt", Dest
    
    Check_and_Copy_Files Folder & "330014.txt", Dest
    
    Check_and_Copy_Files Folder & "334008.txt", Dest

End Sub


Private Sub Check_and_Copy_Files(sourceFolderMatchFiles As String, destFolder As String)

    Static FSO As Object
    Dim FSfile As Object
    Dim sourceFolder As String, matchFiles As String
    Dim p As Long, n As Long
    
    If FSO Is Nothing Then Set FSO = CreateObject("Scripting.FileSystemObject")
   
    If Right(destFolder, 1) <> "\" Then destFolder = destFolder & "\"
    p = InStrRev(sourceFolderMatchFiles, "\")
    sourceFolder = Left(sourceFolderMatchFiles, p)
    matchFiles = Mid(sourceFolderMatchFiles, p + 1)
    
    n = 0
    For Each FSfile In FSO.GetFolder(sourceFolder).Files
        If LCase(FSfile.Name) Like LCase(matchFiles) Then n = n + 1
    Next
    
    If n = 0 Then
        MsgBox "There are no files in " & sourceFolder & " matching " & matchFiles & ", therefore no files copied to " & destFolder, vbInformation, "No files copied"
    Else
        For Each FSfile In FSO.GetFolder(sourceFolder).Files
            If LCase(FSfile.Name) Like LCase(matchFiles) Then
                If FSO.FileExists(destFolder & FSfile.Name) = False Then
                    FSfile.Copy destFolder
                End If
            End If
        Next
    End If
    
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
To rename the copied file, simply add the new file name in the destination path.

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,939
Messages
6,175,532
Members
452,652
Latest member
eduedu

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