如果文件存在于目标目录中,我需要删除它,如果它不存在,则将文件从 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/