How does the FSO Copy function handle files that fail CRC or cannot be read.

wpeterb
wpeterb used Ask the Experts™
on
I have a script to copy files from a users machine for "Backup" purposes. The script uses the FSO Copy.

During a copy if the operation hits a file that is corrupt it stops.
Is there a way to skip or reume copying? any switch or method?
 
'CopyUserData.vbs
Option Explicit
On Error Resume Next

Const MY_COMPUTER   = &H11&
Const WINDOW_HANDLE = 0 ' Must ALWAYS be 0
Dim numOptions, objFolder, objFolderItem, BrowseToFolder
Dim objPath, objShell, strPath, strPrompt, strDirectory
Dim objFSO, objFileCopy, strFilePath, strDestination, strArchive
Dim strComputer, blnSimpleDialog, myStartLocation, strFileText
Dim arrA, strX, strB, strC, count

strComputer = "." 
count = 0

'Set the options for the dialog window
strPrompt = "Select a folder:"
If blnSimpleDialog = True Then
	numOptions = 0      ' Simple dialog
Else
	numOptions = &H10&  ' Additional text field to type folder path
End If	

' Create a Windows Shell object
Set objShell = CreateObject( "Shell.Application" )
' If specified, convert "My Computer" to a valid
' path for the Windows Shell's BrowseFolder method
If UCase( myStartLocation ) = "MY COMPUTER" Then
	Set objFolder = objShell.Namespace( MY_COMPUTER )
	Set objFolderItem = objFolder.Self
	strPath = objFolderItem.Path
Else
	strPath = myStartLocation
End If

Set objFolder = objShell.BrowseForFolder( WINDOW_HANDLE, strPrompt, numOptions, strPath )
' Quit if no folder was selected
If objFolder Is Nothing Then
    	BrowseFolder = ""
	WScript.Echo "Nothing"
	Cleanup()
    	WScript.Quit
End If
	
' Retrieve the path of the selected folder
Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path
'WScript.Echo objPath & " " & "Path"
' Return the path of the selected folder
BrowseToFolder = objPath
'WScript.Echo BrowseToFolder & " " & "Path"
'''''''''''''''''
' Create FileSystemObject. So we can apply .createFolder method
Set objFSO = CreateObject("Scripting.FileSystemObject")

strDirectory = (InputBox(" Location to Save Files", "Copy Location")) 
'strDirectory = a split string to the first \
arrA =Split(strDirectory,"\")
for each strX in arrA
	if count < 1 then		
		strB = strX & "\"
		'WScript.Echo strB 	
	else		
		strB = strB + strX & "\"
		strC = strB
		'WScript.Echo strC 		
		Create_Folder(strC)		
	end if
Count = Count +1
next

' Note If..Exists. Then, Else ... End If construction
Function Create_Folder(strC)
strC = ""  & strC & ""  
If objFSO.FolderExists(strC) Then
   Set objFolder = objFSO.GetFolder(strC)
   WScript.Echo strC & " already created "	
Else
   Set objFolder = objFSO.CreateFolder(strC)
'WScript.Echo "Just created " & strC
End If

End Function

WScript.Echo strC
WScript.Echo BrowseToFolder & " " & "Path"
strDestination = strC

'WSCript.Echo "Written " & strFileText & " to " & strFilePath
'Set objFileCopy = objFSO.GetFolder(strFilePath)
Set objFileCopy = objFSO.GetFolder(BrowseToFolder)
' Copy the file to its destination
objFileCopy.Copy (strDestination)
WSCript.Echo "Copied to " & strDestination

''''''''''''''''''''
Function Cleanup
Set objUser = Nothing
Set objGroup = Nothing
Set objSheet = Nothing
Set objExcel = Nothing
WScript.Echo "Bye"
Wscript.Quit
End Function


' Clean Up
Set objFolder = Nothing
Set objFolderItem = Nothing
Set objPath = Nothing
Set objShell = Nothing
Set objFSO = Nothing

WScript.Echo "Copy is done"
WScript.Quit

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can find where the error is occurring and put this before it:
   On Error Resume Next
If you put this line on top, all errors will be suppressed.

If you want to trap errors after that line you can put this line to resume error trapping:
   On Error Goto 0
Oops, you did have "On Error Resume Next", do you know when it stops?
If you don't see any error messages, you can keep a log througout the file to let you know which line it stopped at...

Author

Commented:
Yes the error message would be like Failed Cyclical Redundancy Check or File Unable to be Copied.

As you see I did have "On Error Resume Next" but it exits the routine. I expect it to move to the next file and continue. I do not think enumerating each file in every folder and looping through each file to perform a copy is the best way either. That is why I am trying to use the Copy method.

I was hoping there was a switch or trap I could do to catch the error and then skip the bad file.

There must be some mechanism for handling a folder copy that contains a corrupt file.

If I add logging is ther an error code thrown that will be written?

Thanks,
Pete

Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Assuming this is the line that catches invalid file:
objFileCopy.Copy (strDestination)

The code below should show you the error code.
sew

On Error Resume Next 
objFileCopy.Copy (strDestination)    ' <=== assume it fails here!!

If Err Then
	MsgBox "Error number: " & Err.Number & vbCrLf & _
	       "Error description: " & Err.Description
End If

Open in new window

Commented:
This may take me 24hrs to test due to the laptop's availability. I don't have a corrupt file here to test.

Thanks,

Pete

Author

Commented:
I am unable to recreate the error. However adding the Logging will enable me to locate the error.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial