Jump to content

Reading lines in a .txt files and performing an action for each line


Recommended Posts

Good Day AUTOIT forums,

First, thank you for reading this. I am working on learning Auto IT and struggling a little as the task put before me is well above what a beginner should be doing, but I figure if I can work through this complicated script I will be good to go for future scripting! For background, I have minimal scripting experience (all in PASCAL interacting with a legacy system) so please bear with my ignorance of general programming awesomeness.

What I am attempting to do, is create a GUI that will allow the user to set a series of options. Then perform an action based on what options are selected for each file which is listed in a .txt document. The goal is to automate the uploading of documents from my computer to a website. 

I've tried structuring this a few different ways and keep running into problems, so I am looking for general advice on how to structure such a script, and if anyone has any examples that would be immensely helpful. 

Each file that needs to be uploaded will correspond to a 3 digit number that will be concatenated into both the URL where the document is to be uploaded and the file path of the document itself, thus the .txt document is a series of 3 digit numbers.

Also! Right now I have it to where it is opening and closing an instance of IE for each file in the list, this is not my intention, but where I ended up in trying to get this working today. 

Please see below and let me know if you have any suggestions. Thanks in advance for your help. 

#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
#include <Excel.au3>
#include <FileConstants.au3>
#include <File.au3>

;Options
Opt("WinTitleMatchMode", 2)

;creates the GUI
Local $hGUI = GUICreate("Intranet Upload Menu", 600, 220)
Local $iOKButton = GUICtrlCreateButton("Upload", 500, 10, 60)
Local $iFilepath = GUICtrlCreateInput("Paste the folder you want to upload",10,-1,400)
Local $iUploadlocation = GUICtrlCreatelabel("Please select where you want the files to upload",10,50)
Local $idCreateFolder = GUICtrlCreateCheckbox("Create Folder",10,70)
Local $idBudget = GUICtrlCreateRadio("Budget",10,90)
Local $idOccupancy = GUICtrlCreateRadio("Occupancy Reports",10,110)
Local $idTargets = GUICtrlCreateRadio("GM Targets",10,130)
Local $idRentalRate = GUICtrlCreateRadio("Rental Rate Worksheet",10,150)
GUISetState(@SW_SHOW, $hGUI)

;sets hotkey to terminate script
HotkeySet("{Esc}", "_Terminate")

;Case statement
While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg

        Case $GUI_EVENT_CLOSE
        MsgBox($MB_SYSTEMMODAL, "Exit", "You are now exiting the script")
        ExitLoop

        Case $iOKButton

            ;Starts the main loop
            Local $ainput
            $file = "D:\Users\CCole\Desktop\input.txt"

            ;open file and read each line, named to array $ainput
            _FileReadToArray($file, $ainput)
            For $i = 1 to UBound ($ainput) -1 ;ubond is the array size
            
                ;MsgBox (0,"",$ainput[$i])

                ;these are actions to be taken in the main loop
                    Run('C:\Program Files\Internet Explorer\iexplore.exe')
                    WinWaitActive("Google – Windows Internet Explorer")
                    Local $oIE = _IEAttach("Google – Windows Internet Explorer")
                    _IELoadWait($oIE)
                    _IENavigate($oIE, "https://some.website/" & $file & "/the.rest.of.the.path")
                    _IEQuit ($oIE)
                

            ;ends the main loop
            Next
            ExitLoop


    EndSwitch

            ;If $i = 710 then Exit

WEnd
Link to comment
Share on other sites

Hustlasaurus,

First, some general comments. 

Don't declare vars or read files in a loop.  See comments in the code below. 

Your input control can be drag and drop by using the WS_EX_ACCEPTFILES extended style (see the help file).

You will get better response to queries in the future if you post runnable code (the _terminate function was missing).

The "exitloop" in the case stmt for $iOKButton terminates the script.  Is that your intent?

Secondly, I am unable to dicern what you want to do with the input control.  Your control text references a "folder".  I interpreted this to be the file containing the codes for the files that you want to upload...??? 

Finally, you can find examples of how to upload files using the search function.  I don't have anything handy and rarely do this kind of thing.

#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
#include <Excel.au3>
#include <FileConstants.au3>
#include <File.au3>

;Options
Opt("WinTitleMatchMode", 2)

;creates the GUI
Local $hGUI = GUICreate("Intranet Upload Menu", 600, 220)
Local $iOKButton = GUICtrlCreateButton("Upload", 500, 10, 60)
Local $iFilepath = GUICtrlCreateInput("Paste the folder you want to upload",10,-1,400)
Local $iUploadlocation = GUICtrlCreatelabel("Please select where you want the files to upload",10,50)
Local $idCreateFolder = GUICtrlCreateCheckbox("Create Folder",10,70)
Local $idBudget = GUICtrlCreateRadio("Budget",10,90)
Local $idOccupancy = GUICtrlCreateRadio("Occupancy Reports",10,110)
Local $idTargets = GUICtrlCreateRadio("GM Targets",10,130)
Local $idRentalRate = GUICtrlCreateRadio("Rental Rate Worksheet",10,150)
GUISetState(@SW_SHOW, $hGUI)

;sets hotkey to terminate script
HotkeySet("{Esc}", "_Terminate")

