Valhalla Legends Forums Archive | Visual Basic Programming | Get all of a File

AuthorMessageTime
ObsidianWolf
Im Looking to grab all the contents of a file without using a do loop

aka Im not trying to do this
[code]
Open myfile for input as #1
dim strTemp
Do while not eof(1)
Line Input #1, n$
StrTemp=StrTemp + N$
Loop
[/code]

I have something that works but It doesnt let me go over 64000 bytes returned.

[code]
Open App.Path & "\master.txt" For Binary As #1
Dim hellow As String * 64000
Get #1, , hellow
Close #1
[/code]

The Problem being that when I dim Hellow I can't Use more then 64k and it needs to be a constant. Is there anyway i can do LOF(1) and have that pertain to the Dim Hellow as String * lof(1) or something like that?
January 24, 2004, 1:19 AM
ObsidianWolf
Found one solution, It does use a loop though.
[code]
Function GetFileSize(strPath) as Long
Open strPath for random as #1
GetFileSize= LOF(1)
Close #1
End Function

Sub FileToVar(strPAth) as Variant
Dim n as Long
Dim strTemp as String * 64000
Open strPath for Binary as #1
Do while not Eof(1)
Get #1,, StrTemp
FileToVar = FileToVar & StrTemp
Loop
Close #1
n=GetFileSize(myfilepath)
FileToVar = Left(FileToVar,n) 'need to do this or else you end up with alot of null's in place where there as no information grabbed from file
End Sub

[/code]

If anyone else has something better then this Please let me know. Hope this helps anyone out there that has had similar problems.
January 24, 2004, 5:02 AM
Grok
[code]
Private Function FileToVar(strPath As String) As Variant
Dim fso As New Scripting.FileSystemObject
Dim TS As Scripting.TextStream
Set TS = fso.OpenTextFile(strPath)
FileToVar = TS.ReadAll
End Function
[/code]
January 24, 2004, 5:46 AM
Adron
[quote author=ObsidianWolf link=board=31;threadid=4865;start=0#msg40852 date=1074907149]
Im Looking to grab all the contents of a file without using a do loop

...

I have something that works but It doesnt let me go over 64000 bytes returned.

[/quote]

Maybe that's a sign that as you start working with bigger text files, you should consider alternatives to bringing the entire file into memory at once? Well, 64kb is pretty much ok these days, but some time you'll have to start dealing with the file a piece at a time.
January 24, 2004, 10:17 AM
ObsidianWolf
In most cases I try to conserve memory usage. But in this case memory usage is not a problem(literally I was told to not worry).

But thanks for Help Grok, Ill try that out.
January 25, 2004, 4:16 AM
SPY-3
[quote author=ObsidianWolf link=board=31;threadid=4865;start=0#msg40868 date=1074920546]
Found one solution, It does use a loop though.
[code]
Function GetFileSize(strPath) as Long
Open strPath for random as #1
GetFileSize= LOF(1)
Close #1
End Function

Sub FileToVar(strPAth) as Variant
Dim n as Long
Dim strTemp as String * 64000
Open strPath for Binary as #1
Do while not Eof(1)
Get #1,, StrTemp
FileToVar = FileToVar & StrTemp
Loop
Close #1
n=GetFileSize(myfilepath)
FileToVar = Left(FileToVar,n) 'need to do this or else you end up with alot of null's in place where there as no information grabbed from file
End Sub

[/code]

If anyone else has something better then this Please let me know. Hope this helps anyone out there that has had similar problems.
[/quote]
i belive you ment
[code]
Function GetFileSize(strPath) as Long
Open strPath for random as #1
GetFileSize= LOF(1)
Close #1
End Function

FUNCTION FileToVar(strPAth) as Variant
Dim n as Long
Dim strTemp as String * 64000
Open strPath for Binary as #1
Do while not Eof(1)
Get #1,, StrTemp
FileToVar = FileToVar & StrTemp
Loop
Close #1
n=GetFileSize(myfilepath)
FileToVar = Left(FileToVar,n) 'need to do this or else you end up with alot of null's in place where there as no information grabbed from file
End Sub

[/code]
sub would just make vb mad at you
August 27, 2004, 2:13 PM
Stealth
He meant it on January 23rd, 2004! That was months ago. Stop resurrecting these long-dead topics by slapping useless comments or links to PSCode onto them!
August 27, 2004, 6:26 PM

Search