Solved

Confirm vbscript file copy

Posted on 2004-08-19
5
3,518 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

623 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