• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 64
  • Last Modified:

vbs CopyFile not working correctly

I have a script that just stopped working a few days ago, when it had been working for the past three months. The script runs from a user's local drive, and only fails for a few users with a "File not found" error. Both popups are displayed, so I know both source and destination exist. The strange thing is, I added the last if statement when trying to figure out this issue, and that copy works. Has anyone ever seen this happen? What am I missing here?

set ws = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
  
strLocalFolder = "C:\USERS\MyLocalFolder\" 

strRemoteFile = "\\ServerName\RemoteFolder\test.dat"

If (objFSO.FileExists (strRemoteFile )) Then 
	ws.Popup "Found file " & strRemoteFile 
End If

If (objFSO.FolderExists(strLocalFolder )) Then 
	ws.Popup "Found folder " & strLocalFolder 
End If

objFSO.CopyFile strRemoteFile, strLocalFolder, True

If (objFSO.FolderExists(strLocalFolder )) Then 
               objFSO.CopyFile  strRemoteFile, strLocalFolder, True
End If

Open in new window

0
Michael Woods
Asked:
Michael Woods
  • 4
  • 2
2 Solutions
 
Bill PrewCommented:
One thought, or maybe two...

Does the file you are copying get updated regularly.  If so, perhaps if the copy runs when it is being updated it is either locked out of the file, or the file may appear missing for that brief instant.

Or perhaps this is an occasional network blip.

Either way, you could do error trapping in the script and if the file or folder doesn't exist sleep for a few seconds and try again.  Maybe repeat that recovery attempt for a few minutes before raising an error message and exiting the script.


»bp
0
 
Michael WoodsAuthor Commented:
Thanks Bill. The file gets created once a month, and the process that creates it call this script when it is complete. Even when I try to copy it now it doesn't work.  The error trapping/sleep solution was something I thought about, but was hoping that I could find the cause of this issue.
0
 
Bill PrewCommented:
What line is referenced in the error message when the "File not found" error occurs?


»bp
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Bill PrewCommented:
Are you running this as CSCRIPT or WSCRIPT?


»bp
0
 
Bill PrewCommented:
Here's a slightly different way to get at it, and adds just a little bit of error detection and display.  Might be an alternate approach you could try.

strRemoteFile = "\\ServerName\RemoteFolder\test.dat"
strLocalFolder = "C:\USERS\MyLocalFolder\" 

Set objFSO = CreateObject("Scripting.FileSystemObject")

On Error Resume Next

If objFSO.FileExists(strRemoteFile) Then 
    If objFSO.FolderExists(strLocalFolder) Then 
        Set objFile = objFSO.GetFile(strRemoteFile)
        If Err.Number = 0 Then
            objFile.Copy strLocalFolder, True
            If Err.Number <> 0 Then
                Wscript.Echo "Error copying file: [" & strRemoteFile & "] -> [" & strLocalFolder & "]"
                Wscript.Echo "[" & Err.Number & "] " & Err.Description
                Err.Clear
            End If
        Else
            Wscript.Echo "Error accessing file: [" & strRemoteFile & "]"
            Wscript.Echo "[" & Err.Number & "] " & Err.Description
            Err.Clear
        End If
    Else
        Wscript.Echo "Folder does not exist: [" & strLocalFolder & "]"
    End If
Else
    Wscript.Echo "File does not exist: [" & strRemoteFile & "]"
End If

Open in new window


»bp
0
 
Michael WoodsAuthor Commented:
The copy on line 16 is where the error appears.
We are running WSCRIPT.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now