Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file scripting object 4

Status
Not open for further replies.

Vec

IS-IT--Management
Jan 29, 2002
418
0
0
US
How do I add this reference or what ever it is. I am reading and it said "include the file scripting object" but it does not say how??

-------------------------------------------------------------------------
-------------------------------------------------------------------------

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Univer
 
Goto Project->references and add the Microsoft Scripting Runtime or Microsoft Script Control 1.0, one of the two, cant remember as I dont use it. FSO is slow, and its another dependency to your project.
 
Thanks LPlates, so if it is slow, what is the best way to acheive this:

File = App.Path & "\approved.txt"
Set textfile = fileobj.OpenTextFile(File)
i = 0
While Not textfile.AtEndOfStream
txt = textfile.ReadLine
Combo1.AddItem txt
Wend
textfile.Close

-------------------------------------------------------------------------
-------------------------------------------------------------------------

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Univer
 
Dim intFreeFile As Integer
Dim sText As String
Dim txt As String

On Error Resume Next
intFreeFile = FileFree
Open App.Path & "\approved.txt" For Binary Access Read As #intFreeFile
Do Until EOF(intFreeFile)
Line Input #intFreeFile, txt
Combo1.AddItem txt
txt = vbNullString
Loop
Close #intFreeFile
 
OOps,I just noticed .. intFreeFile = FileFree should read..

intFreeFile = FreeFile
 
>FSO is slow

On what do you base this assertion?
 
Testing various code including recursive directory scans using FSO, I found other ways to achieve the results faster
 
Hmm. I've never found FSO particularly slow. Indeed, for this particular requirement it is about 20 times faster than the 'classic' method
 
And here's a demonstration of that:
[tt]
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private fileobj As New FileSystemObject

Private Sub Command1_Click()
Dim starttime As Long
Dim maxloop As Long
Dim overhead As Long
Dim lp As Long

Dim lWithFSO As Long
Dim lWithoutFSO As Long

maxloop = 100 ' Repeat test maxloop times to get better results

SetupFile

starttime = GetTickCount
For lp = 0 To maxloop
Dummy
Next
overhead = GetTickCount - starttime

starttime = GetTickCount
For lp = 0 To maxloop
WithFSO
Next
lWithFSO = GetTickCount - starttime - overhead



starttime = GetTickCount
For lp = 0 To maxloop
NoFSO
Next
lWithoutFSO = GetTickCount - starttime - overhead

MsgBox "With FSO: " & CStr(lWithFSO) & vbCrLf & "Without FSO: " & CStr(lWithoutFSO)
End Sub

Public Sub Dummy()

End Sub

' For purposes of comparison have tried to make both the FSO and non-FSO solutions as similar as possible

Public Sub NoFSO()
Dim intFreeFile As Integer
Dim txt As String

On Error Resume Next

intFreeFile = FreeFile
Open "c:\test.txt" For Binary Access Read As #intFreeFile

Do Until EOF(intFreeFile)
Line Input #intFreeFile, txt
Loop
Close #intFreeFile

End Sub

Public Sub WithFSO()
Dim textfile As TextStream
Dim txt As String


Set fileobj = New FileSystemObject
Set textfile = fileobj.OpenTextFile("c:\test.txt")


Do Until textfile.AtEndOfStream
txt = textfile.ReadLine
Loop
Set textfile = Nothing

End Sub

' Set up a 4000 line file to give ourselves something substantial to work on
Public Sub SetupFile()

Dim txt As String
Dim lp As Long

With fileobj.OpenTextFile("c:\test.txt", ForWriting)


For lp = 1 To 4000
.WriteLine "Now is the winter of our discontent"
Next

End With

End Sub


 
Thanks for the excellent testing example strongm! In your setup code you need either:

With fileobj.OpenTextFile("c:\test.txt", ForWriting, True)
With mobjFSO.CreateTextFile("c:\test.txt")

I'm not trying to be picky but for the benefit of someone who may be new to the FileScriptingObject.

In your SetupFile procedure, you use .WriteLine to output your file which is a TextStream method, I think. How does that work or is a TextStream object implicity instantiated somehow by the prior code opening a text file for output?

Thanks again for taking the time to post an excellent example!




Have a great day!

j2consulting@yahoo.com
 
No.The only thin you need to do is to add a reference to the Microsoft Scripting Runtime (which I took as a given for anybody interested enough to folow the thread this far)

You don't need either of the additional lines that you suggest
 
When I tried to run it without the , True the code stopped because temp.txt did not exist. Not a problem. My question just above had to do with using .WriteLine which is a TextStream method. How can you use it without first instantiating the TextStream object, or am I missing something obvious here?

Have a great day!

j2consulting@yahoo.com
 
I had the same problem, I got an error file not found. Im quite amazed at the result in this test...hmmm.
 
Ah. "Tools/Options/General/Error Trapping" may be one issue here.

As for the TextStream query, you need to do a little research on the With keyword...

I'm happy to answer further questions on this if you like, but I think you'd be better off figuring the details out for yourself.
 
strongm
>Ah. "Tools/Options/General/Error Trapping" may be one issue here.
'Break on unhandled errors'

The line ...
[blue]
With fileobj.OpenTextFile("c:\test.txt", ForWriting)
[/blue]
Needs to have the create flag their...

[blue]
With fileobj.OpenTextFile("c:\test.txt", ForWriting, True)
[/blue]
SBendBuckeye is right!
 
I'm somewhat familiar with the With / End With pair, but I have almost always used it as a way to reduce excessive qualification and to tidy up my code. The examples in MSDN show the above as a way to group property assignments, etc.

Your example makes it appear that there is an implicit TextStream object created by the FileSystemObject.OpenTextFile method call. Is this accurate or anywhere close? I'm not afraid to dig into stuff if you would be kind enough to point me.

Also, I wasn't trying to nitpick your code but I remember as a raw rookie how frustrating it was to find some really cool piece of code and then have some little thing be wrong with it and not have a clue what to do.


Have a great day!

j2consulting@yahoo.com
 
Bah [tongue] ...yes, you're both right. But, frankly, that is not the issue here...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top