VBA Copy File, notify if file not available

TaskMaster

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

I have the following which will copy a list of files and paste them into my monthly folder as needed. Is there a way to notify me if any of these files aren't in the Folder location and can't be copied. Also is it possible to add if this file already exists in the new location do not copy?

VBA Code:
Sub Copy_Files()

Dim Folder As String
Dim Dest As String
Dim fso As Object
Dim LastMonth As Date

Set fso = VBA.CreateObject("Scripting.FileSystemObject")
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"

Call fso.CopyFile(Folder & "330016*", Dest)

Call fso.CopyFile(Folder & "330008*", Dest)

Call fso.CopyFile(Folder & "330009*", Dest)

Call fso.CopyFile(Folder & "330010*", Dest)

Call fso.CopyFile(Folder & "330011*", Dest)

Call fso.CopyFile(Folder & "330013*", Dest)

Call fso.CopyFile(Folder & "330014*", Dest)

Call fso.CopyFile(Folder & "334008*", Dest)

Call fso.CopyFile(Folder & "334009*", Dest)

Call fso.CopyFile(Folder & "334010*", Dest)

Call fso.CopyFile(Folder & "334011*", Dest)

Call fso.CopyFile(Folder & "358026*", Dest)

Call fso.CopyFile(Folder & "358027*", Dest)


End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try this macro:
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*.*", Dest
    
    Check_and_Copy_Files Folder & "330008*.*", Dest
    
    Check_and_Copy_Files Folder & "330009*.*", Dest
    
    Check_and_Copy_Files Folder & "330010*.*", Dest
    
    Check_and_Copy_Files Folder & "330011*", Dest
    
    Check_and_Copy_Files Folder & "330013*", Dest
    
    Check_and_Copy_Files Folder & "330014*", Dest
    
    Check_and_Copy_Files Folder & "334008*", Dest
    
    Check_and_Copy_Files Folder & "334009*", Dest
    
    Check_and_Copy_Files Folder & "334010*", Dest
    
    Check_and_Copy_Files Folder & "334011*", Dest
    
    Check_and_Copy_Files Folder & "358026*", Dest
    
    Check_and_Copy_Files Folder & "358027*", 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
 
Upvote 0
Solution
Hi John, looping back around to this query. As it stands the process works perfectly. Im wondering if it is possible to rename the files when moving from .txt to .csv
 
Upvote 0
rename the files when moving from .txt to .csv

Replace the Check_and_Copy_Files function with:

VBA Code:
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 any of these files aren't in the Folder location and can't be copied. Also is it possible to add if this file already exists in
    'the new location do not copy?
    
    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
        Debug.Print FSfile
        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
                    If LCase(FSfile.Name) Like "*.txt" Then
                        'Rename .txt file as .csv
                        FSO.CopyFile FSfile.Path, destFolder & Replace(FSfile.Name, ".txt", ".csv")
                    Else
                        FSfile.Copy destFolder
                    End If
                End If
            End If
        Next
    End If
    
End Sub
Note the change only affects .txt files (i.e. only .txt files are renamed as .csv), otherwise all previous functionality is retained.
 
Upvote 0

Forum statistics

Threads
1,215,095
Messages
6,123,072
Members
449,093
Latest member
ripvw

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