excel - 如果目标目录中存在文件,则从当前目录中删除文件的最佳方法

标签 excel vba

如果文件存在于目标目录中,我需要删除它,如果它不存在,则将文件从 Mydir 移动到 Desdir。

如果文件存在于 destDir 中并且不将其移动到 destDir,请建议从 MyDir 中删除文件的最佳方法?谢谢你。

请看以下代码:

On Error Resume Next
        If Dir(destDir & "\" & strFileName2) = "" Then'//file doesn't exist in destDir
            FileSys.MoveFile Source:=myDir & "\" & strFileName2,   Destination:=destDir & "\" '//move the file
        Else
        '//delete this file from myDir and do not move the file
        'code?
        End If
        On Error GoTo 0

最佳答案

删除文件的一种方法:

Kill myDir & "\" & strFileName2    'Kill "C:\test\Test.txt"

.

另一种方法是使用 FSO (FileSystemObject)
Option Explicit

Public Sub delFile(ByVal fPath As String, ByVal fName As String)

    If Len(Dir(folderID, vbDirectory)) > 0 Then

        If Len(Dir(folderID & fileID)) > 0 Then

            If Right(fPath, 1) <> "\" Then fPath = fPath & "\"

            'Kill fPath & fName

            CreateObject("Scripting.FileSystemObject").DeleteFile fPath & fName

        End If
    End If
End Sub

关于excel - 如果目标目录中存在文件,则从当前目录中删除文件的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32685093/

相关文章:

excel - 检查整行是否具有值 0

c# - Excel 到 PDF C# 库

excel - WScript.Shell Exec 的实时控制台输出

VBA本地时区调整

excel - 复制和粘贴具有随机值的单元格

MYSQL 语句在框架中输入时工作,保存 mysql 时无法让框架使用函数

html - 如何在不使用 Sendkeys 的情况下在 Excel 中运行网站表单?

VBA复制行高

vba - 在 Excel 2010 上寻找更好定义的范围?

vba - VBA 中指针大小的真正决定因素是什么?