Solved

Confirm vbscript file copy

Posted on 2004-08-19
5
3,507 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now