Jump to content

Help with ReadLine


Ace08
 Share

Recommended Posts

uhm guys can someone help.... i've been unable to figure this one out

the script would pick up a line in a file that starts with :59: and :50F:.

if the file starts with :50F: then it will read the next 3 lines below it that should start with 1/, 2/ and sometimes 3/ so far i was able to read until 3/ but if the line will only be at 2/ the script still gets a value for 3/ sorry if i wasn't able to explain more bu will give an example.... here is the script

#include <Date.au3>
#include <Array.au3>
$var = FileOpenDialog ("Choose The File to be Converted.", "C:\Autoit\Script", "Files(*.txt)" , 2)
If @error Then
Else
    global $strcountUMD, $TotAmtUMD, $strcountPNP, $TotAmtPNP,$sRmadd,$z
    $file = FileOpen($var, 0)
    ;Check if file opened for reading OK
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf
    While 1
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop
        $Tag3 = StringInStr($line ,":50F:")
        If $Tag3 > 0 then
            $RemName = FileReadLine($file)
            $RemName = StringReplace($RemName, "1/", "")
            $RemAdd = FileReadLine($file)
            $RemAdd = StringReplace($RemAdd, "2/", "")
            $sRmadd = FileReadLine($file)
            $sIn = StringInStr($sRmadd , ":")
            if $sIn > 0 then
                $RRAdd = $RemAdd
                
            else
                $sRmadd = StringReplace($sRmadd, "3/", "")
                $RRAdd = $RemAdd & " " & $sRmadd
            endif
            if StringLen($RRAdd) >= 76 then
                $NewRRAdd = StringLeft($RRAdd, 75)
                $RRAdd2 = StringMid($RRAdd,76,stringlen($RRAdd))
            else
                $NewRRAdd = $RRAdd
                $RRAdd2 = " "
            endif 
        endif
        
        $Tag4 = StringInStr($line, ":59:")
        if $Tag4 > 0 then
        msgbox(0,"",$line)
            $Temp1 = $line
            $Temp1 = stringReplace($Temp1, ":59:", "")
            $Temp1 = stringReplace($Temp1, "/", "")
            $Temp1 = stringReplace($Temp1, " ", "")
            $Temp1 = stringReplace($Temp1, "-", "")
            $_Temp1 = Number($Temp1)
            if $_Temp1 > 0 then
                $Trn = "02"
                $Pcode = "CBBM"
                $Bcode = "DGO"
                $Bname = "DGO"
                $Acctr = $Temp1
                $BeneName = FileReadLine($file)
                $BeneAdd = FileReadLine($file)
                $BeneAdd2 = FileReadLine($file)
                if stringLeft($BeneAdd2, 1) = ":" then
                    $BAdd = $BeneAdd
                else
                    $BAdd = $BeneAdd & " " & $BeneAdd2
                endif
                
            elseif $_Temp1 = 0 then
                
                $Trn = "01"
                $Pcode = "BPMM"
                $Bcode = "DGO"
                $Bname = "DGO"
                $Acctr = " "
                $NameBene = $line
                $NameBene = stringReplace($NameBene, ":59:", "")
                $BeneName = $NameBene
                $BeneAdd = FileReadLine($file)
                $BeneAdd2 = FileReadLine($file)
                if stringLeft($BeneAdd2, 1) = ":" then
                    $BAdd = $BeneAdd
                else
                    $BAdd1 = $BeneAdd & " " & $BeneAdd2
                    $BeneAdd3 = FileReadLine($file)
                    if stringLeft($BeneAdd3, 1) = ":" then
                        $BAdd = $BAdd1
                    else
                        $BAdd = $BAdd1 & " " & $BeneAdd3
                    endif
                endif
            endif
            if stringlen($BAdd) >= 76 then
                $realAdd1 = stringleft($BAdd, 75)
                $realAdd2 = stringmid($BAdd, 76, stringLen($BAdd))
            else
                $realAdd1 = $BAdd
                $realAdd2 = " "
            endif
        endif
    Wend
Endif

here is a sample on what the file would look like

:50F:CCPT/MY/A373221

1/Ali Carter

2/MY/Addr 1

:57A:BNORPHMMXXX

:59:/1200059040

Al Paso 1

Phi 1

-}

:50F:CCPT/MY/A373221

1/Ali Carter

2/MY/Addr 1

:57A:BNORPHMMXXX

:59:/0250404508

Al Paso 2

Phi 2

-}

:50F:CCPT/MY/MJ123

1/Michael Jackson

2/MY/Addr Kuala Lumpur

:57A:BNORPHMMXXX

:59:/1230013944

