INSERT IN POSITION 133 "|" on each line...

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

INSERT IN POSITION 133 "|" on each line...

Post by sal21 »

This code format in 132 character all line in txt file.

Now i need to insert in position 133 of each line processed the digit "|"... when the code wirte into new txt file
i have maked this code, but...????!!!!

Code: Select all

Sub MAIN()

    On Error GoTo ExitHandler

    Dim intUnitIn As Integer
    Dim intUnitOut As Integer
    Dim strBuf As String, Y As Long
    Dim strFilename As String
    Dim strNewFilename As String

    Close intUnitIn
    Close intUnitOut

    strFilename = "C:\TEMP\TABULATI.TXT"
    strNewFilename = "C:\TEMP\TABULATI_132.TXT"

    intUnitIn = FreeFile
    Open strFilename For Input As #intUnitIn
    intUnitOut = FreeFile
    Open strNewFilename For Output As #intUnitOut
    Do While Not EOF(intUnitIn)
        Line Input #intUnitIn, strBuf
        If Not strBuf = "" And Trim$(strBuf) <> "" Then
        Do While Len(strBuf) > 132
            Print #intUnitOut, Left(strBuf, 132) & "|"
            strBuf = Mid(strBuf, 133)
        Loop
        Debug.Print Len(strBuf)
        Y = 132 - Len(strBuf)
        'Debug.Print "DIFF " & Y
        Print #intUnitOut, strBuf
        'Debug.Print strBuf
        End If
    Loop

    Close intUnitIn
    Close intUnitOut

    Exit Sub

ExitHandler:

    Close intUnitIn
    Close intUnitOut

    MsgBox Err.Description, vbExclamation

End Sub



result:
sdfsdsdsdsdf |<position 133
jhgh |<position 133

i think not is for me :groan: :hairout: :sad:

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: INSERT IN POSITION 133 "|" on each line...

Post by HansV »

What exactly is the problem?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: INSERT IN POSITION 133 "|" on each line...

Post by sal21 »

HansV wrote:What exactly is the problem?
the len of line just is 132 , no action add only in postion 133 the character "|" (without quotes) :grin:

The last character of line is in postion 45 calculate the diff 133-45 insert "|" in postion 88

in thsi case i have all lines fixed 133 of lenght with the end "|"

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: INSERT IN POSITION 133 "|" on each line...

Post by HansV »

The code that you posted is intended to split very long lines into chunks of length 132, with a "| at the end. There will not be a "|" after the last chunk.
That is not what you want?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: INSERT IN POSITION 133 "|" on each line...

Post by sal21 »

HansV wrote:The code that you posted is intended to split very long lines into chunks of length 132, with a "| at the end. There will not be a "|" after the last chunk.
That is not what you want?
yes! .... but for this time the code in in experiment and i dont know wath is the real result :scratch: :scratch: :scratch: :scratch:

but, I hope you understand my prob? :grin:

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: INSERT IN POSITION 133 "|" on each line...

Post by HansV »

No, I don't. Sorry.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: INSERT IN POSITION 133 "|" on each line...

Post by sal21 »

HansV wrote:No, I don't. Sorry.

oK...

Tis piece of code chek the len of string?
....
Y = 132 - Len(strBuf)
....

now admit strBuf have a len 45 calculate the difference to 133 in this case is 88. (in this case is the value of Y)
add to Print #intUnitOut, strBuf numbers of Y spaces & "|"....

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: INSERT IN POSITION 133 "|" on each line...

Post by HansV »

You now have

Code: Select all

        Y = 132 - Len(strBuf)
        'Debug.Print "DIFF " & Y
        Print #intUnitOut, strBuf
Change it to

Code: Select all

        Y = 132 - Len(strBuf)
        'Debug.Print "DIFF " & Y
        Print #intUnitOut, strBuf & Space(Y) & "|"
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: INSERT IN POSITION 133 "|" on each line...

Post by sal21 »

HansV wrote:You now have

Code: Select all

        Y = 132 - Len(strBuf)
        'Debug.Print "DIFF " & Y
        Print #intUnitOut, strBuf
Change it to

Code: Select all

        Y = 132 - Len(strBuf)
        'Debug.Print "DIFF " & Y
        Print #intUnitOut, strBuf & Space(Y) & "|"
SO SIMPLE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :thankyou: :thankyou: :thankyou: :thankyou: :thankyou: :thankyou: :thankyou:

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: INSERT IN POSITION 133 "|" on each line...

Post by sal21 »

HansV wrote:You now have

Code: Select all

        Y = 132 - Len(strBuf)
        'Debug.Print "DIFF " & Y
        Print #intUnitOut, strBuf
Change it to

Code: Select all

        Y = 132 - Len(strBuf)
        'Debug.Print "DIFF " & Y
        Print #intUnitOut, strBuf & Space(Y) & "|"

this is out on this question... but realted to the len of string.

I have maked this piece of code to find string in a txt file with approx 2.300.000 lines.
Now about variuos experiment this is the fasted way, for me :grin:

But have a prob!

When the code intercept the string is possible to looping the subseguent lines and stop when the string STO00001 in found in mid$ position 4,8

????


Code: Select all


Option Explicit
Type udtTRec
    'eventuale   'DummyStringIniziale as string * ?
    DatoMid36 As String * 36   ' dovrebbe essere mid(1,36) della riga corrente
    DummyStringFInale As String * 99  ' è la parte restante della riga
    'N.B. devi contare anche il crlf finale di ogni riga
End Type
Sub MAIN()

    Dim udtRec As udtTRec
    Dim infI As Integer
    Dim lNumRec As Long
    Dim lSize As Long
    Dim J As Long
    Dim sngStartTime As Single
    Dim sngTotalTime As Single

    sngStartTime = Timer

    lSize = Len(udtRec)
    infI = FreeFile

    Open "C:\REPORT_L0928\test.TXT" For Random As #infI Len = lSize
    lNumRec = LOF(infI) \ lSize

    For J = 1 To lNumRec
        Get #infI, , udtRec
        If udtRec.DatoMid36 = "string lenght 36 bit" Then

            'TEST TIMER
            sngTotalTime = Timer - sngStartTime
            MsgBox "Time taken:  " & Round(sngTotalTime, 2) & " seconds"
            'TEST TIMER

        End If
    Next J

End Sub

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: INSERT IN POSITION 133 "|" on each line...

Post by HansV »

You could use

If Mid(udtRec.DatoMid36, 4, 8) = "STO00001" Then Exit For
Best wishes,
Hans