Jump to content

search value in text file


kneze
 Share

Recommended Posts

Hi all,

i would like to enter a vlaue to Inputbox 1 an press button1. Script should search value in a text file and put value after "; " to Inputbox 2.

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.2
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <file.au3>

$Form1 = GUICreate("Form1", 257, 119, 192, 124)
$Input1 = GUICtrlCreateInput("Input1", 15, 35, 121, 21)
$Input2 = GUICtrlCreateInput("Input2", 15, 60, 121, 21)
$Button1 = GUICtrlCreateButton("Button1", 145, 35, 75, 25)
GUISetState(@SW_SHOW)


$iLines = _FileCountLines(@ScriptDir & "\data.txt")
;MsgBox(0, "Char read:", $ilines)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

 Case $Button1



    EndSwitch
WEnd

can anyone help?

data.txt

Link to comment
Share on other sites

kneze,

This should get you started...

Not using regexp

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.2
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <file.au3>

$Form1 = GUICreate("My Search Form", 257, 119, 192, 124)
$Input1 = GUICtrlCreateInput("", 15, 35, 121, 21)
$Input2 = GUICtrlCreateInput("", 15, 60, 121, 21)
$Button1 = GUICtrlCreateButton("Search", 145, 35, 75, 25)
GUISetState(@SW_SHOW)

local $aFile = stringsplit(fileread(@scriptdir & '/data.txt'),@CRLF,3)  ; <--- read the file to an array splitting on end of line chars (CRLF)
local $srcharg

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $srcharg = guictrlread($input1)
            ; iterate through the array (same as reading the file line by line) looking for $input1
            for $i = 0 to ubound($aFile) - 1
                ; if $input1 is found then split that entry and use the last element for $input2
                if stringinstr($aFile[$i],$srcharg) <> 0 then GUICtrlSetData($input2,stringsplit($aFile[$i],';',3)[1])
            next
    EndSwitch
WEnd

Using regexp

<code removed...see example below>

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Goofing around with your gui a bit...added 'Not Found' code...

#cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.14.2
    Author:         myName

    Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <file.au3>
#include <array.au3>
#include <StaticConstants.au3>

$Form1 =    GUICreate("My Search Form", 300, 100, 192, 124)
            GUICtrlCreateLabel('Input', 15, 20, 40, 17)
$Input1 =   GUICtrlCreateInput("", 15, 35, 121, 21)
            GUICtrlCreateLabel('Result', 160, 20, 40, 17)
            GUICtrlCreateLabel(' = ', 137, 37, 15, 17)
            GUICtrlSetFont(-1, 12)
$Result =   GUICtrlCreateLabel("", 160, 35, 121, 21, bitor($ss_sunken, $ss_center, $ss_centerimage))
$Button1 =  GUICtrlCreateButton("Search", 15, 65, 75, 25, $BS_DEFPUSHBUTTON) ; <--- push button with enter key as well as mouse

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $in = guictrlread($input1)
            $out = stringregexpReplace(fileread(@scriptdir & '\data.txt'), '(?s).*(?<=^|\v)' & $in & ';(\N+).*', "$1")
            guictrlsetdata($Result, @extended ? $out : 'Not Found')
    EndSwitch
WEnd

kylomas

edit:code correct per mikell

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

kylomas,
the 2nd code crashes with error if the entry in input1 is not available in data.txt
May I suggest this

Case $Button1
  $in = guictrlread($input1)
  $out = stringregexpReplace(fileread(@scriptdir & '\data.txt'), '(?s).*(?<=^|\v)' & $in & ';(\N+).*', "$1")
  If @extended Then guictrlsetdata($input2, $out)

Edit
Using  stringregexp(.... ,...., 3)[0]  is extremely dangerous  :)

 

Edited by mikell
Link to comment
Share on other sites

Did something similar...look up...:)

Thanks for the example...spent about 1/2 hour trying to get the regex replace thing to work...finally gave up...

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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

×
×
  • Create New...