Re: Really Baffled any clues as to what is going on?
- From: "Michael Harris \(MVP\)" <mikhar at mvps dot org>
- Date: Wed, 31 Aug 2005 18:05:39 -0700
Frederick wrote:
> I am working on a script, it has an input file of machine names. I am
> prompting for the name of that file and doing some error handling if
> the file does not exist or the iput is blank. When the logic is in
> the MAIN body of the script everything works great! Here is the logic
> in as a part of MAIN.
>
It's a bad idea to use 'On Error Resume Next' (inline error handling) this
way.
'On Error Resume Next' should never be in effect for more than a *very*
short span of statements (the one with the potential to throw the error and
the statements to check for the error. Otherwise use 'On Error GoTo 0' to
go back to having the host for the script catch and report unexpected error.
In your specific case, inline error handling with On Error isn't even
necessary...
bFileExists = objFS.FileExists(fstrInputFile)
....and only proceed with processing if bFileExists is true.
> __________________________________________________________________
> On Error Resume Next
> Set objFS = CreateObject("Scripting.FileSystemObject")
>
> Dim fstrInputFile
> Dim fstrLogFilePath
> Dim fstrErrMsg
> Dim fServerList
>
> ' Get Name of Input File and Check to see if its valid
> fstrInputFile = InputBox("Enter name of file containing machines to
> modify Including Full path)","Machines To
> Modify","C:\MigrationTools\ListOfWorkstations.txt")
>
> Set fServerList = objFS.OpenTextFile(fstrInputFile)
>
> If fstrInputFile = "" Then
> MsgBox ("Operation Cancelled, no input file supplied")
> Wscript.Quit(1)
> ElseIf Err Then
> MsgBox ("Error: "& Err.Description)
> Wscript.Quit(1)
> End if
>
> ' Get Path to Store Log File and Check to see if its valid and
> Writable fstrLogFilePath = InputBox("Enter the directory to store the
> Log File","Log File Path","C:\MigrationTools\Logs")
>
> If fstrLogFilePath = "" Then
> MsgBox ("Operation Cancelled, no path for the log file supplied")
> Wscript.Quit(1)
> End if
> ____________________________________________________________________
>
> Now if I move this logic down in to a function and put the results
> into and array, everything works as long as there fstrInputFile
> acutally exisits, otherwise at the Set fServerList =
> objFS.OpenTextFile(fstrInputFile) line the script just dies. Help
> would be appericated.
>
> Here is the logic in a sub..
> ____________________________________________________________________
> On Error Resume Next
> Set objFS = CreateObject("Scripting.FileSystemObject")
>
> 'Get the path to required Inputfile And the Logfile path
> GetPathArray = Split(GetPath,",")
> strInputFile = GetPathArray(0)
> strLogFilePath = GetPathArray(1)
>
> Function GetPath
> Dim fstrInputFile
> Dim fstrLogFilePath
> Dim fstrErrMsg
> Dim fServerList
>
> ' Get Name of Input File and Check to see if its valid
> fstrInputFile = InputBox("Enter name of file containing machines to
> modify (Including full path)","Machines To
> Modify","C:\MigrationTools\ListOfWorkstations.txt")
>
> Set fServerList = objFS.OpenTextFile(fstrInputFile)
>
> If fstrInputFile = "" Then
> MsgBox ("Operation Cancelled, no input file supplied")
> Wscript.Quit(1)
> ElseIf Err Then
> MsgBox ("Error: "& Err.Description)
> Wscript.Quit(1)
> End if
>
> ' Get Path to Store Log File and Check to see if its valid and
> Writable fstrLogFilePath = InputBox("Enter the directory to store the
> Log File","Log File Path","C:\MigrationTools\Logs")
>
> If fstrLogFilePath = "" Then
> MsgBox ("Operation Cancelled, no path for the log file supplied")
> Wscript.Quit(1)
> End if
>
> GetPath = fstrInputFile & "," & fstrLogFilePath
>
> End Function
> ____________________________________________________________________
--
Michael Harris
Microsoft MVP Scripting
.
- Follow-Ups:
- Re: Really Baffled any clues as to what is going on?
- From: Frederick
- Re: Really Baffled any clues as to what is going on?
- References:
- Really Baffled any clues as to what is going on?
- From: Frederick
- Really Baffled any clues as to what is going on?
- Prev by Date: Re: Dumping changed data into a text file
- Next by Date: Re: Manipulating multiple Excel workbooks
- Previous by thread: RE: Really Baffled any clues as to what is going on?
- Next by thread: Re: Really Baffled any clues as to what is going on?
- Index(es):
Relevant Pages
|