Al Paso 3

Phil 3

-}

:50F:CCPT/MY/MJ123

1/Michael Jackson

2/MY/Addr Kuala Lumpur

:57A:BNORPHMMXXX

:59:/948654

Al Paso 4

Phil 4

-}

:50F:CCPT/MY/Cat123

1/Catherine Ng

2/MY/Address 4

:59:Al Paso 5

Phil 5

-}

:50F:CCPT/ID/JC123

1/Jeff Chee

2/MY/Address 10

:57A:BNORPHMMXXX

:59:/994411

Al Paso 6

Phil 6

-}

:50F:CCPT/MY/Cat123

1/Catherine Ng

2/MY/Address 4

:59:Al 5

Phil 5

-}

:50F:CCPT/MY/A373221

1/Ali Cater

2/MY/Addr 1

:57A:BNORPHMMXXX

:59:/0250404508

Al Paso 2

Phil 2

-}

i've put a msgbox so i would know what the ouput would be, it sould give an output of :

:59:/1200059040

:59:/0250404508

:59:/1230013944

:59:/948654

:59:Al Paso 5

:59:/994411

:59:Al Paso 5

:59:/0250404508

but its only showing

:59:/1200059040

:59:/0250404508

:59:/1230013944

:59:/948654

:59:/994411

:59:/0250404508

Work smarter not harder.My First Posted Script: DataBase

Link to comment
Share on other sites

I wonder if the error is arising here?

$RemName = FileReadLine($file)
            $RemName = StringReplace($RemName, "1/", "")
            $RemAdd = FileReadLine($file)
            $RemAdd = StringReplace($RemAdd, "2/", "")
            $sRmadd = FileReadLine($file)

Every time you use FileReadLine it reads the next line, so what look like your intended sequential operations on the same line [eg for $RemName and $RemAdd] will be applied to different lines. This would have the effect of skipping some lines.

It might be better to use _FileReadToArray and then you have a static target to aim at. You could work line by line through the array, making the changes to a second array. then when all done _FileWriteFromArray the new array back to the file.

William

Edited by saywell
Link to comment
Share on other sites

uhm guys can someone help.... i've been unable to figure this one out

the script would pick up a line in a file that starts with :59: and :50F:.

if the file starts with :50F: then it will read the next 3 lines below it that should start with 1/, 2/ and sometimes 3/ so far i was able to read until 3/ but if the line will only be at 2/ the script still gets a value for 3/ sorry if i wasn't able to explain more bu will give an example.... here is the script

I cannot follow you what you have written above with the output below how it should be

here is a sample on what the file would look like

i've put a msgbox so i would know what the ouput would be, it sould give an output of :

:59:/1200059040

:59:/0250404508

:59:/1230013944

:59:/948654

:59:Al Paso 5

:59:/994411

:59:Al Paso 5

:59:/0250404508

Where are the lines 1/, 2/?

Can you describe it again please?

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

@saywell

Thanks for the info will try this one and i think this was the part that i am having errors(or skiping line)

if $sIn > 0 then
                $RRAdd = $RemAdd
                
            else
                $sRmadd = StringReplace($sRmadd, "3/", "")
                $RRAdd = $RemAdd & " " & $sRmadd
            endif

when i removed this the script is working but does not read 3/ ;)

you have sample of those?

@UEZ

sorry UEZ if it isn't clear to be honest i dont know how to expalin it fully but the idea is it would output all the lines that has :59: in the begining, 1/, 2/ and 3/ will be used in another function, the problem is the script stores 3 lines below :50F: there is a posibility that the 3rd line could start at :59: if this is stored in variable3 since i am using a readline the result would be that the script wont see the line starting with :59: like on the previous sample i did'nt output the two lines :59:Al Paso 5.

Work smarter not harder.My First Posted Script: DataBase

Link to comment
Share on other sites

Can you explain how the output should be for following lines?

:50F:CCPT/MY/MJ123
1/Michael Jackson
2/MY/Addr Kuala Lumpur
:57A:BNORPHMMXXX
:59:/1230013944
Al Paso 3
Phil 3
-}
:50F:CCPT/MY/MJ123
1/Spongebob
2/MY/Addr Hollywood
3/MY/AMER
:57A:BNORPHMMXXX
:59:/948654
Al Paso 4
Phil 4
-}

1st output:

/1230013944

/948654

2nd output:

1/Michael Jackson

2/MY/Addr Kuala Lumpur

1/Spongebob

2/MY/Addr Hollywood

3/MY/AMER

Is that correct?

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Can you explain how the output should be for following lines?

