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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading writing .txt files 1

Status
Not open for further replies.

MrMoocow

Programmer
May 19, 2001
380
US
Hi,
Would any one be so kind as to point me in the right direction as to the creation and reading of txt files.

Thanks,
Brad
 
Brad --

The code below will read a text file line by line. The textual content of each line is placed in lsText.

For further information check out the VB documentation for the following commands: Open, Close, Input, Print, and Write.

WB

--------------------------------------------------
Dim llFile As Long
Dim lsText As String

Open "c:\AppFolder\Params.txt" For Input As llFile
While Not EOF(llFile)
Input #llFile, lsText
'
' ....do whatever....
'
Wend
Close llFile

 
I forgot one line -- it should be inserted just before the Open statement:

llFile = FreeFile()

WB
 
Go to and look at "Top Searches". Select "FileSysrtemObject". "Open", "Line Input", etc have been maintained only for compatibility for old programs. It is assumed that by now they should have been upgraded or abandoned so VB6 is the last version to support this compatibility. Start now to use objects for file processing.
 
John --

You are correct, and Brad and I would both do well to follow your advice.

My response was based on an old, tried-and-true method for handling text files. It definitely works, but as you pointed out, it will eventually become obsolete.

Thanks -- WB
 
Wait hold on I'm sorry but if I shouldn't use WildBillH 's then what? ((MS was of little help and I don't have the MSDN library installed).

Thanks for continued support,
Brad
 
go to Libraries/ MSDN / Visual Studio 6.0 Doc / VB Doc / Using VB / Programmer's Guide / Part 2 / Processing Drives .....
Code:
Sub Read_Files()
    Dim fso As New FileSystemObject, txtfile, _
      fil1 As File, ts As TextStream
    fso.CreateTextFile "c:\testfile.txt", True
    MsgBox "Writing file"
    ' Write a line.
    Set fil1 = fso.GetFile("c:\testfile.txt")
    Set ts = fil1.OpenAsTextStream(ForWriting)
    ts.Write "Hello World"
    ts.Close
    ' Read the contents of the file.
    Set ts = fil1.OpenAsTextStream(ForReading)
    s = ts.ReadLine
    MsgBox s
    ts.Close
End Sub
Code:
Dim objFs as scripting.FileSystemObject
Dim objFolder as scripting.Folder
Dim objFiles as scripting.Files
Set objfs = CreateObject("Scripting.FileSystemObject")
Set objFolder = objfs.GetFolder(strFoldername)
Set objFiles = objFolder.Files
For each objFile in objFiles
    ... = objfile.DateCreated
    ... = objFile.DateModified
    ... = objFile.DateLastAccessed
Next
Code:
Dim objFS
Set objFs = CreateObject("Scripting.FileSystemObject")
objFS.MoveFolder strSource,strdestination
Code:
Private Function GetFileIntoString(ByVal strFileName As String) As String
    '*****
    '* Read a file of input into a string.
    '*****
    Dim objTS         As TextStream, _
        objFS           As FileSystemObject
    Dim strResponse     As String
    
    Set objFS = New FileSystemObject
    On Error Resume Next
        ' Open file for Reading, Create=NO, ASCII
        Set objTS = objFS.OpenTextFile(strFileName, ForReading, False, False)
        strResponse = Err.Description
        If Len(strResponse) = 0 Then
            GetFileIntoString = objTS.ReadAll
        End If
        objTS.Close
    On Error GoTo 0
    
    Set objTS = Nothing
    Set objFS = Nothing
    If Len(strResponse) > 0 Then
        Err.Raise vbObjectError + 513, "GetFileIntoString", _
                strFileName & vbCrlf & strResponse
    End If
    
End Function
Code:
Dim fs as Scripting.FileSystemObject
Dim a as Scripting.TextStream
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\testfile.txt", True)
a.WriteLine("This is a test.")
a.Close
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top