Link to home
Start Free TrialLog in
Avatar of FatalErr
FatalErr

asked on

status test in vbscript for a TextStream (TextStream = fso.OpenTextFile) before using TextStream.Close

set fso = CreateObject("Scripting.FileSystemObject")
set TextStream = fso.OpenTextFile("c:\test.txt")

is there a way to check the status of TextStream before using TextStream.Close or some other trick short of "on error resume next"?
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Hi FatalErr,

If fso.FileExists("c:\test.txt") Then
    set TextStream = fso.OpenTextFile("c:\test.txt")
Else
    set TextStream = fso.CreateTextFile("c:\test.txt")
End If

Regards,

Patrick
Avatar of FatalErr
FatalErr

ASKER

I'm looking for some way to determine that TextStream is open.  Something like

if TextStream.Status = open Then
  TextStream.Close
end if

Thanks,
Ah, I see.  In that case, I think the old trick of using On Error Resume Next, and then checking for
Err <> 0 is the way to go.

Patrick
ASKER CERTIFIED SOLUTION
Avatar of jomacinc
jomacinc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Before you set your TextStream to nothing,  you need to close it.   Is there a property that you can look at to see if it's open?  Thanks,
My code is not setting it to nothing, it is checking if it already is nothing. Generally a closed text stream is Nothing. So if it is not, the I would presume it is open.
Thanks for accepting the answer, but was a grade of "C" really warrented??

Please See:
https://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/help.jsp#hi55
I appreciate this forum and do not take the accepting of answers nor the grading lightly.  My question was not answered and it did not appear that you knew more on the subject based on your last reply.  I find that it just frustrates people to keep quizzing them.  So, I let things be for a while.   If I am mistaken, I am open to you showing me that you have the solution.  Or if I find that your presumtion of the text stream being "open"  is correct in some research, I'll freely admit it.  In that case, I would have no problem with upping your grade if a moderator can provide a way to do so.  

According to the documentation you point to, a C is the appropriate grade if there is no solution. There is no GPA like concept here, so getting a C looks to me like a good way to reward effort.  I think it's better than asking for a question to be closed.

I asked:
is there a way to check the status of TextStream before using TextStream.Close or some other trick short of "on error resume next"?

I gave a solution example:
if TextStream.Status = open Then
  TextStream.Close
end if

You proposed:
If Not TextStream Is Nothing Then
  TextStream.Close
end if

I replied:
Before you set your TextStream to nothing,  you need to close it.   Is there a property that you can look at to see if it's open?

You replied:
My code is not setting it to nothing, it is checking if it already is nothing. Generally a closed text stream is Nothing. So if it is not, the I would presume it is open.

It is my experience that if you close a RecordSet or a Connection that is open OR you set an open one to Nothing without closing it first, you get an error.   The command TextStream.Close exists, so it can exist in either state.  My experience with programming has led me not to presume that the existance of the TextStream implies it being open.

Thanks,
Stacey
You are right in that I presumed that a test for 'nothing' would work, this presumption was based on the fact that a closed textstream returns an 'object not set' error if you try and reclose it and this was the one rare time that I was not in a position the confirm it.

As for a 'C' being an appropriate grade for an unanswered question, I must disagree - the documentation does not say that - in fact it gives no example of an appropriate time for such a grade.

I do appriciate that you wanted to award something, but an unanswered question is just that, and the guidlines for closing such a question can be found here:
https://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/help.jsp#hi71

I think this question was answered, but it was by matthewspatrick at the start of this thread, although the error check method is not ideal, it appears to be the only way to check if the textstream is closed.
IMO He should be awarded the points with a grade of 'A' or 'B'

Regards, Jomac.
https://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/help.jsp#hi73 gives the following guidelines for the grade C:

C: Because Experts' reliability are often judged by their grading records, many Experts would like the opportunity to clarify if you have questions about their solutions. If you have given the Expert(s) ample time to respond to your clarification posts and you have responded to each of their posts providing requested information; or if the answers, after clarification, lack finality or do not completely address the issue presented, then a "C" grade is an option. You also have the option here of just asking Community Support to delete the question.

I don't like to close a question that people have contributed to.  Then it looks like you took someones answer and didn't give them credit.  Maybe they shouldn't have the grade C at all.  Maybe it should just be Excellent and Good.  I don't know  :  )
I tend to agree, the grading system is a bit ambiguous... either way, there was no intention for this to come of as a complaint. After more indepth research it would apper the only way to check the status is to capture the error (this is not a method I like, but it is the only one any of us can find). I really do think the points belong to matthewspatrick.

Anyway - all the best and if you do find another way, post it back so others can draw on the effort.

Jomac :-)
I'm surprised that they didn't give a way to check but I don't find it inconceivable.  Thanks for the follow-up on this being the best solution.  I should have given it to matthewspatrick with an A.  Had I know it was the only way, I would have.  I will post if I find something else.

Thanks again!  And sorry for the "C" in this case.
For the sake of finality, here is a little function that will check if the textstream is closed.

Private Function TextStreamClosed(oTS As Scripting.TextStream) As Boolean
    On Local Error GoTo TextStreamClosedError
    If oTS.AtEndOfStream Then DoEvents

    Exit Function
TextStreamClosedError:
    If Err.Number = 91 Then TextStreamClosed = True
End Function