Link to home
Start Free TrialLog in
Avatar of halfondj
halfondj

asked on

MoveFile/CopyFile in VBS and error handling

The MoveFile and CopyFile methods in VBS does not return a code specifiying if there was an error or not performing the call.  What's the best way to know if a file was moved or copied?  Doing a FileExists doesn't seem to guarantee that the file was copied completely.

The files being copied are on a local PC and the destination is to an FTP server using WebDrive.

Thanks.
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

You can add the standard exception method to your program:
private sub Start
'Begin of script
On Error GoTo StandardErrorHandler

' All your functions, like movefile, ...

  exit sub
StandardErrorHandler:
msgbox "copy failed"

end sub
try also
If Len(Trim(Dir("yourpath\yourfile,vbNormal))> 0 Then
     'File is there

end if
Avatar of halfondj
halfondj

ASKER

I'm using VBS, not VB.  It's my understanding that one can only use 'On Error Resume Next' in VBS.  For a test, I've implemented the following code, but if I disconnected the mapped drive (the drive that was mapped via WebDrive) when the CopyFile was taking place, the CopyFile still returned successful, not unsuccessful.

I need to ensure that the CopyFile/MoveFile returns unsuccessful, when in fact, the file did not copy fully to the destination folder.

Here's the test code snippet:

:
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

on error resume next
objFSO.CopyFile "x:\bigtestfile.txt", "q:\bigtestfile.txt"

if err.number<>0 then
    set objFSO = Nothing

    wscript.echo err.description
       
    'wscript.echo "Path not found. Operation aborted."
    wscript.quit(1)
end if
on error goto 0

wscript.echo "looks good"
:
ASKER CERTIFIED SOLUTION
Avatar of vinnyd79
vinnyd79

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To Marper:  I liked the idea of your ShowDrive function, but if the drvpath doesn't exist, e.g. if the drive mapped via WebDrive disconnects, just like if a network drive disconnects unexpectly, then the GetDrive method returns an runtime error.

I implemented a test to see if the drive is mapped by doing the MapDrive function below - any ideas of changing the routine is welcomed.  I don't like the idea that I have to query all the drives mapped to see if the one I need is really mapped.

Taken from the Windows Script Help File - "An error occurs if drivespec does not conform to one of the accepted forms or does not exist."

To Vinnyd79: After I posted my question, I came up with a similar idea to what you suggested, re:getting the file size of the source/dest files after performing the copy.  I would like to grant you the points, but will hold off until I see if anyone can suggest a better routine for checking if a drive is mapped or not.  Any suggestions?

Thanks for responding.

---------------------------------------------------------
Function MapDrive(ByVal v_bConnect)
        :
        'Check to be sure that the FTP site has a mapped drive.
        Set colDrives = objFSO.Drives

        MapDrive = False
       
        For Each objDrive In colDrives  
            If objDrive.DriveLetter = cFTP_DRV_LTR Then
                If objDrive.IsReady = True Then
          MapDrive = True
                End If
       
               Exit For
           End If
     Next
       
    Set colDrives = Nothing
   :
End Function
halfondj,
you should close this question and award the points to someone who helped you the most.
That's how it works here.
When the members see you are not closing your questions, they will not help you again in the future.
I certainly don't mind awarding the points, but what if the entire question was not answered?  The question is worth 500 points.  Can't I wait a little while longer?

Please advise.

Thanks.
sure, it's OK to wait longer.
I have just tried to give you a friendly advise.
Hope you will get a right solution soon. I am looking at it too.

Regards,
Thanks for your suggestions.  Here's what I came up with:

Sub CopyFiles(ByVal v_sSrcFolder, ByVal v_sFileSpec, ByVal v_sDestFolder)

             :
             :
           
            'Get the source file's size - used to compare to destination's after the file copy.
            Set fSrcFile = objFSO.GetFile(sFullSrcFileName)
           
            'Turn on error handling.
            On Error Resume Next

            'Allow the user to retry 'x' amount of times.
            For iCnt = 1 To cRETRY_CNT
                'Copy the file.
                objFSO.CopyFile sFullSrcFileName, v_sDestFolder & "\", True

                'Confirm that the file was copied to the destination folder.
                Set fDestFile = objFSO.GetFile(sFullDestFileName)
               
                'If the object returned is empty then the file did not get copied.
                If fDestFile <> Empty Then
                    'If the file sizes are equal, the copy was successful.
                    If fDestFile.Size = fSrcFile.Size Then
                        bNoErrFl = True

                        g_fsoLogFile.WriteLine("    Src File Copied .......... " & sFile & ", " & fSrcFile.Size & " bytes")

                        Exit For
                    Else
                        sMsg = "The [" & sFullSrcFileName & "] was copied to [" & v_sDestFolder & "], but " & vbCrLf & _
                               "the source and destination file sizes are not equal." & vbCrLf & _
                               "Press 'Retry' to retry the copy or press 'Abort' to terminate the script." & vbCrLf & _
                               "No. Of Retries: " & iCnt & " of " & cRETRY_CNT
                                 
                        iRC = MsgBox(sMsg, vbCritical + vbRetryCancel, "Error: " & WScript.ScriptName)

                        g_fsoLogFile.WriteLine()
                        g_fsoLogFile.WriteLine(sMsg)
           
                        'User pressed the 'Retry' button.
                        If iRC = vbRetry Then
                            g_fsoLogFile.WriteLine("User chose to retry.")
                        Else
                            g_fsoLogFile.WriteLine("User chose to terminate the script.")
                                               
                            Exit For
                        End If
                    End If
                Else
                    sMsg = "The file [" & sFullSrcFileName & "] cannot be copied to [" & v_sDestFolder & "]." & vbCrLf & _
                           "The file's drive mapping may be disconnected.  Check the mapping" & vbCrLf & _
                           "and if necessary, manually map the drive and press 'Retry' or press" & vbCrLf & _
                           "'Abort' to terminate the script." & vbCrLf & _
                           "No. Of Retries: " & iCnt & " of " & cRETRY_CNT
                                 
                    iRC = MsgBox(sMsg, vbCritical + vbRetryCancel, "Error: " & WScript.ScriptName)
               
                    g_fsoLogFile.WriteLine()        
                    g_fsoLogFile.WriteLine(sMsg)
                   
                    'User pressed the 'Retry' button.
                    If iRC = vbRetry Then
                        g_fsoLogFile.WriteLine("User chose to retry.")
                    Else
                        g_fsoLogFile.WriteLine("User chose to terminate the script.")
                                               
                        Exit For
                    End If
                End If
            Next

            'Turn off error handling.
            On Error Goto 0
           
            'If the file cannot be copied, tell the user that the script will be terminated.
            If Not bNoErrFl Then
                sMsg = "There was an error copying the file [" & sFullSrcFileName & "] to [" & _
                       v_sDestFolder & "]." & vbCrLf & _
                       "The script will now terminate."
               
                g_fsoLogFile.WriteLine()
                g_fsoLogFile.WriteLine(sMsg)
               
                MsgBox sMsg, vbCritical, "Error: " & WScript.ScriptName
               
                Exit For
            End If
        Next

        Set objFSO = Nothing
    End If

    Set colFiles = Nothing
    Set fSrcFile = Nothing
    Set fDestFile = Nothing    

    'If there was an error when trying to copy the file(s), then terminate the script.
    If Not bNoErrFl Then
        Cleanup()
        WScript.Quit
    End If
End Sub