【问题标题】:How to add the code to delete the older file if FSO copied file is newer than the file in the destination folder?如果 FSO 复制的文件比目标文件夹中的文件新,如何添加代码以删除旧文件?
【发布时间】:2019-10-10 10:29:35
【问题描述】:

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

【问题讨论】:

    标签: excel vba fso


    【解决方案1】:

    我想我知道你的意思,它只是“但是,如果检测到名称相同,我将面临编写替换旧文件的代码的困难。”有点混乱。

    看看这个看看它是否有帮助,你可以通过各种方式对其进行修改和更改。 它以稍微不同的方式进行处理,但可以按预期工作。

            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
    

    【讨论】:

    • 嗨 SpringSphinx,让我试试代码。谢谢,祝您有愉快的一天!
    猜你喜欢
    • 1970-01-01
    • 2010-11-15
    • 2023-03-17
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    相关资源
    最近更新 更多