Sign in to follow this  
Followers 0
w00dr0w

FileOpen & FileReadLine question

7 posts in this topic

#1 ·  Posted (edited)

I've set up a GUI that takes a user specified text file and enters it into another program. The problem I'm having is that each line in the text file needs to have certain characters skipped.

My code so far, and an example of the text I need entered.

#include <GUIConstants.au3>
#Region ### START Koda GUI section
$Form1_1 = GUICreate("JD's importer", 258, 160, 193, 115)
$Label1 = GUICtrlCreateLabel("Select the pointfile for import", 8, 8, 200, 17)
$Input1 = GUICtrlCreateInput("", 8, 24, 200, 21)
$SelectPath_Btn = GUICtrlCreateButton("...", 220, 22, 30, 25, 0)
$Label2 = GUICtrlCreateLabel("Select the Phun_draw .exe (Phun_Draw_Object_2008_03_16.exe)", 8, 50, 200, 34)
$Input2 = GUICtrlCreateInput("", 8, 80, 200, 21)
$SelectPath2_Btn = GUICtrlCreateButton("...", 220, 80, 30, 25, 0)
$OK_Btn = GUICtrlCreateButton("OK", 70, 120, 30, 25, 0)
$Cancel_Btn = GUICtrlCreateButton("Cancel", 138, 120, 50, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $Cancel_Btn
            Exit
        Case $SelectPath_Btn
            $Path1 = FileOpenDialog("Select pointfile", @MyDocumentsDir, "point file (*.txt)")
            If $Path1 = '' Or @error = 1 Then ContinueLoop
            GUICtrlSetData($Input1, $Path1)
            $Point_file = FileOpen($Path1, 0)
            $Point1 = FileReadLine($Point_file, 1)
        Case $SelectPath2_Btn
            $Path2 = FileOpenDialog("Select Program File", @ProgramFilesDir, "(*.exe)", 1)
            If $path2 = '' Or @error = 1 Then ContinueLoop
            GUICtrlSetData($Input2, $Path2)
        Case $OK_Btn
            Run("notepad.exe")
            WinWaitActive("Untitled - Notepad")
            Send($Point1)

    EndSwitch
WEnd

And an example of the text

The characters I need skipped are the commas and the last digit of each line, I can change the output of the text to be separated by any character (even tabbed), just chose the comma for this example.

-1.45,4.79,0
-0.168,5,0
1.12,4.87,0
2.3,4.44,0
8.32,1.51,0
8.69,1.07,0
8.92,0.546,0
9,0,0
8.92,-0.546,0
8.69,-1.07,0
8.32,-1.51,0
2.3,-4.44,0
1.12,-4.87,0
-0.168,-5,0
-1.45,-4.79,0
-2.6,-4.27,0
-3.58,-3.49,0
-4.36,-2.45,0
-4.84,-1.25,0
-5,0,0
-4.84,1.25,0
-4.36,2.45,0
-3.58,3.49,0
-2.6,4.27,0

A second question would be, Can I somehow loop the entry part so I can tell the program I have 25 lines of text, then it loops 25 times, reading the next line down and so forth until it finishes with the 25th line?

Edited by w00dr0w

Share this post


Link to post
Share on other sites



A second question would be, Can I somehow loop the entry part so I can tell the program I have 25 lines of text, then it loops 25 times, reading the next line down and so forth until it finishes with the 25th line?

_FileReadToArray() will read the file line by line into an array, and you can do a simple For/Next loop with that.

As far as your first question, I'm still having an issue of what the exact output you want is, I understand you don't want the last digit, and you want to skip the commas, but what do you mean by "skip" the commas? Just omit them? If that's the case, then look at StringReplace and or StringRegExp.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Maybe I should give a little explanation on how I'd like the script to work

I open a text file called points.txt that has 24 lines to it

the program reads the first set of digits (before the first comma, so in my previous txt example it would be -1.45) and saves that number into a string, then skip the first comma and save the second number in the line into the string, then moves onto the next line (skipping the last digit)

After all lines are saved into the string, Id like it to open a program (previously specified in the GUI) send the first number in the string, send a "tab" keystroke, send the second number in the string, then send an "enter" keystroke. repeat for numbers 3-24 (still sending a "tab" and "enter' keystroke like for the first two digits)

I've added an input box to the GUI so I can specify the number of lines in the file (as it will vary) so I could hopefully implement my second question

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

This has some ideas for you in it. It has a new Updown control in the upper right-hand corner of the GUI which is read from to determine the number of lines to use from the file, and the script uses an array to store the lines of text:

;My Phun Game hack. This script has a new Updown control in the upper right-hand corner 
;of the GUI which is read from to determine the number of lines to use from the file. 
;The script uses an array to store the lines of text, instead of a single string:
#include <GUIConstants.au3>
Global $LineArr[181]
Global $numLines
#Region ### START Koda GUI section
$Form1_1 = GUICreate("JD's importer", 258, 160, 193, 115)
$Label1 = GUICtrlCreateLabel("Select the pointfile for import", 8, 8, 200, 17)
$Input1 = GUICtrlCreateInput("", 8, 24, 130, 21)
$Input2 = GUICtrlCreateInput("1", 195, 24, 50, 21)
GUICtrlCreateUpdown($Input2)
GUICtrlSetLimit(-1, 180, 1)
$SelectPath_Btn = GUICtrlCreateButton("...", 142, 22, 30, 25, 0)
$Label2 = GUICtrlCreateLabel("Select the Phun_draw .exe (Phun_Draw_Object_2008_03_16.exe)", 8, 50, 200, 34)
$Input2 = GUICtrlCreateInput("", 8, 80, 200, 21)
$SelectPath2_Btn = GUICtrlCreateButton("...", 220, 80, 30, 25, 0)
$OK_Btn = GUICtrlCreateButton("OK", 70, 120, 30, 25, 0)
$Cancel_Btn = GUICtrlCreateButton("Cancel", 138, 120, 50, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### START Koda GUI section

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $Cancel_Btn
            Exit
        Case $SelectPath_Btn
            $Path1 = FileOpenDialog("Select pointfile", @MyDocumentsDir, "point file (*.txt)", 1)
            If Not @error Then GUICtrlSetData($Input1, $Path1)
        ;If $Path1 = '' Or @error = 1 Then ContinueLoop
        ;$Point_file = FileOpen($Path1, 0)
        ;$Point1 = FileReadLine($Point_file, 1)
        Case $SelectPath2_Btn
            $Path2 = FileOpenDialog("Select Program File", @ProgramFilesDir, "(*.exe)", 1)
            If Not @error Then GUICtrlSetData($Input2, $Path2)
            If StringInStr($Path2, " ") Then
                $myPhunExe = '"' & $Path2 & '"'
            EndIf
        Case $OK_Btn
            Get_Numbers()
            SendsStrokes()
        ;Run("notepad.exe")
        ;WinWaitActive("Untitled - Notepad")
        ;Send($Point1)
    EndSwitch
WEnd

Func Get_Numbers()
    Local $Point_file
    $numLines = GUICtrlRead($Input1)
    If Not FileExists($Path1) Then
        MsgBox(0, "Game Hack", 'This is or is not one of those "File dont exist" error popups.')
        Return
    EndIf
    $Point_file = FileOpen($Path1, 0)
    Sleep(220)
    For $i = 1 To $numLines
        $LineArr[$i] = FileReadLine($Point_file, 1)
        If @error Then Return
        If StringLen($LineArr[$i]) < 2 Then Return
        Sleep(60); you might be able to use less sleep here, like 30
    Next
    FileClose($Point_file)
EndFunc  ;==>Get_Numbers

Func SendsStrokes()
    Local $LinePartArr[4]
    Run($myPhunExe)
    WinWaitActive("My Phun Game WinTitle")
    For $i = 1 To UBound($LineArr) - 1
        $LineArr[$i] = StringStripWS($LineArr[$i], 3)
        $LinePartArr = StringSplit($LineArr[$i], ",")
        If IsArray($LinePartArr) And $LinePartArr[0] > 0 Then
            ControlFocus("Phun Game WinTitle", "", "PhunInputAControlID")
            ControlSend("Phun Game WinTitle", "", "PhunInputAControlID", $LinePartArr[1])
            ControlFocus("Phun Game WinTitle", "", "PhunInputBControlID")
            ControlSend("Phun Game WinTitle", "", "PhunInputBControlID", "{TAB}")
            ControlFocus("Phun Game WinTitle", "", "PhunInputAControlID")
            ControlSend("Phun Game WinTitle", "", "PhunInputAControlID", $LinePartArr[2])
            ControlFocus("Phun Game WinTitle", "", "PhunGoButtonControlID")
            ControlSend("Phun Game WinTitle", "", "PhunGoButtonControlID", "{ENTER}")
        EndIf
    Next
EndFunc  ;==>SendsStrokes
Edited by Squirrely1

Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites

Func _mySendToWindowTabs($hFile, $hWnd)
    If IsHWnd($hWnd) = 0 Then $hWnd = WinGetHandle($hWnd)
    ;Turn the string/file into an array we need (digits sperated by commas)
    Local $sString = $hFile, $aArray, $iCC
    If FileExists($hFile) Then $sString = FileRead($hFile)
    Local $aArray = StringRegExp($sString, "(?s)(-*\d*\.*\d*),(-*\d*\.*\d*),\d*(?m:$|\r\n)", 3)
    If IsArray($aArray) = 0 Then Return SetError(1, 0, 0)
    
    ;Now we have our array, activate the window
    If WinActive($hWnd) = 0 Then WinActivate($hWnd)
    Sleep(500);Let's make sure it's active, personally, I'd probably use controlsend so the form doesn't have to be active
    For $iCC = 0 To UBound($aArray) - 2
        Send($aArray[$iCC] & "{TAB}")
    Next
    ;Now that we have made it to the last tab, no sense in sending tab again and return back to what called this fun originally
    Return Send($aArray[$iCC + 1])
EndFunc


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Ok So I re-wrote the script from scratch using smoke_n's code example

When I run the script, I get

Jd.au3 (53) : ==> Subscript used with non-Array variable.:

Send($aArray[$iCC + 1])

Send($aArray^ ERROR

my code

#include <GUIConstants.au3>
$GUI1 = GUICreate("JD's Phun importer", 258, 160, 193, 115)
$PF_Label = GUICtrlCreateLabel("Select the pointfile", 8, 8, 100, 17)
$PF_Input = GUICtrlCreateInput("", 8, 24, 100, 25)
$PF_Button = GUICtrlCreateButton("...", 120, 24, 30, 25)
$PF_NumLabel = GUICtrlCreateLabel("Number of points", 165, 8 , 100, 17)
$PF_Num = GUICtrlCreateInput("1", 180, 24, 50, 25)
$PF_NumLimit = GUICtrlSetLimit($PF_Num, 3, 1)
$PF_UpDown = GUICtrlCreateUpdown($PF_Num)
$APP_Label = GUICtrlCreateLabel("Select the Phun_draw .exe", 8, 65, 200, 17)
$APP_Input = GUICtrlCreateInput("", 8, 80, 200, 25)
$APP_Button = GUICtrlCreateButton("...", 220, 80, 30, 25)
$OK_Button = GUICtrlCreateButton("OK", 70, 120, 30, 25)
$Cancel_Btn = GUICtrlCreateButton("Cancel", 138, 120, 50, 25)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $Cancel_Btn
            Exit
        Case $PF_Button
            $hFile = FileOpenDialog("Select Pointfile", @DesktopDir, "*point file (*.txt)")
            GUICtrlSetData($PF_Input, $hFile)
            If $hFile = '' Or @Error = 1 Then
                MsgBox(1, "An Error Occurred", "No file by that name exists, Please select a correct pointfile")
                ContinueLoop
            EndIf
        Case $APP_Button
            $hWnd = FileOpenDialog("Select Program File", @ProgramFilesDir, "(*.exe)", 1, "Phun_Draw_Object_2008_03_16.exe")
            GUICtrlSetData($APP_Input, $hWnd)
            If $hWnd = '' Or @Error = 1 Then
                MsgBox(1, "An Error Occurred", "Incorect or no program found, Please select the correct program")
                ContinueLoop
            EndIf
        Case $OK_Button
            If IsHWnd($hWnd) = 0 Then $hWnd = WinGetHandle($hWnd)
        ;Turn the string/file into an array we need (digits sperated by commas)
            Local $sString = $hFile, $aArray, $iCC
            If FileExists($hFile) Then $sString = FileRead($hFile)
            Local $aArray = StringRegExp($sString, "(?s)(-*\d*\.*\d*),(-*\d*\.*\d*),\d*(?m:$|\r\n)", 3)
            If IsArray($aArray) = 0 Then SetError(1, 0, 0)
            
        ;Now we have our array, activate the window
            If WinActive($hWnd) = 0 Then WinActivate($hWnd)
            Sleep(500);Let's make sure it's active, personally, I'd probably use controlsend so the form doesn't have to be active
            For $iCC = 0 To UBound($aArray) - 2
                Send($aArray[$iCC] & "{TAB}")
            Next
        ;Now that we have made it to the last tab, no sense in sending tab again and return back to what called this fun originally
            Send($aArray[$iCC + 1])
            Sleep(1000)
            MsgBox(1, "Status", "Import Complete")
        EndSwitch
    WEnd
Edited by w00dr0w

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Check the error codes returned by this line:

Local $aArray = StringRegExp($sString, "(?s)(-*\d*\.*\d*),(-*\d*\.*\d*),\d*(?m:$|\r\n)", 3)
see help file Edited by Squirrely1

Das Häschen benutzt Radar

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