Reading/Changing a text file

bigmc6000

New Member
Joined
Aug 17, 2005
Messages
47
This is really a macro-guru question. I have a text file that is read by another app. In it's text file it reads:

0 1 0 5 1 5 1 etc...

I would like to replace that with:


0 0 1 5 1 5 1
0 1 0 1 2 4
0 3 4 5 1 1
etc...

Is there a cleaver way to read in the file, search for the first section of text and replace it with the second section of text and do that for the whole file?

Thanks in advance!
 

Some videos you may like

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,047
Office Version
  1. 365
Platform
  1. Windows
Could you give a clearer example?

How do you get that result from just this?

0 1 0 5 1 5 1 etc...
 

bigmc6000

New Member
Joined
Aug 17, 2005
Messages
47
You don't actually get the result persay. If it see's that it knows to Paste a set that I give it. I.E. the application doesn't assign values to the lines in it's initial run - it just does teh first line. But if you replace that first line with the second group it defines 3 more functions in the app...
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,047
Office Version
  1. 365
Platform
  1. Windows
I'm sorry, perhaps it's just me, but you've totally lost me now.:)
 

bigmc6000

New Member
Joined
Aug 17, 2005
Messages
47

ADVERTISEMENT

Ok - hmm. Let's say a text file has a bunch of data in it for, say, 5 different cases; Case 1, Case 2... For each Case the file contains 10 lines of info. What I would like to do is find that 10th line (which I know what it says) and replace it with something that is 3 lines long.

That make any sense?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,047
Office Version
  1. 365
Platform
  1. Windows
Becoming a bit clearer.

What you would need to do is read the text file in line by line, compare the line to whatever (I'm still a bit unclear what that that is).

Based on the comparison you would write the line to another file and if needed also write the new data.

You could then delete the original file and rename the new one.

All this is possible, in fact not too difficult, buit more information is needed.

I'll try and mock something up in code and post back.
 

bigmc6000

New Member
Joined
Aug 17, 2005
Messages
47

ADVERTISEMENT

I just want to look to see (essentially) if a string of text is equal to another string of text that I define. If that is true then write a third string into the new file. Getting any better?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,047
Office Version
  1. 365
Platform
  1. Windows
Here's a little something I mocked up.

It looks through a text file, writing to a new text file and if it finds 5 it also writes 3 new pieces of data.
Code:
Sub test()
Dim FF1, FF2
Dim strLine As String
Dim I As Long
    FF1 = FreeFile()
    FF2 = FreeFile(1)
    
    Open "C:\Papers\TextTest.txt" For Input As FF1
    Open "C:\Papers\NewTextTest.txt" For Output As FF2
        While Not EOF(FF1)
            Line Input #FF1, strLine
            Print #FF2, strLine
            If Val(strLine) = 5 Then
                For I = 1 To 3
                    Print #FF2, "New Data"
                Next I
            End If
        Wend
    Close #FF2
    Close #FF1
End Sub
 

bigmc6000

New Member
Joined
Aug 17, 2005
Messages
47
Is there a way to do the pop up window deal - that lets you navigate and select the file you want to read in?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,047
Office Version
  1. 365
Platform
  1. Windows
Code:
Sub test()
Dim FF1, FF2
Dim strLine As String
Dim strFileName
Dim I As Long

    FF1 = FreeFile()
    FF2 = FreeFile(1)
    
    strFileName = Application.GetOpenFilename("Text Files (*.txt), *.txt")
    
    If strFileName = False Then Exit Sub
    
    Open strFileName For Input As FF1
    Open "C:\Papers\NewTextTest.txt" For Output As FF2
        While Not EOF(FF1)
            Line Input #FF1, strLine
            Print #FF2, strLine
            If Val(strLine) = 5 Then
                For I = 1 To 3
                    Print #FF2, "New Data"
                Next I
            End If
        Wend
    Close #FF2
    Close #FF1
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,118,030
Messages
5,569,757
Members
412,289
Latest member
Kingchaos64
Top