Jump to content

Searching for values?


8218
 Share

Recommended Posts

Hey.

Is it possible to use the AutoIT 3 to search either a .txt file or an .ini file after a given word, and then submit another word in the file, depending on the given word?

What I'm trying to do is a simple translater that when successfull locates the word from the "database" and sends anoter word (the translated word)

Any help would be apprichated :P

Edited by 8218
Link to comment
Share on other sites

Opt("MustDeclareVars",1)

Const $FILE = "file.txt"
Local $handle,$fcontents

$handle = FileOpen($FILE,0)       ; read
If $handle == -1 Then             ; error
    Exit
EndIf
$fcontents = FileRead($handle)    ; read the contents of the file
FileClose($handle)                ; close the file handle

; replace all occurences of searchstring with replacestring
$fcontents = StringReplace($fcontents,"searchstring","replacestring") 

$handle = FileOpen($FILE,2)       ; write
If $handle == -1 Then             ; error
    Exit
EndIf
FileWrite($handle,$fcontents)     ; read the contents of the file
FileClose($handle)                ; close the file handle

Link to comment
Share on other sites

Sorry, I think did a very bad formulation.

What I meant was like a GUIbox with an input box and a "Translate" button that when you click the button, checks the input and sees if it can find the match in the text file. For an example the text file could be

Word1 : Translated word1

Word2 : Translated word2

And then when you typed Word1 in the input box and clicked Translate, the Translated word2 would pop up in a msgbox.

Thanks on behalf :P

Link to comment
Share on other sites

Sorry, I think did a very bad formulation.

What I meant was like a GUIbox with an input box and a "Translate" button that when you click the button, checks the input and sees if it can find the match in the text file. For an example the text file could be

Word1 : Translated word1

Word2 : Translated word2

And then when you typed Word1 in the input box and clicked Translate, the Translated word2 would pop up in a msgbox.

Thanks on behalf :P

ok... i think i understood

Opt("MustDeclareVars",1)
Opt("GUIOnEventMode",1)

Const $GUI_EVENT_CLOSE = -3
Const $WIDTH = 250
Const $HEIGHT = 90
Const $TITLE = "Translator"

Const $TRANSINI = "translation.ini"
Const $SECTION  = "trans"

Enum $LANGA,$LANGB

Local $Hwnd,$In_Search

main()
Func main()
    $Hwnd = GUICreate($TITLE,$WIDTH,$HEIGHT)
    GUISetOnEvent(-3,"_EXIT")
    
    $In_Search = GUICtrlCreateInput("",20,20,200,20)
    GUICtrlSetOnEvent(GUICtrlCreateButton("Translate",20,50,200,30),"_TRANSLATE")
    
    GUISetState(@SW_SHOW)
    While 1
        Sleep(1000)
    WEnd
EndFunc

Func _TRANSLATE()
    Local $translations,$totranslate

    $translations = readFile()
    $totranslate = GUICtrlRead($In_Search)
    
    For $i = 1 To $translations[0][0]
        If $translations[$i][0] == $totranslate Then
            MsgBox(0,$TITLE,$translations[$i][1],0,$Hwnd)
            Return
        EndIf
    Next 
    
    MsgBox(48,$TITLE,"Didn´t found a damn thing!",0,$HWnd)
    
EndFunc

Func readFile()
    Local $fcontents,$handle
    
    $fcontents = IniReadSection($TRANSINI,$SECTION)
    If @error Then 
        die("Couldn´t load ""translation.ini""")
    EndIf
    Return $fcontents
EndFunc

Func die($mesg)
    MsgBox(48,$TITLE,$mesg,0,$Hwnd)
    Exit
EndFunc

Func _EXIT()
    Exit
EndFunc

The ini looks like this

CODE
[trans]

Hallo=hello

Tschüss=bye

Wort=word

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...