ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Read text file? (https://www.excelbanter.com/excel-programming/420975-read-text-file.html)

Charlotte E.[_2_]

Read text file?
 
If I use this line, to read a text file...

TFC = Input$(LOF(1), 1)

....it seems as if it only reads the first 65 536 chars?!?

But, I'm dealing with files often twice as long, sometimes up to 3 times!

How can I force the entire contents of the text file into the variable
'TFC'?


TIA.

CE



Dave Peterson

Read text file?
 
Here's an example that reads a file and adds carriage returns to every line
feed.

Using File System Object and .readall are the things you need.

Option Explicit
Sub UpDateTxtFile()

Dim FSO As Object
Dim RegEx As Object

Dim myFile As Object
Dim myContents As String
Dim myInFileName As String
Dim myOutFileName As String

myInFileName = "C:\my documents\excel\test.txt"
myOutFileName = Environ("temp") & "\testout.txt"

Set FSO = CreateObject("Scripting.FileSystemObject")

Set myFile = FSO.OpenTextFile(myInFileName, 1, False)
myContents = myFile.ReadAll
myFile.Close

Set RegEx = CreateObject("VBScript.RegExp")
With RegEx
.Global = True
.IgnoreCase = False
.Pattern = vbLf
myContents = .Replace(myContents, vbCrLf)
End With

Set myFile = FSO.CreateTextFile(myOutFileName)
myFile.Write myContents
myFile.Close

End Sub

"Charlotte E." wrote:

If I use this line, to read a text file...

TFC = Input$(LOF(1), 1)

...it seems as if it only reads the first 65 536 chars?!?

But, I'm dealing with files often twice as long, sometimes up to 3 times!

How can I force the entire contents of the text file into the variable
'TFC'?

TIA.

CE


--

Dave Peterson

Charlotte E.[_2_]

Read text file?
 
Hi Dave,

Sorry for a late reply - a little busy today :-)

Got it working, but one thing puzzles me:

myFile.Close

This file apparently doesn't closes the file completely?!?

I had to use:

Reset

To be able to use the file, including deleting it, from other
applications???


Anyway, it works - thanks :-)


CE





Dave Peterson wrote:
Here's an example that reads a file and adds carriage returns to
every line feed.

Using File System Object and .readall are the things you need.

Option Explicit
Sub UpDateTxtFile()

Dim FSO As Object
Dim RegEx As Object

Dim myFile As Object
Dim myContents As String
Dim myInFileName As String
Dim myOutFileName As String

myInFileName = "C:\my documents\excel\test.txt"
myOutFileName = Environ("temp") & "\testout.txt"

Set FSO = CreateObject("Scripting.FileSystemObject")

Set myFile = FSO.OpenTextFile(myInFileName, 1, False)
myContents = myFile.ReadAll
myFile.Close

Set RegEx = CreateObject("VBScript.RegExp")
With RegEx
.Global = True
.IgnoreCase = False
.Pattern = vbLf
myContents = .Replace(myContents, vbCrLf)
End With

Set myFile = FSO.CreateTextFile(myOutFileName)
myFile.Write myContents
myFile.Close

End Sub

"Charlotte E." wrote:

If I use this line, to read a text file...

TFC = Input$(LOF(1), 1)

...it seems as if it only reads the first 65 536 chars?!?

But, I'm dealing with files often twice as long, sometimes up to 3
times!

How can I force the entire contents of the text file into the
variable 'TFC'?

TIA.

CE




Dave Peterson

Read text file?
 
I've never seen that occur.

But glad you got it working.

"Charlotte E." wrote:

Hi Dave,

Sorry for a late reply - a little busy today :-)

Got it working, but one thing puzzles me:

myFile.Close

This file apparently doesn't closes the file completely?!?

I had to use:

Reset

To be able to use the file, including deleting it, from other
applications???

Anyway, it works - thanks :-)

CE

Dave Peterson wrote:
Here's an example that reads a file and adds carriage returns to
every line feed.

Using File System Object and .readall are the things you need.

Option Explicit
Sub UpDateTxtFile()

Dim FSO As Object
Dim RegEx As Object

Dim myFile As Object
Dim myContents As String
Dim myInFileName As String
Dim myOutFileName As String

myInFileName = "C:\my documents\excel\test.txt"
myOutFileName = Environ("temp") & "\testout.txt"

Set FSO = CreateObject("Scripting.FileSystemObject")

Set myFile = FSO.OpenTextFile(myInFileName, 1, False)
myContents = myFile.ReadAll
myFile.Close

Set RegEx = CreateObject("VBScript.RegExp")
With RegEx
.Global = True
.IgnoreCase = False
.Pattern = vbLf
myContents = .Replace(myContents, vbCrLf)
End With

Set myFile = FSO.CreateTextFile(myOutFileName)
myFile.Write myContents
myFile.Close

End Sub

"Charlotte E." wrote:

If I use this line, to read a text file...

TFC = Input$(LOF(1), 1)

...it seems as if it only reads the first 65 536 chars?!?

But, I'm dealing with files often twice as long, sometimes up to 3
times!

How can I force the entire contents of the text file into the
variable 'TFC'?

TIA.

CE


--

Dave Peterson


All times are GMT +1. The time now is 02:45 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com