

InterviewSolution
Saved Bookmarks
1. |
Solve : Is there anyway to make a vbscript close if it has an error?? |
Answer» Is there anything i can add to my vbscript to make it close if it is going to show an error, instead of showing an error.?To be more specific my the vbscript has to locate a file, im very new to vbscripts i just want to make it if it cant find the file then to simply move on to the rest of the function.Use On Error Resume Next, and after trying to access the file: Use On Error Resume Next, and after trying to access the file:on error resume next continues processing. I though he wanted on error goto 0Quote from: gh0std0g74 on December 07, 2009, 10:57:18 PM on error resume next continues processing. I though he wanted on error goto 0 Quote Is there anything i can add to my vbscript to make it close if it is going to show an error, instead of showing an error.? On Error Goto 0 disables error handling ALTOGETHER; the error will be shown and the script stopped.It's not working for me i still get an error, but this time not for the path. Dim objFileSystem, objOutputFile Dim strOutputFile Const OPEN_FILE_FOR_APPENDING = 8 ' generate a filename base on the script name strOutputFile = "C:\bla.txt" If Err =0 then 'rest of routine End If On Error Resume Next Set objFileSystem = CreateObject("Scripting.fileSystemObject") Set objOutputFile = objFileSystem.OpenTextFile(strOutputFile, _ OPEN_FILE_FOR_APPENDING) IS this right? I get an expected statement error in the last sentences.the comma is a comment. the "'rest of routine" comment was not actual code but a comment on how to use it. the OpenTextFile() method is the main way an error will be thrown. On Error Resume Next continues and stores the error number in err; by using On Error Resume next immediately before the CreateTextFile Method, you can check to see if an error occured opening the file; if not, (err=0) you do whatever you are doing with the file; otherwise, you go on without showing the error dialog. Another possibility, if this script is being executed in a batch, you can use Cscript instead of WScript. The error will be DISPLAYED on the command LINE and batch processing will continue.I am still ending a file not found error. This is what i have : Dim objFileSystem, objOutputFile Dim strOutputFile Const OPEN_FILE_FOR_APPENDING = 8 strOutputFile = "C:\bla.txt" If Err =0 then End If Set objFileSystem = CreateObject("Scripting.fileSystemObject") Set objOutputFile = objFileSystem.OpenTextFile(strOutputFile, _ OPEN_FILE_FOR_APPENDING) objOutputFile.WriteLine(" ") objOutputFile.Close Set objFileSystem = NothingCode: [Select]Dim objFileSystem, objOutputFile Dim strOutputFile Const OPEN_FILE_FOR_APPENDING = 8 strOutputFile = "C:\bla.txt" Set objFileSystem = CreateObject("Scripting.fileSystemObject") If objFileSystem.FileExists( strOutputFile ) Then Set objOutputFile = objFileSystem.OpenTextFile(strOutputFile, _ OPEN_FILE_FOR_APPENDING) objOutputFile.WriteLine(" ") objOutputFile.Close Else MsgBox strOutputFile & " not found" End If Set objFileSystem = Nothing Oldun i tried your scipt, i get a completely seperate error that says "C:\bla.txt not found".Quote from: mikefresia on December 10, 2009, 02:41:22 PM I am still ending a file not found error. Quote from: mikefresia on December 10, 2009, 10:49:24 PM Oldun i tried your scipt, i get a completely seperate error that says "C:\bla.txt not found". They are the same error. The FIRST time you got the error, it was generated by this statement: Quote Set objOutputFile = objFileSystem.OpenTextFile(strOutputFile, _ It could have been written as: Quote Set objOutputFile = objFileSystem.OpenTextFile(strOutputFile, _ to create the file (C:\bla.txt) if it doesn't exist, which apparently it doesn't. The second time you got the error, it was generated by this statement: Quote MsgBox strOutputFile & " not found" This approach was more direct in that it checked to see if the file existed and if not, generated the error message. In either case, there was no more code in the script (that we could see), so the script went to end of job. Had there been more code you could use the WScript.Quit statement to end your script at any time. |
|