如果文件存在于目标目录中,并且文件不存在,我需要将其从Mydir移至Desdir。
请提出从MyDir中删除文件的最佳方法(如果文件存在于destDir中,并且不将其移动到destDir中)?谢谢你。
请参见以下代码:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句