Solved

Confirm vbscript file copy

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scores100 challenge 3 82
Sorting in Excel with Group Headers if the Exist 2 68
mapBully challenge 6 89
Magic Software info 18 102
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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.
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

708 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

19 Experts available now in Live!

Get 1:1 Help Now