:50F:CCPT/MY/MJ123
1/Michael Jackson
2/MY/Addr Kuala Lumpur
:57A:BNORPHMMXXX
:59:/1230013944
Al Paso 3
Phil 3
-}
:50F:CCPT/MY/MJ123
1/Spongebob
2/MY/Addr Hollywood
3/MY/AMER
:57A:BNORPHMMXXX
:59:/948654
Al Paso 4
Phil 4
-}

1st output:

/1230013944

/948654

2nd output:

1/Michael Jackson

2/MY/Addr Kuala Lumpur

1/Spongebob

2/MY/Addr Hollywood

3/MY/AMER

Is that correct?

Br,

UEZ

@UEZ

Not realy you see the scriptshould do is this, read a line if the line starts with :50F: then read another line store it in a variable $RemName(This line always starts with 1/)and replace 1/ with "" after read another line and store it to $RemAdd(This line always start with 2/) and replace 2/ with "" then read another line store it at $sRmadd(Now this line can be 3/ but not always) then read another line until it finds the line that starts with :59: and do whats on tag4

$Tag4 = StringInStr($line, ":59:")
        if $Tag4 > 0 then
        msgbox(0,"",$line)
            $Temp1 = $line
            $Temp1 = stringReplace($Temp1, ":59:", "")
            $Temp1 = stringReplace($Temp1, "/", "")
            $Temp1 = stringReplace($Temp1, " ", "")
            $Temp1 = stringReplace($Temp1, "-", "")
            $_Temp1 = Number($Temp1)
            if $_Temp1 > 0 then
                $Trn = "02"
                $Pcode = "CBBM"
                $Bcode = "DGO"
                $Bname = "DGO"
                $Acctr = $Temp1
                $BeneName = FileReadLine($file)
                $BeneAdd = FileReadLine($file)
                $BeneAdd2 = FileReadLine($file)
                if stringLeft($BeneAdd2, 1) = ":" then
                    $BAdd = $BeneAdd
                else
                    $BAdd = $BeneAdd & " " & $BeneAdd2
                endif
                
            elseif $_Temp1 = 0 then
                
                $Trn = "01"
                $Pcode = "BPMM"
                $Bcode = "DGO"
                $Bname = "DGO"
                $Acctr = " "
                $NameBene = $line
                $NameBene = stringReplace($NameBene, ":59:", "")
                $BeneName = $NameBene
                $BeneAdd = FileReadLine($file)
                $BeneAdd2 = FileReadLine($file)
                if stringLeft($BeneAdd2, 1) = ":" then
                    $BAdd = $BeneAdd
                else
                    $BAdd1 = $BeneAdd & " " & $BeneAdd2
                    $BeneAdd3 = FileReadLine($file)
                    if stringLeft($BeneAdd3, 1) = ":" then
                        $BAdd = $BAdd1
                    else
                        $BAdd = $BAdd1 & " " & $BeneAdd3
                    endif
                endif
            endif
            if stringlen($BAdd) >= 76 then
                $realAdd1 = stringleft($BAdd, 75)
                $realAdd2 = stringmid($BAdd, 76, stringLen($BAdd))
            else
                $realAdd1 = $BAdd
                $realAdd2 = " "
            endif
        endif

i have added a msgbox(0,"",$line) to see if the script realy is reading all the lines starting with :59: when it is doing $Tag4, but like what happened in my example :59:Al Paso 5 was not included in the output.

Oh and thanks UEZ for your time and help i appreciate it ;)

Edited by Ace08

Work smarter not harder.My First Posted Script: DataBase

Link to comment
Share on other sites

Try changing this section and see if it fits what you're looking for.

(see commented lines below)

$sRmadd = FileReadLine($file)
If StringInStr($sRmadd, ":59:") = 0 Then ;~ <<<<<<< Add this line
    $sIn = StringInStr($sRmadd, ":")
        If $sIn > 0 Then
            $RRAdd = $RemAdd
        Else
            $sRmadd = StringReplace($sRmadd, "3/", "")
            $RRAdd = $RemAdd & " " & $sRmadd
        EndIf
            If StringLen($RRAdd) >= 76 Then
            $NewRRAdd = StringLeft($RRAdd, 75)
            $RRAdd2 = StringMid($RRAdd, 76, StringLen($RRAdd))
        Else
        $NewRRAdd = $RRAdd
        $RRAdd2 = " "
    EndIf
Else                            ;~ <<<<<<<<<<<<< Add this line
    $line = $sRmadd         ;~ <<<<<<<<<<<<< Add this line
EndIf                           ;~ <<<<<<<<<<<<< Add this line

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Glad I could help.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...