Jump to content

read csv file then match and get value


Recommended Posts

hi,

thanks for auto IT scripts, its really powerful, i hope someone in this forum can help me out, here is the deal:

i have a csv file with data separed with coma ;. its a 94ko file with 2974 lines.

i need to open, read inside this csv, match a value (set as variable) $A then if its matched (dont know how long it will take to read all data inside) get the line when $A is found and get all the line data. ex:

found:

$A; x; xx; xxx; xxxx

i would like to grab all the data separed with comas and pu them in variables.

i dont have so much dev experience but i suppose i need "string" and IF ...THEN.

thanks for help

Link to comment
Share on other sites

try this- it will take a tab separated CSV and load up an array with it's contents, then display on the screen. You should be able to do something with the array afterwards. Not perfect code by a long way, but should help.

___________________

#region --- ScriptWriter generated code Start ---

Opt("WinWaitDelay",100)

Opt("WinTitleMatchMode",4)

Opt("WinDetectHiddenText",1)

Opt("MouseCoordMode",0)

$h_MapFile = FileOpen("test_invoice_products2.txt", 0)

$i_Counter = 0

Dim $filefields[6]

$filefields[0] = 'code0'

$filefields[1] = 'code1'

$filefields[2] = 'code2'

$filefields[3] = 'code3'

$filefields[4] = 'code4'

$filefields[5] = 'code5'

While 1

$i_Counter = $i_Counter + 1

$scanned_line = FileReadLine($h_MapFile, $i_Counter) ; read in whole line

If @error Then

MsgBox(0, "Error", "Finished")

ExitLoop

EndIf

$separate_words = StringSplit($scanned_line, @TAB)

$s_MsgBox = ''

For $i = 1 to 6

$s_MsgBox = $s_MsgBox & $filefields[$i-1] & ': ' & $separate_words[$i] & @LF

Next

MsgBox(0, 'Map #' & $i_Counter, $s_MsgBox)

WEnd

FileClose($h_MapFile)

Link to comment
Share on other sites

try this- it will take a tab separated CSV and load up an array with it's contents, then display on the screen. You should be able to do something with the array afterwards.  Not perfect code by a long way, but should help.

___________________

#region --- ScriptWriter generated code Start ---

Opt("WinWaitDelay",100)

Opt("WinTitleMatchMode",4)

Opt("WinDetectHiddenText",1)

Opt("MouseCoordMode",0)

$h_MapFile = FileOpen("test_invoice_products2.txt", 0)

$i_Counter = 0

Dim $filefields[6]

$filefields[0] = 'code0'

$filefields[1] = 'code1'

$filefields[2] = 'code2'

$filefields[3] = 'code3'

$filefields[4] = 'code4'

$filefields[5] = 'code5'

While 1

$i_Counter = $i_Counter + 1

$scanned_line = FileReadLine($h_MapFile, $i_Counter)  ; read in whole line

If @error Then

  MsgBox(0, "Error", "Finished") 

  ExitLoop

EndIf

$separate_words = StringSplit($scanned_line, @TAB)

$s_MsgBox = ''

For $i = 1 to 6

  $s_MsgBox = $s_MsgBox & $filefields[$i-1] & ': ' & $separate_words[$i] & @LF

Next

MsgBox(0, 'Map #' & $i_Counter, $s_MsgBox)

WEnd

FileClose($h_MapFile)

<{POST_SNAPBACK}>

just want to say that i think it's cool that your first post is to try to help someone. that being said, there is an easier way...

$input = FileOpen("Source.csv",0)
$output = FileOpen("results.csv",2)
$FINDME = InputBox("What","What value to look for?")
while 1
    $line = FileReadLine($input)
    if @error = -1 then ExitLoop
    if StringInStr($line,$FINDME) Then
        FileWriteLine($output,$line)
        MsgBox(0,"Found",$FINDME & " found:" & @crlf & $line & @CRLF & "result has been added to results.csv")
    EndIf
WEnd
Link to comment
Share on other sites

thanks very much for the help! it works great!

i just modified the script to match my needs, if it can help someone else:

$input = FileOpen("Source.csv",0)

;$output = FileOpen("results.csv",2) ; suppress because FileWriteline generated and error, dont understand why, i set the output as constant

$FINDME = InputBox("What","What value to look for?")

while 1

$line = FileReadLine($input)

if @error = -1 then ExitLoop

if StringInStr($line,$FINDME) Then

FileWriteLine("c:\temp\results.csv",$line)

MsgBox(64,"Found",$FINDME & " found:" & @crlf & $line & @CRLF & "result has been added to results.csv")

EndIf

WEnd

now, next step is to read output csv : A;B;C;D and put them in var:

A=$A

etc...

thanks again!

Link to comment
Share on other sites

thanks very much for the help! it works great!

i just modified the script to match my needs, if it can help someone else:

$input = FileOpen("Source.csv",0)

;$output = FileOpen("results.csv",2) ; suppress because FileWriteline generated and error, dont understand why, i set the output as constant

$FINDME = InputBox("What","What value to look for?")

while 1

    $line = FileReadLine($input)

    if @error = -1 then ExitLoop

    if StringInStr($line,$FINDME) Then

        FileWriteLine("c:\temp\results.csv",$line)

        MsgBox(64,"Found",$FINDME & " found:" & @crlf & $line & @CRLF & "result has been added to results.csv")

    EndIf

WEnd

now, next step is to read output csv : A;B;C;D and put them in var:

A=$A

etc...

thanks again!

<{POST_SNAPBACK}>

glad i could help
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...