Solved

Confirm vbscript file copy

Posted on 2004-08-19
5
3,509 Views
Last Modified: 2008-01-09
Is there a way to confirm a file copy in vbscript?  I need to overwrite a file with another file of the same name, and I want to keep a list of which ones worked and which ones didn't, since the file may be opan at the time of the copy.
Thanks.
0
Comment
Question by:archv6625
  • 3
5 Comments
 
LVL 11

Expert Comment

by:griessh
ID: 11840739
Hi archv6625,
' your copy goes here
' Now checking for File already open
If Err.Number <> 55 Then
' do something here
End If


======
Werner
0
 
LVL 11

Expert Comment

by:griessh
ID: 11840744
Sorry ...

If Err.Number = 55 Then
0
 

Author Comment

by:archv6625
ID: 11840776
Do I need to add any code to check for the file being open, or will vb and the Err object handle that for me?
0
 
LVL 11

Expert Comment

by:griessh
ID: 11841109
If you do the 'copy' and that fails vb should raise an error (55) for that action. I can't test here since I am in a UNIX environment, but I am pretty confident that should work.

Give it a try ...
0
 
LVL 3

Accepted Solution

by:
travisjhall earned 90 total points
ID: 11848337
I don't know how much you know about VBScript, archv6625, but if you are a relative beginner you might need a little more information than you have been given.

To enable error handling in VBScript, use:
On Error Resume Next

To disable it again, use:
On Error Goto 0

Try not to think too much about that "Goto 0". It's sort of legacy syntax, and has largely lost its meaning. Just accept that the statement disables error handling.

Having enabled error handling, the Err object will provide you with information about any errors that occur. Note that a file copy can fail for multiple reasons, and error code 55 relates to only one of those reasons. Off the top of my head, I don't know whether that's the error that you get if you try to copy over a locked file, but we can probably take Werner's word for that. However, you probably want to check for other errors as well. Maybe something like this:

On Error Resume Next
Err.Clear 'Just in case an error has been raised previously - you don't want to be checking the error from the wrong statement
fso.CopyFile "c:\somefile", "c:\someotherfile", True
If Err.Number = 0 Then
  WriteToLog "c:\somefile deleted" 'WriteToLog will be a Sub you have written to handle recording success or failure
Else
  WriteToLog "c:\somefile not deleted - Error " & Err.Number & ": " & Err.Description
End If
On Error Goto 0


This will record success or failure, and an error code and message that might help when it does fail.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 92
Sorting in Excel with Group Headers if the Exist 2 90
groupSumClump challenge 9 115
What is Python programming? 3 113
This is an explanation of a simple data model to help parse a JSON feed
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question