• Welcome to Valhalla Legends Archive.
 

Get all of a File

Started by ObsidianWolf, January 23, 2004, 07:19 PM

Previous topic - Next topic

ObsidianWolf

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

aka Im not trying to do this

Open myfile for input as #1
dim strTemp
Do while not eof(1)
  Line Input #1, n$
  StrTemp=StrTemp + N$
Loop


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


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


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?

ObsidianWolf

Found one solution, It does use a loop though.

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



If anyone else has something better then this Please let me know.  Hope this helps anyone out there that has had similar problems.

Grok


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

Adron

Quote from: ObsidianWolf on January 23, 2004, 07:19 PM
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.


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.

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.

SPY-3

Quote from: ObsidianWolf on January 23, 2004, 11:02 PM
Found one solution, It does use a loop though.

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



If anyone else has something better then this Please let me know.  Hope this helps anyone out there that has had similar problems.
i belive you ment

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


sub would just make vb mad at you

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!
- Stealth
Author of StealthBot