如果FSO复制的文件比目标文件夹中的文件新,如何添加代码以删除旧文件?

时间:2019-10-10 10:29:35

标签: excel vba fso

我正在编写VBA代码,以将文件拉到源文件夹中,然后移到我的文件夹中进行分析,以免破坏原始文件。但是,如果检测到名称相同,我将无法编写替换旧文件的代码。

Private Sub CommandButton1_Click()
Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder As String
Dim datasource As Workbook
Dim target As Worksheet
Dim strName As String
Dim lastrow As Long
Dim userinput As String

userinput = InputBox(Prompt:="Please key in the file name and format", Title:=" File name", Default:=".xlsm") 'This is Your File Name which you want to Copy
sSFolder = "J:\Inter Dept\MP8 Packaging\2.0 MP8.1\B2-Machine Data Tracking\B2-Machine Data Tracking 2019\" 'Change to match the source folder path
sDFolder = "C:\Users\limmigmy\Desktop\Rejection Report\Rejection\Packing Analysis\Production Files\" 'Change to match the destination folder path

Set FSO = CreateObject("Scripting.FileSystemObject") 'Create Object

If Not FSO.FileExists(sSFolder & userinput) Then 'Checking If File Is Located in the Source Folder
    MsgBox "Specified File Not Found", vbInformation, "Not Found"

ElseIf Not FSO.FileExists(sDFolder & userinput) Then 'Copying If the Same File is Not Located in the Destination Folder
    FSO.CopyFile (sSFolder & sFile), sDFolder, True
    MsgBox "Specified File Copied Successfully", vbInformation, "Done!"
**Else
    MsgBox "Specified File Already Exists In The Destination Folder",** vbExclamation, "File Already Exists"
End If

1 个答案:

答案 0 :(得分:0)

我想我知道您的意思,只是“但是,如果检测到相同的名称,我将很难编写替换旧文件的代码。”有点令人困惑。

检查一下是否有帮助,您可以切碎和更改它。 它的处理方式略有不同,但是可以正常工作。

        Sub CopyFileIfOlder()
        'declare your variables
        Dim strFilePathOne As String, FileDateOne As Date, FileOneLoc As String
        Dim strFilePathTwo As String, FileDateTwo As Date, FileTwoLoc As String

        'set your file paths to the two different files
        strFilePathOne = "C:\VBNET Test Area\StackOverFlow\File1.txt"
        strFilePathTwo = "C:\VBNET Test Area\File1.txt"

        'declare and set your objects
        Dim oFSO As Object
            Set oFSO = CreateObject("Scripting.FileSystemObject")
        Dim oFile1 As Object
            Set oFile1 = oFSO.GetFile(strFilePathOne)
        Dim oFile2 As Object
            Set oFile2 = oFSO.GetFile(strFilePathTwo)

        'grabs your files modified dates : grabs your files dir location not inc filename
        FileDateOne = oFile1.DateLastModified: FileOneLoc = oFile1.Path
        FileDateTwo = oFile2.DateLastModified: FileTwoLoc = oFile2.Path

        'sets an error handler incase anything goes wrong copying
        On Error GoTo FileDeleteAndCopyError

            'tests to see if file1 is older
            If FileDateOne < FileDateTwo Then
                'file one is older
                Kill strFilePathOne 'deletes file1
                    FileCopy strFilePathTwo, FileOneLoc 'copies the newer version into older files place
            Else
                'file two is newer
                Debug.Print "FILE ONE IS NEWER THAN FILE TWO SO I WONT DO ANYTHING" 'PRINTS A MESSAGE SAYING NO COPY HAS BEEN DONE
            End If

        'RETURNS ERROR HANDLER TO NORMAL
        On Error GoTo 0

        'EXITS SUB BECAUSE IF NOT THE ERROR HANDLER WILL FIRE
        Exit Sub

        'ERROR HANDLER INCASE FILE COPY GOES WRONG
   FileDeleteAndCopyError:
            MsgBox "There has been an error!", vbCritical

        End Sub