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

Reading and manipulating a TXT file line by line

Status
Not open for further replies.

dashen

Programmer
Jul 14, 2005
233
US
I have a text file that need to be manipulated a certain way

00 See dog.
00 See dog run.
00 Run dog run.
01 (1) It was a stormy night,
01 in Boston, on the crowded streets
01 of Main Street.
02 (2) That was a declarative statement.
03 (3) Who says that
03 I can't have a little
03 fun


I need to read the TXT file line by line and output a new file or manipulate the old file with.
00 See dog. See dog run. Run dog run.
01 (1) It was a stormy night, in Boston, on the crowded streets of Main Street.
02 (2) That was a declarative statement.
03 (3) Who says that I can't have a little fun


Can someone help me with this? I haven't done this in a while. I basically need to data mine some old business documents to figure out how to pull conditions for a project. Basically move everthing to electronic format.

I know this is probably a simple thing, but I haven't had to do this in a while. Thanks for any help you can give.
 
do a search here, and in the msdn help files, using the keywords:

scripting OpenTextFile
 
cool, thanks.

I know I Can Open TextFile For Input As #1
and then Line Input #1

but the files are quite large and I don't want to overload some variable.

Is there some way to incorporate the Line read with a read for the 1st 2 characters and then somehow just manipulate the directly in the TXT file itself.

So for example. Reading 01 and then just deleting all CrLf and "01" until it reaches "02", in that way putting the "01" on a single line?
 
Code:
 Dim fso, strIniPath, txtFile1, strLine
 Dim newStrLine
 Dim tempTestChar
 
 Set fso = CreateObject("Scripting.FileSystemObject")
    strIniPath = "F:\Info Services\JIMS2\Historical Order analysis\EMELIEN.D120898.OUT5"
    If fso.fileexists(strIniPath) Then
        Set txtFile1 = fso.OpenTextFile(strIniPath, 1, False)
        
        tempTestChar = "00"
        Do While Not txtFile1.AtEndOfStream
            strLine = txtFile1.readline
            If (Left(strLine, 2) = tempTestChar) Then
                newStrLine = newStrLine & Mid(strLine, 2)
            Else
                '*****************************************
                'Output newStrLine to txt before resetting
                '*****************************************
                newStrLine = strLine
                tempTestChar = Left(strLine, 2)
            End If
        Loop
        txtFile1.Close
    End If

How would I output to a new TXT file from what I calculated? Thanks.
 
Also with the above code, the text fields get too large eventually and newStrLine throws an Error.

Is there a way to Write to a TXT file and just Writeline on completion of that one line, so I don't have to use a variant to hold the data?
 
Something along these lines should work

Code:
Private Sub Command1_Click()
' Declare variables
Dim fso As New FileSystemObject
Dim dict As New Dictionary
Dim InStream As TextStream
Dim OutStream As TextStream
Dim InputData As String

' Open files for processing
Set InStream = fso.OpenTextFile("C:\New Text Document.txt", ForReading)
Set OutStream = fso.OpenTextFile("C:\New Text Document2.txt", ForWriting, True)

' Get first record for matching purposes
InputData = InStream.ReadLine
dict.Add Left$(InputData, 2), Left$(InputData, 2)
OutStream.Write InputData

' Loop through the rest of the file
Do
    InputData = InStream.ReadLine

    ' Check to see if we are to add to the current
    ' record or start a new one
    If dict.Exists(Left$(InputData, 2)) = False Then
        OutStream.WriteLine
        dict.Add Left$(InputData, 2), Left$(InputData, 2)
        OutStream.Write InputData
    Else
        OutStream.Write Mid$(InputData, 4)
    End If
Loop Until InStream.AtEndOfStream

' Writes out last CRLF and closes and destroys
' All objects from memory
OutStream.WriteLine
InStream.Close
OutStream.Close
Set dict = Nothing
Set InStream = Nothing
Set OutStream = Nothing
Set fso = Nothing

' Prompts user of completion
MsgBox "Done!", vbInformation
End Sub

Swi
 
See faq222-6008 for reasons not to use the new keyword on the same line as the dim statement.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top