Home > on error > createtextfile error handling

createtextfile error handling

213 Scores: 0 Reward points : 0 Joined: 3/10/2009Location: Seattle, WA Status: offline VBscript error caused by open file Tuesday, January 04, 2011 5:04 AM

Vbscript On Error Goto

(permalink) 0 Hi, I wrote a script that does an application installation vbscript on error resume next and outputs messages to a log file as the script progresses. If I run the script and it fails

Vbscript Error Handling Best Practices

due to a runtime issue, the line that closes the log file never runs which leaves the file open and locked. How would I capture the error when the script fails at error handling in vbscript tutorial the open log file step and perform any additional steps? I'm thinking I should capture the error, if an error condition exists, close the open file and then reattempt opening the file again so the script can continue normally.  The failure I am trying to address occurred at "Set objFile = objFSO.CreateTextFile(strDirectory & strFile)". Here is a code snippet:   Dim objFSO, objFolder, vbscript on error goto sub objShell, objLogFile, objFile Dim strDirectory, strFile 'Variables used for storing current date and time to enter in the log Dim dateStamp, timeStamp dateStamp = Date() timeStamp = Time() 'Declare and initialize variables needed for log file output strDirectory = "c:\Windows\Temp\" strFile = "Microsoft_Lync.log" 'Create the File System Object Set objFSO = CreateObject("Scripting.FileSystemObject") 'Create log file (replaces existing log file) Set objFile = objFSO.CreateTextFile(strDirectory & strFile) 'Open log file for writing Set objFile = Nothing Set objFolder = Nothing Set objLogFile = objFSO.OpenTextFile (strDirectory & strFile, 2, True) 'Output time and date stamps to log file objLogFile.WriteLine(timeStamp & "  " & dateStamp) objLogFile.WriteLine 'Close log file objLogFile.Close Thanks for your help! Rob #1 rasimmer Total Posts : 2386 Scores: 165 Reward points : 0 Joined: 3/19/2009Location: Richmond, VA Status: offline Re:VBscript error caused by open file Tuesday, January 04, 2011 9:13 AM (permalink) 0 You don't even need that line.  When you do an .OpenTextFile with the True, it will create the file if it does not exist.  In any event, error handling would be th

United States Australia United Kingdom Japan Newsletters Forums Resource Library Tech Pro Free Trial Membership Membership My Profile People Subscriptions My stuff Preferences Send a message Log Out TechRepublic Search GO Topics:

Vbscript On Error Exit

CXO Cloud Big Data Security Innovation Software Data Centers Networking Startups Tech & vbscript error handling line number Work All Topics Sections: Photos Videos All Writers Newsletters Forums Resource Library Tech Pro Free Trial Editions: US United States

On Error Resume Next Vbscript W3schools

Australia United Kingdom Japan Membership Membership My Profile People Subscriptions My stuff Preferences Send a message Log Out TechRepublic | Forums | Web Development Web Development Register Now or Log In to http://www.visualbasicscript.com/VBscript-error-caused-by-open-file-m88294.aspx post Welcome back, My Profile Log Out Recent Activity FAQs Guidelines Question 0 Votes Locked VBS Error Handling and Loops By alxcsby · 6 years ago 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 http://www.techrepublic.com/forums/discussions/vbs-error-handling-and-loops/ 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. On Error Resume NextConst ForReading = 1Const ForAppending = 8Const OverwriteExisting = TRUESet objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.OpenTextFile("C:\PCs.txt", ForReading)dim currentFileLocationdim newFileLocationcurrentFileLocation=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, OverwriteExistingLoop 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 Ifwscipt.Echo "MassCopy has finished copying files." Share Flag This conversation is currently closed to new

resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine https://msdn.microsoft.com/en-us/library/5t9b5c0c(v=vs.84).aspx Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired content Samples We’re sorry. The content you requested has been removed. You’ll be auto redirected in https://www.safaribooksonline.com/library/view/vbscript-in-a/0596004885/ch04.html 1 second. FileSystemObject FileSystemObject Reference (Windows Scripting) FileSystemObject Methods FileSystemObject Methods CreateTextFile Method CreateTextFile Method CreateTextFile Method Add Method (Folders) BuildPath Method Close Method (FileSystemObject) Copy on error Method (FileSystemObject) CopyFile Method CopyFolder Method CreateFolder Method CreateTextFile Method Delete Method DeleteFile Method DeleteFolder Method DriveExists Method FileExists Method FolderExists Method GetAbsolutePathName Method GetBaseName Method GetDrive Method GetDriveName Method GetExtensionName Method GetFile Method GetFileName Method GetFileVersion Method GetFolder Method GetParentFolderName Method GetSpecialFolder Method GetStandardStream Method GetTempName Method Move Method MoveFile Method MoveFolder Method vbscript on error OpenAsTextStream Method OpenTextFile Method Read Method ReadAll Method ReadLine Method Skip Method SkipLine Method Write Method (FileSystemObject) WriteBlankLines Method WriteLine Method (FileSystemObject) TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. CreateTextFile Method  Creates a specified file name and returns a TextStream object that can be used to read from or write to the file.Syntax Copy object.CreateTextFile(filename[, overwrite[, unicode]]) ArgumentsobjectRequired. Always the name of a FileSystemObject or Folder object.filenameRequired. String expression that identifies the file to create.overwriteOptional. Boolean value that indicates whether you can overwrite an existing file. The value is true if the file can be overwritten, false if it can't be overwritten. If omitted, existing files are not overwritten.unicodeOptional. Boolean value that indicates whether the file is created as a Unicode or ASCII file. The value is true if the file is created as a Unicode file, f

Pages Close VBScript in a Nutshell, 2nd Edition by Paul Lomax... Published by O'Reilly Media, Inc. VBScript in a Nutshell, 2nd Edition Foreword Preface Why This Book? Who Should Read This Book? How This Book Should Be Used If You're New to VBScript If You're a VBScript Programmer If You're a VB or VBA Developer New to VBScript How This Book Is Structured The Format of the Language Reference Conventions in This Book How To Contact Us Acknowledgments I. The Basics 1. Introduction 1.1. VBScript's History and Uses 1.2. What VBScript Is Used For: Gluing Together Objects 1.3. Differences Between VBScript and VBA 2. Program Structure 2.1. Functions and Procedures 2.2. Classes 2.3. Global Code 2.4. Reusable Code Libraries 3. Data Types and Variables 3.1. VBScript Data Types: The Many Faces of the Variant 3.2. Variables and Constants 4. Error Handling and Debugging 4.1. Debugging 4.2. Error Handling 4.3. Common Problem Areas and How to Avoid Them 5. VBScript with Active Server Pages 5.1. How ASP Works 5.2. Active Server Pages Object Model 6. Programming Outlook Forms 6.1. Why Program Outlook Forms? 6.2. The Form-Based Development Environment 6.3. Running Your Code 6.4. Program Flow 6.5. The Outlook Object Model 6.6. Accessing Other Object Models 7. Windows Script Host 5.6 7.1. Why Use WSH? 7.2. Running WSH Scripts 7.3. Program Flow 7.4. The WSH Object Model 7.5. WSH Language Elements 7.6. Accessing Other Object Models 8. VBScript with Internet Explorer 8.1. The

web
stats