Solved

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

Posted on 2006-07-11
13
1,056 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 92

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 92

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
 
LVL 4

Accepted Solution

by:
jomacinc earned 500 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now