Jump to content

reading from csv for automation


Recommended Posts

I am sure this has been asked and answered 1000 times but I could not find what I am looking for in the forum.

 

I am trying to automate a repetitive task by using the data in a csv file.

 

Example data

0001,1,1,1999

0002,1,1,1998

0003,2,1,2000

ect (5000 rows long so you can see how time consuming this is to type manually!)

 

I pulled the csv into an array but I need to skip the first line (info about the array) and then use the "Send ( xxxxx )" command to step through each column and then move on to the next row.

 

Please help me with the bit that allows me to start with row 1 and address each column then repeat with row 2 ect...

 

thanks!

#include <MsgBoxConstants.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <File.au3>
#include <Array.au3>

Global $g_bPaused = False

HotKeySet("{PAUSE}", "HotKeyPressed")
HotKeySet("{ESC}", "HotKeyPressed")

 Local $aArray = 0
 _FileReadToArray("C:\mfgdates.csv", $aArray, Default, ",")
Local $iRows = UBound($aArray, $UBOUND_ROWS)
Local $iCols = UBound($aArray, $UBOUND_COLUMNS)
Local $iDimension = UBound($aArray, $UBOUND_DIMENSIONS) ; The dimension of the array e.g. 1/2/3 dimensional.
MsgBox($MB_SYSTEMMODAL, "", "The array is a " & $iDimension & " dimensional array with " & _
            $iRows & " row(s) & " & $iCols & " column(s).")

IPTOOL()

Func IPTOOL()
   If @error Then
      MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error)
   Else
      For $i = 0 To $iRows - 1 ;this doesnt work (sad face)
         Send($aArray[column1]) ;how do I declare column1, 2, 3, 4???
         Sleep(500)
         Send("{ENTER}")
         Sleep(6000)
         Send($aArray[column2])
         Sleep(500)
         Send("{ENTER}")
         Sleep(5000)
         Send($aArray[column3])
         Sleep(500)
         Send("{ENTER}")
         Sleep(5000)
         Send($aArray[column4])
         Sleep(500)
         Send("{ENTER}")
         Sleep(5000)
         MsgBox($MB_SYSTEMMODAL, "", $aArray[column1] & "," & " " & $aArray[column2] & "," & " " & $aArray[column3] & "," & " " & $aArray[column4])
      Next
   EndIf
EndFunc

Func HotKeyPressed()
    Switch @HotKeyPressed
        Case "{PAUSE}"
            $g_bPaused = Not $g_bPaused
            While $g_bPaused
                Sleep(100)
                ToolTip('Script is "Paused"', 0, 0)
            WEnd
            ToolTip("")

        Case "{ESC}"
            Exit
    EndSwitch
EndFunc

 

Link to comment
Share on other sites

You can use something like the following (fixed issue with "If @error") in IPTool function since this would return the @error from MsgBox not _FileReadToArray hope that makes sense.

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <File.au3>
#include <Array.au3>

Global $g_bPaused = False

HotKeySet("{PAUSE}", "HotKeyPressed")
HotKeySet("{ESC}", "HotKeyPressed")

 Local $aArray
_FileReadToArray("C:\mfgdates.csv", $aArray, Default, ",")
    If @error Then Exit MsgBox(4096, "", "There was an error reading the file. @error: " & @error)
Local $iRows = UBound($aArray, $UBOUND_ROWS)
Local $iCols = UBound($aArray, $UBOUND_COLUMNS)
Local $iDimension = UBound($aArray, $UBOUND_DIMENSIONS) ; The dimension of the array e.g. 1/2/3 dimensional.
MsgBox($MB_SYSTEMMODAL, "", "The array is a " & $iDimension & " dimensional array with " & _
            $iRows & " row(s) & " & $iCols & " column(s).")

IPTOOL()

Func IPTOOL()
    Local $sMsgBoxText = ""
    For $i = 1 To $iRows - 1
        For $j = 0 To $iCols - 1
             Send($aArray[$i][$j])
             Sleep(500)
             Send("{ENTER}")
            If $j = $iCols - 1 Then
                $sMsgBoxText &= $aArray[$i][$j]
            Else
                $sMsgBoxText &= $aArray[$i][$j] & ", "
            EndIf
        Next
        Sleep(6000)
        MsgBox(4096, "", $sMsgBoxText)
        $sMsgBoxText = ""
    Next
EndFunc

Func HotKeyPressed()
    Switch @HotKeyPressed
        Case "{PAUSE}"
            $g_bPaused = Not $g_bPaused
            While $g_bPaused
                Sleep(100)
                ToolTip('Script is "Paused"', 0, 0)
            WEnd
            ToolTip("")

        Case "{ESC}"
            Exit
    EndSwitch
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...