I'm trying to make a script that will allow me to copy a specified file to a predefined list of PCs (C:\PCs.txt). I'd like to have this script write to a separate file (C:\MassCopyError.txt) for each PC that fails, then notify me when it's finished.
So far, I'm able to copy the file without error, and I'm able to write the first PC that fails, but I can't get it to write more than one error, and I can't get it to notify me when it's finished. It just copies, notifies me there's been one error, logs one error, and closes out. Any ideas what I'm doing wrong? I know this would be easier in pure VB or even C++, but I'm trying to do it in VBS.
<code> On Error Resume Next Const ForReading = 1 Const ForAppending = 8 Const OverwriteExisting = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("C:\PCs.txt", ForReading) dim currentFileLocation dim newFileLocation currentFileLocation=InputBox("What is the file's current location?", "Current File Location") newFileLocation=InputBox("Where would you like the file to go?", "New File Location")
Do Until objFile.AtEndOfStream On Error Resume Next strComputer = objFile.ReadLine strRemoteFile = "\\" & strComputer & "\" & newFileLocation & "\" objFSO.CopyFile currentFileLocation, strRemoteFile, OverwriteExisting Loop
If Err.Number <> 0 Then wscript.Echo "An error has occurred. Please see C:\MassCopyError.txt for details" Set objFile = objFSO.CreateTextFile("C:\MassCopyError.txt") Set objFile = obj.FSO.OpenTextFile ("C:\MassCopyError.txt", ForAppending, True) objFile.WriteLine(strComputer&vbCrLf) objFile.Close Err.Clear End If
wscipt.Echo "MassCopy has finished copying files." </code>
This conversation is currently closed to new comments.
I had the error check out of context. This was causing loop issues. Thanks to Tom Lavedas of MSTechNet for the solution. here's my final script for anyone needing a similar solution: <code> '-------------------------------------------------' '= MassCopy v1.0 =' '= (XP Only) =' '= Author: Alex Cosby =' '= Date: 08/19/10 =' '= Note: In order to work, PC must Ping =' '-------------------------------------------------' Const ForReading = 1 Const ForAppending = 8 Const OverwriteExisting = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("C:\PCs.txt", ForReading) Set objLogFile = objFSO.OpenTextFile("C:\MassCopyError.txt", ForAppending, True)
dim currentFileLocation dim newFileLocation currentFileLocation=InputBox("What is the file's current location?", "Current File Location") newFileLocation=InputBox("Where would you like the file to go?", "New File Location")
Do Until objFile.AtEndOfStream strComputer = objFile.ReadLine strRemoteFile = "\\" & strComputer & "\" & newFileLocation & "\"
On Error Resume Next objFSO.CopyFile currentFileLocation, strRemoteFile, OverwriteExisting nErr = Err.Number on error goto 0 ' restores VBS error trapping
If nErr <> 0 Then ' Uncomment below to be prompted at each error. ' wscript.Echo "An error has occurred. Please see C:\MassCopyError.txt for details." objLogFile.WriteLine strComputer & vbCrLf End If Loop
objFile.Close
wscript.Echo "MassCopy has finished. Check C:\MassCopyError.txt for any errors." </code>
If you're asking for technical help, please be sure to include all your system info, including operating system, model number, and any other specifics related to the problem. Also please exercise your best judgment when posting in the forums--revealing personal information such as your e-mail address, telephone number, and address is not recommended.
VBS Error Handling and Loops
So far, I'm able to copy the file without error, and I'm able to write the first PC that fails, but I can't get it to write more than one error, and I can't get it to notify me when it's finished. It just copies, notifies me there's been one error, logs one error, and closes out. Any ideas what I'm doing wrong? I know this would be easier in pure VB or even C++, but I'm trying to do it in VBS.
<code>
On Error Resume Next
Const ForReading = 1
Const ForAppending = 8
Const OverwriteExisting = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\PCs.txt", ForReading)
dim currentFileLocation
dim newFileLocation
currentFileLocation=InputBox("What is the file's current location?", "Current File Location")
newFileLocation=InputBox("Where would you like the file to go?", "New File Location")
Do Until objFile.AtEndOfStream
On Error Resume Next
strComputer = objFile.ReadLine
strRemoteFile = "\\" & strComputer & "\" & newFileLocation & "\"
objFSO.CopyFile currentFileLocation, strRemoteFile, OverwriteExisting
Loop
If Err.Number <> 0 Then
wscript.Echo "An error has occurred. Please see C:\MassCopyError.txt for details"
Set objFile = objFSO.CreateTextFile("C:\MassCopyError.txt")
Set objFile = obj.FSO.OpenTextFile ("C:\MassCopyError.txt", ForAppending, True)
objFile.WriteLine(strComputer&vbCrLf)
objFile.Close
Err.Clear
End If
wscipt.Echo "MassCopy has finished copying files."
</code>