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

Michael WoodsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.