Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-07-11
13
Medium Priority
?
1,215 Views
Last Modified: 2008-02-01
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"?
0
Comment
Question by:FatalErr
  • 6
  • 5
  • 2
13 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 17086011
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
0
 
LVL 1

Author Comment

by:FatalErr
ID: 17086094
I'm looking for some way to determine that TextStream is open.  Something like

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

Thanks,
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 17086623
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Accepted Solution

by:
jomacinc earned 1000 total points
ID: 17087562
Can you use:

If Not TextStream Is Nothing Then
  TextStream.Close
end if

Regards, Jomac
0
 
LVL 1

Author Comment

by:FatalErr
ID: 17104280
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,
0
 
LVL 4

Expert Comment

by:jomacinc
ID: 17104532
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.
0
 
LVL 4

Expert Comment

by:jomacinc
ID: 17238745
Thanks for accepting the answer, but was a grade of "C" really warrented??

Please See:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/help.jsp#hi55
0
 
LVL 1

Author Comment

by:FatalErr
ID: 17245105
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
0
 
LVL 4

Expert Comment

by:jomacinc
ID: 17246222
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:
http://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.
0
 
LVL 1

Author Comment

by:FatalErr
ID: 17246428
http://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  :  )
0
 
LVL 4

Expert Comment

by:jomacinc
ID: 17246471
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 :-)
0
 
LVL 1

Author Comment

by:FatalErr
ID: 17246507
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.
0
 
LVL 4

Expert Comment

by:jomacinc
ID: 17246515
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
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question