开发者

How to delete file of source after copying in vbscript?

开发者 https://www.devze.com 2023-04-05 22:15 出处:网络
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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消