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

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

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

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

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

#10 ·  Posted

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

    • olmanRvr
      write return value of exe call to text file
      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()  
    • GeorgeB
      How to restrict inputboxes to specific characters only?
      By GeorgeB
      I'm having trouble in making an input box where I can limit the characters that can be inputted.  Anybody have an idea how this can be done in Auto IT?
      Specifically I would like to create an input field where the user would enter a MAC address.  What I would like to do is to limit the characters that can be inputted to only HEX values (A-F and 0-9 as well as the - character).  This is so that the user cannot input invalid characters. The program is going to be used by very non-technical people, so the idea is to remove as many chances for errors that we can think of.  I would also prefer to limit the field to only 17 characters, so that there is only enough space to enter the MAC address with the - character as the separator.
      Thank you all in advance for any suggestions!
    • DavidFigueroa
      Automatically adding leading zero's to inputbox
      By DavidFigueroa
      I'm trying to create an inputbox that automatically formats numbers with leading zeroes when the updown control is used.  I found an example from Melba23 (see below) that shows a step incremented inputbox, and based on that, I tried to create one that would work, but it does not.
      I am creating separate inputs for hours and minutes, and I want to make sure the inputbox is always 2 digits. And right up front, I don't pretend to really understand the DLLStruct* calls.. 
      Melba23's sample:
      #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <UpDownConstants.au3> $hGUI = GUICreate("Test", 500, 500) $hInput = GUICtrlCreateInput("5000", 10, 10, 100, 20) $hUpDown = GUICtrlCreateUpdown($hInput, BitOR($UDS_WRAP, $UDS_NOTHOUSANDS)) GUICtrlSetLimit($hUpDown, 6000, 5000) GUISetState() GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY") While 1     Switch GUIGetMsg()         Case $GUI_EVENT_CLOSE             Exit     EndSwitch WEnd Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)     ; Is it from the UpDown?     If BitAND($wParam, 0xFFFF) = $hUpDown Then         ; Create NMUPDOWN structure         Local $tStruct = DllStructCreate("hwnd;long;int;long;long", $lParam)         ; Is it a change message?         If DllStructGetData($tStruct, 3) = 0xFFFFFD2E Then ; $UDN_DELTAPOS             ; Alter the change value             DllStructSetData($tStruct, 5, 100 * DllStructGetData($tStruct, 5))         EndIf     EndIf EndFunc ;~ The NMUPDOWN structure holds: ;~ 1 - Handle of UpDown ;~ 2 - ControlID of UpDown ;~ 3 - Message type sent by UpDown ;~ 4 - Current value of UpDown ;~ 5 - Change to apply to input (+/-1) And here is my non-working sample (it runs, and the initial value shows correctly, but when you hit the updown, you lose the leading zeroes.
      #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> $hGUI = GUICreate("Window", 300, 100, -1, -1) $Count = 1 Global $hEdit = GUICtrlCreateInput($Count, 15, 15, 100, 30) GUICtrlSetFont(-1, 12, 400, "", "Tahoma") GUICtrlSetData(-1, StringFormat("%03u",$Count)) Global $hUpDown = GUICtrlCreateUpdown(-1) GUICtrlSetLimit(-1, 100, 0) GUISetState() While 1     $hMsg = GUIGetMsg()     Switch $hMsg         Case $GUI_EVENT_CLOSE             Exit     EndSwitch WEnd Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)     ; Is it from the UpDown?     If BitAND($wParam, 0xFFFF) = $hUpDown Then         ; Create NMUPDOWN structure         Local $tStruct = DllStructCreate("hwnd;long;int;long;long", $lParam)         ; Is it a change message?         If DllStructGetData($tStruct, 3) = 0xFFFFFD2E Then ; $UDN_DELTAPOS             ; Alter the change value             $ValueToSet = DllStructGetData($tStruct, 4)             $ValueToSet = StringFormat("%03u", $ValueToSet)             GUICtrlSetData($hEdit, $ValueToSet)         EndIf     EndIf EndFunc  
    • OMGWTFLOLBBQ
      Text to Array
      By OMGWTFLOLBBQ
      Hello!
       I've been going through all of the help files and examples, but I feel lost. I can't seem to find what I need. Essentially, what I'm doing is simple(in my head).
      Create a file based on the information needed, select a line in that file, then put that line back in DOS. My code is below, I know it's messy, but I'll take the heat if you can help.
       
      ;BitlockerUpdate ;2016.05.24 HotKeySet("{ESC}", "Terminate") Run("C:\Windows\System32\CMD.exe") ;open command prompt Sleep(500) ;sleep half second if ProcessExists("cmd.exe") Then winactivate("Administrator: C:\Windows\System32\CMD.exe") ;refocus command prompt Send("C:") Send("{ENTER}") send("CD{SPACE}%windir%\sysnative\") send("{ENTER}") EndIf ;create file with Bitlocker Protectors Send("manage-bde.exe{SPACE}-protectors{SPACE}-get{space}c:{space}>{Space}%userprofile%\Desktop\protectors.txt") ;send code "manage-bde -protectors -get c: > %userprofile%\Desktop\protectors.txt" Send("{ENTER}") ;send enter key to create text file on desktop with protectors Sleep(1000) ;sleep one second(1s) ;edit txt file/create array, pull line and paste into new file, copy text then paste into cmd window below ;update data if ProcessExists("cmd.exe") Then winactivate("Administrator: C:\Windows\System32\CMD.exe") ;refocus command prompt Send("manage-bde.exe{SPACE}-protectors{SPACE}-adbackup{SPACE}c:{SPACE}-id{SPACE}{COPIEDINFORMATIONGOESHERE}") Send("{ENTER}") ;don't forget to delete temp file if you create one created. EndIf ;remove files if ProcessExists("cmd.exe") Then winactivate("Administrator: C:\Windows\System32\CMD.exe") ;refocus command prompt Send("DEL{SPACE}%userprofile%\Desktop\protectors.txt") ;send code to delete protectors.txt Send("{ENTER}") ;don't forget to delete temp file if you create one created. EndIf MsgBox(0,"Done","Copy and upload are done. Please check Computer in AD.") Func Terminate() Exit 0 EndFunc  
      The protectors.txt file looks like this:
      BitLocker Drive Encryption: Configuration Tool version 6.1.7601 Copyright (C) Microsoft Corporation. All rights reserved. Volume C: [OSDisk] All Key Protectors TPM: ID: {0000000-0000-0000-0000-0000000} Numerical Password: ID: {1000000-1000-1000-1000-1000000} Password: 000000-000000-000000-000000-000000-000000-000000 Data Recovery Agent (Certificate Based): ID: {2000000-2000-2000-2000-2000000} Certificate Thumbprint: 1111111a1aa1a1aa1a1a1a1aaaaaa1aaa1 I would specifically need the line after Numerical Password. 
      Original line:      ID: {1000000-1000-1000-1000-1000000}
      Needed information: -ID: {1000000-1000-1000-1000-1000000}
       
      I apologize in advance for inconveniencing anyone with my silly issues. Hopefully this can stand as a great learning experience for both me and anyone else looking.
       
    • farhadrahman2
      InputBox
      By farhadrahman2
      hey guys, i have a problem .
      I want to search a file in a particular folder and after inserting the name  in the input box i want to make a drop down menu of all the matches and then select it and open it.
      I need the full code .
      so please anyone help me on this one.