Copy and Rename files

TaskMaster

Board Regular
Joined
Oct 15, 2020
Messages
55
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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
To rename the copied file, simply add the new file name in the destination path.

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,958
Members
449,096
Latest member
Anshu121

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