I am developing function that copy file from Temp Folder to Drive C. After copying, I would like to delete file in Temp Folder. I tried following codes but can't delete file.Please explain it to me.
sample codes:
Set objFSO = CreateObject("Scripting.FileSystemObject")
File = file of Temp Folder
objFSO.CopyFile File, "C:\"
objFSO.DeleteFile(File)
OR
Set objFSO = CreateObject("Scripting.FileSystemObject")
File = f开发者_StackOverflow社区ile of Temp Folder
objFSO.CopyFile File, "C:\"
Set delFileName = objFSO.GetFile(File)
delFileName.Delete delFileName
Copying a file from one location to C:\ and then deleting the version in the original location is the same as moving the file, so do that instead:
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile File, "C:\"
Set objFSO = Nothing
If you really really want to do it in the way you've described then:
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
objFSO.CopyFile File, "C:\"
If Err.Number = 0 Then objFSO.DeleteFile File
On Error Goto 0
Set objFSO = Nothing
will do the trick.
精彩评论