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?
 
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
 
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.