Replace text in a text data file

How can i replace a value in a text  data file with vb6 code.
Exable
If the column 132 is 0 value replaced to 5.
Thanks
adgram1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GeoffHarperCommented:
Try the below:


Dim fn1 As Integer, fn2 As Integer, buf As String

fn1 = FreeFile
Open "C:\test1.txt" For Input As #fn1
fn2 = FreeFile
Open "C:\test2.txt" For Output As #fn2
Do While Not EOF(fn1)
    Input #fn1, buf
    If Len(buf) >= 132 Then
        If Mid$(buf, 132, 1) = "0" Then Mid$(buf, 132, 1) = "5"
    End If
    Print #fn2, buf
Loop
Close #fn2
Close #fn1

Open in new window

0
adgram1Author Commented:
Sory but dont works, it destroys the lines.May be the problem are the commas in the values. Here is a line :
5011007003005       JAMESON 070                                                                                         23,76      23         23      
0
GeoffHarperCommented:
Maybe you mean a logical column instead of a fixed column?  I assumed it was a flat-file with fixed column positions for the data.

Is it a comma-delimited, or tab-delimited file?  What kind of data file is it?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

adgram1Author Commented:
Its a tab-delimited file with the following structure
field              start     width
Bar Code        1           20
descr             21         100
price              121        11
vat                132         8
dep               140         8

thanks for reply
0
GeoffHarperCommented:
Try this:


Dim fn1 As Integer, fn2 As Integer
Dim buf As String, cols As Variant, i As Integer

fn1 = FreeFile
Open "C:\test1.txt" For Input As #fn1
fn2 = FreeFile
Open "C:\test2.txt" For Output As #fn2
Do While Not EOF(fn1)
    Input #fn1, buf
    cols = Split(buf, vbTab)
    If Trim$(cols(3)) = "0" Then cols(3) = "5"
    buf = ""
    For i = 0 To 4
        If Len(buf) > 0 Then buf = buf & vbTab
        buf = buf & cols(i)
    Next
    Print #fn2, buf
Loop
Close #fn2
Close #fn1

Open in new window

0
adgram1Author Commented:
subscript out of range
If Trim$(cols(3)) = "0" Then cols(3) = "5"

I attached the file

thanks for reply export.txt
0
GeoffHarperCommented:
It actually is not a tab-delimited file. I also didn't realize there was 8-bit data within until I got your file.

Try this:


Dim fn1 As Integer, fn2 As Integer, buf As String
Dim pos As Long, lastpos As Long

fn1 = FreeFile
Open "C:\test1.txt" For Binary Access Read As #fn1
buf = Space$(LOF(fn1))
Get #fn1, , buf
Close #fn1

If Right$(buf, 2) <> vbCrLf Then buf = buf & vbCrLf

lastpos = 1
pos = InStr(buf, vbCrLf)
Do While pos > 0
    If Trim$(Mid$(buf, (lastpos + 131), 8)) = "0" Then Mid$(buf, (lastpos + 131), 1) = "5"
    pos = pos + 2
    lastpos = pos
    pos = InStr(pos, buf, vbCrLf)
Loop
    
fn2 = FreeFile
Open "C:\test2.txt" For Binary Access Write As #fn2
Put #fn2, , buf
Close #fn2

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adgram1Author Commented:
sorry but
bad file mode
Get #fn1, , buf


thanks for reply
0
GeoffHarperCommented:
Your input file is exactly like the file you sent me?  And it's not opened by something else?  Where does the file come from (i.e. what generated it: Windows app, Unix app, etc.)?
0
adgram1Author Commented:
Yes its the same and it generated from the following code



db.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & Text1.Text & ";"
  adoPrimaryRS.Open "SELECT Base_Apothiki.[Bar Code], Base_Apothiki.Perigrafi, Format(([Lianiki_TimiEuro1]),'0.00') AS lt1  , Table_Klasis_FPA.Pososto , Table_Klasis_FPA.Pososto FROM Base_Apothiki INNER JOIN Table_Klasis_FPA ON Base_Apothiki.Klasi_FPA = Table_Klasis_FPA.KlasisFPAID WHERE (((Base_Apothiki.[Bar Code])>''));", db, adOpenStatic, adLockOptimistic
 ''adoPrimaryRS.Open "SELECT Base_Apothiki.[Bar Code], Base_Apothiki.Perigrafi, Base_Apothiki.Lianiki_TimiEuro1 (Lianiki_TimiEuro1 AS decimal)  , Table_Klasis_FPA.Pososto , Table_Klasis_FPA.Pososto FROM Base_Apothiki INNER JOIN Table_Klasis_FPA ON Base_Apothiki.Klasi_FPA = Table_Klasis_FPA.KlasisFPAID WHERE (((Base_Apothiki.[Bar Code])>''));", db, adOpenStatic, adLockOptimistic


Set DataGrid1.DataSource = adoPrimaryRS
    Screen.MousePointer = vbHourglass
    
    Dim lLen  As Long
    Dim lStart As Long
    
    Open App.Path & "\" & Text4.Text For Output As #1
        With adoPrimaryRS
            .MoveFirst
            Do While Not .EOF
                strLine = ""
                
              
                
               For i = 0 To .Fields.Count - 1
    If .Fields(i).Name = "Bar Code" Then
        lLen = 20
    ElseIf .Fields(i).Name = "Perigrafi" Then
        lLen = 100
     ''ElseIf .Fields(I).Name = "Lianiki_TimiEuro1" Then
     ElseIf .Fields(i).Name = "lt1" Then
        lLen = 11
        ElseIf .Fields(i).Name = "Pososto" Then
        lLen = 8
       
     
    End If
    If Option1.Value = True Then
    strLine = strLine & "" & PadRight(.Fields(i).Value, lLen)
    Else
    strLine = strLine & "" & PadLeft(.Fields(i).Value, lLen)
    End If
    
 
Next i
                
                Print #1, strLine
                .MoveNext
            Loop
            .MoveFirst
        End With
    Close #1
    
    Screen.MousePointer = vbDefault

Open in new window

0
GeoffHarperCommented:
The same code I sent you (last) works on that file on my system with no errors, and the resulting file is exactly what I would expect.  Something's different between your system and mine, but I don't know what it it is.
0
adgram1Author Commented:
I restart the pc and everything is ok now.
Special thanks GeoffHarper
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.