Sign in to follow this  
Followers 0
kneze

search value in text file

10 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

Your 3rd code gives a result if you enter "23"  :)

Share this post


Link to post
Share on other sites

mikell,

Thanks, corrected code shamelessly plagiarizing your SRE...


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

Share this post


Link to post
Share on other sites

Hi all

it works, Thanks for your solution.

if i use multiple values separatet with  ; in same line. how can i get value3 to inputbox3, value 5 to inputbox5

 

list.txt

Share this post


Link to post
Share on other sites
8 hours ago, kylomas said:

mikell,

Thanks, corrected code shamelessly plagiarizing your SRE...

Totally allowed of course, please take it easy :)
 

kneze,
Totally different question. This deserves a new thread - and BTW you could first try something by yourself
Hint : StringSplit

1 person likes this

Share this post


Link to post
Share on other sites

Hi Mikell

thanks i try to use stringSplit.

thanks all again.

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Similar Content

    • StudioMaker
      By StudioMaker
      Hey anyone knows how i can have a inputbox that is connected to an HotKeySet? Like if u put in like "a" in the inputbox it sets the hotkey to "a"?
       
    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
       
      i have a simple question, i have a text file with empty rows, i want to remove/delete them. Is there any function which might help me?
       
      Thanks in advance.
       
      Regards
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good morning community!
      I am working on a script which read from a text file ( .txt ) and should import all the content in a SQLite3 DB, in order to execute some queries that should be difficult to execute on a text file.
      So, I was looking for something very very fast, because the file could be very large ( I don't know exaclty how much can became big, but I know a lot of rows, it's a log file ... )
      I found the "Import method", but I don't know If I can implement it in a query ( @jchd, it's your turn!  )
      Do you know some methods that I can implement in my script to have a very very fast import of thousands and thousands rows in a SQLite3 DB?
      Thanks a lot
      Francesco
    • akira2891
      By akira2891
      Hi,
      I was wondering is this all inputs can be done in 1 box with 3 fields and not to use another gui.
      And how can i validate data for example if i don't enter username he prompt me a message box all time until field is empty.
      I try like this but it give me a msgbox and continue on another field.
      $username = InputBox("Add new user", "Username", "", "", 200, 130, Default, Default, 0) if $username <> " " then msgbox(0, "", "Please enter username") endif $email = InputBox("Add new user", "Email", "", "", 200, 130, Default, Default, 0) $password = InputBox("Add new user", "Password", "", "*", 200, 130, Default, Default, 0)  
    • olmanRvr
      By olmanRvr
      I have a compiled script :-timeStampSecs.exe which returns timestamp in seconds. I call it in FileWrite to write the time stamp in a text file.But it writes some other values instead of the timestamp. Please help.
      thanks
      olmar
      The script is below:-
      $kNum=run("timeStampSecs.exe","",1); also tried with flags 2,4,8,10 $hFile=FileOpen("timeStamp.txt",2) FileWrite($hFile,"the key is: "&$kNum) FileClose("$hFile") ;Code of thetimeStampSecs.exe:- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #include <Date.au3> Func secFromEpoch() Local $ts=_DateDiff( "s","1970/01/01 00:00:00",_NowCalc()) $ts=String($ts) ConsoleWrite("timeStamp is: "&$ts&@CRLF) Return $ts EndFunc secFromEpoch()