Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Confirm vbscript file copy

Posted on 2004-08-19
5
Medium Priority
?
3,528 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 360 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Screencast - Getting to Know the Pipeline

916 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