local $ainput, $file = "D:\Users\CCole\Desktop\input.txt"
_FileReadToArray($file, $ainput)

; this is the start of your message loop
; Declare vars and read the file before you get into the loop, as above

While 1

    $iMsg = GUIGetMsg()
    Switch $iMsg

        Case $GUI_EVENT_CLOSE
            MsgBox($MB_SYSTEMMODAL, "Exit", "You are now exiting the script")
            ExitLoop

        Case $iOKButton

            ; iterate through the array
            For $i = 1 to UBound ($ainput) -1 ;ubound is the array size

                ;MsgBox (0,"",$ainput[$i])

                ;these are actions to be taken in the main loop
                    Run('C:\Program Files\Internet Explorer\iexplore.exe')
                    WinWaitActive("Google – Windows Internet Explorer")
                    Local $oIE = _IEAttach("Google – Windows Internet Explorer")
                    _IELoadWait($oIE)
                    _IENavigate($oIE, "https://some.website/" & $file & "/the.rest.of.the.path")
                    _IEQuit ($oIE)


            ;ends the main loop
            Next

            ; you don't really want this exitloop here as it will terminate the script
            ;ExitLoop


    EndSwitch

            ;If $i = 710 then Exit

WEnd

func _terminate()
    Exit
endfunc

Good Luck,

kylomas

edit: additional info

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

  • 2 weeks later...

Hi FUBAR,

Thanks so much for your help. I've revised the set up a little based on your instructions. I'm going to narrow the scope of the GUI for now til I can get it running then come back and add functionality later once my skills improve. Currently, I am trying to set it up so that an IE window will be created that changes based on what radio button is selected. However, It only ever uses the first Case. I've read over multiple posts here and tried it a few different ways but none of them seem to work for me. This is the last executable version I had. 

#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
#include <Excel.au3>
#include <FileConstants.au3>
#include <File.au3>

;Options
Opt("WinTitleMatchMode", 2)

;creates the GUI
Local $hGUI = GUICreate("Intranet Upload Menu", 600, 220)
Local $iOKButton = GUICtrlCreateButton("Upload", 500, 10, 60)
Local $iUploadlocation = GUICtrlCreatelabel("Please select where you want the files to upload",10,30)
Local $idBudget = GUICtrlCreateRadio("Budget",10,50)
Local $idOccupancy = GUICtrlCreateRadio("Occupancy Reports",10,70)
Local $idTargets = GUICtrlCreateRadio("GM Targets",10,90)
Local $idRentalRate = GUICtrlCreateRadio("Rental Rate Worksheet",10,110)
;Local $idCreateFolder = GUICtrlCreateCheckbox("Create Folder?",10,140)
;Local $iFoldername = GUICtrlCreateInput("What should we call the new folder?",10,160,400)
GUISetState(@SW_SHOW, $hGUI)

;sets hotkey to terminate script
HotkeySet("{Esc}", "_Terminate")

local $ainput, $GUIselect, $file = "D:\Users\CCole\Desktop\input.txt"
_FileReadToArray($file, $ainput)

; this is the start of your message loop
; Declare vars and read the file before you get into the loop, as above

While 1

    $iMsg = GUIGetMsg()
    Switch $iMsg

        Case $GUI_EVENT_CLOSE
            MsgBox($MB_SYSTEMMODAL, "Exit", "You are now exiting the script")
            ExitLoop

        Case $iOKButton

            ; iterate through the array
            For $i = 1 to UBound ($ainput) -1 ;ubound is the array size

                ;MsgBox (0,"",$ainput[$i])

                ;these are actions to be taken in the main loop
                Switch $GUIselect

                    Case $idBudget  And BitAND(GUICtrlRead($idBudget ), $GUI_CHECKED) = $GUI_CHECKED
                    $url = "https://intranet.studenthousing.com/Property/" & $ainput[$i] & "/Budgets/Budget/Forms/AllItems.aspx"

                    Case $idOccupancy  And BitAND(GUICtrlRead($idOccupancy ), $GUI_CHECKED) = $GUI_CHECKED
                    $url = "https://intranet.studenthousing.com/Property/" & $ainput[$i] & "/Budgets/Occupancy%20Reports/Forms/AllItems.aspx"

                    Case $idTargets  And BitAND(GUICtrlRead($idTargets ), $GUI_CHECKED) = $GUI_CHECKED
                    $url = "https://intranet.studenthousing.com/Property/" & $ainput[$i] & "/Budgets/GM%20Targets/Forms/AllItems.aspx"

                    Case $idRentalRate  And BitAND(GUICtrlRead($idRentalRate ), $GUI_CHECKED) = $GUI_CHECKED
                    $url = "https://intranet.studenthousing.com/Property/" & $ainput[$i] & "/Budgets/Rental%20Rate%20Worksheets/Forms/AllItems.aspx"
;This is not reading what is checked and performing the appropriate action, need to work on GUI and case statement.

                EndSwitch

                    ;$url = "https://intranet.studenthousing.com/Property/" & $ainput[$i] & "/Budgets/default.aspx"
                    Msgbox (0,"",$url)
                    Local $oIE = _IECreate($url)
                    Sleep(5000)


            ;ends the main loop
            Next

    EndSwitch

            ;If $i = 710 then Exit

WEnd

func _terminate()
    Exit
endfunc
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...