How can I perform file-copy checking using VBScript?

John Savill

April 17, 2007

1 Min Read
ITPro Today logo in a gray background | ITPro Today

A. You can check file-copy operations by using the Err value, and after the copy, you can perform a FileExists operation to confirm that the new file exists, as you see in the code below. You can save this script, passing a folder and filename as the first parameter, and the folder to copy to as the second the folder:

On Error Resume Next
Dim objFSO, sourceFile, targetFolder
set objFSO=CreateObject("Scripting.FileSystemObject")
set sourceFile = objFSO.GetFile(WScript.Arguments(0))
set targetFolder = objFSO.GetFolder(WScript.Arguments(1))
objFSO.CopyFile sourceFile, targetFolder
If Err.Number 0 Then
Wscript.Echo "File copy problem " & err.number, err.description
End If
If objFSO.FileExists(targetFolder.Path & "" & sourceFile.Name) Then
Wscript.Echo "File Copied"
End If

You can add additional logic and perform other actions in the scripts, if necessary. Here is the script running normally:

D:Temp>cscript copyfilecheck.vbs d:tempblob.file g:data
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

File copy problem 70 Permission denied

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like