How can I perform file-copy checking using VBScript?

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:\temp\blob.file g:\data\
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

File copy problem 70 Permission denied

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish