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

Excel 如果FSO复制的文件比目标文件夹中的文件新,如何添加代码以删除旧文件?,excel,vba,fso,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 A

我正在编写一个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

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

看看这个,看看它是否有用,你可以切碎并改变它。 它以一种稍微不同的方式进行,但它的工作方式与预期的一样

        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
子CopyFileIfOlder()
'声明您的变量
Dim strFilePathOne作为字符串,FileDateOne作为日期,FileOneLoc作为字符串
Dim strFilePath2作为字符串,FileDateTwo作为日期,FileTwoLoc作为字符串
'将文件路径设置为两个不同的文件
strFilePathOne=“C:\VBNET测试区域\StackOverFlow\File1.txt”
strFilePathTwo=“C:\VBNET测试区域\File1.txt”
'声明并设置对象
物体的暗度
Set of so=CreateObject(“Scripting.FileSystemObject”)
作为对象的le1的尺寸
le1的集合=oFSO.GetFile(strFilePathOne)
作为对象的le2的尺寸
setofile2=oFSO.GetFile(strfilepath2)
'获取文件修改日期:获取文件目录位置而不是文件名
FileDateOne=oFile1.DateLastModified:FileOneLoc=oFile1.Path
FileDateTwo=oFile2.DateLastModified:FileTwoLoc=oFile2.Path
'设置错误处理程序,以防复制出错
错误转到FileDeleteAndCopyError时出错
'测试文件1是否较旧
如果FileDateOne
嗨,斯普林斯芬克斯,让我试试代码。谢谢,祝你今天愉快!