Jump to content

Read Data from File


Guest FoxZapper
 Share

Recommended Posts

Guest FoxZapper

All,

I am the newest member here. I was using AutoIT2 earlier.

I have just shifted to V3.

The tool rocks. But, I have a question for the Gurus.

I am trying to make a bot program to enter data in a client server application.

I wish to read data from a file (EXCEL or a comma/pipe delimited file) and use this data to enter values in an application.

For instance, I receive orders from the web which I store in an ASCII file and need to use the data to enter Purchase Orders etc into a different application.

I thought I would use an EXCEL file or a delimited ASCII file. I searched the forum and have found that a single line can be read. In my case, I wish to break down the line into meaningful bits.

Can someone offer suggestions?

Thanks.

Link to comment
Share on other sites

Reading an Excel file directly would be a bit difficult in AutoIt3, but it can handle CSV files very well. Mind that Excel often 'modifies' CSV files by adding double quotes "" to fields containing the separator (",") character.

Below is a code snippet I used in a program. It is based on a UDF (User Defined Function) _FileReadToArray by Jon, but I modified it a bit to handle the Excel 'quirks'.

Regards,

-Sven

Local $Filename; Name of the CSV File
 Local $hFile   ; Filehandle to opened file
 Local $aArray   ; One line of the hotfix list, splitted into an array
 Local $Counter   ; just a counter  

 $Filename = "C:\Myfile.csv"

 $hFile = Fileopen( $FileName, 0)
 if $hFile = -1 then
    Msgbox(0,"","Error: Could not open CSV file")
   Return -1
 endif

; Reading all lines from the CSV File

 while FileReadLineToArray( $hFile, $aArray, "," ) > 1

;  Do your stuff here.

  For $Counter = 1 to aArray[0]  ; for each element on this line...

 ;  Do something with aArray[$Counter]

  Next


 wend
 
 FileClose($hFile)


;===============================================================================
Func FileReadLineToArray( $hFile, ByRef $aArray, $sSeparator )
;
; It reads one line of a delimited file into an array of strings.
;
;
; Parameter(s):  $hFile   - Handle to the already opened file
;                  $aArray   - The array to store the line of the file
;            $sSeparator - The separation character (e.g. "," or ";").
;
; Requirement(s):   A file must be opened first.
;
; Return Value(s):  On Success   - Returns Number of elements
;                  On Failure   - Returns -1 and sets @error = 1
;                  On EndofFile - Returns -2 and sets @error = -1
;                  On LineError - Returns -3 and sets @error = -2
; Author(s):        Sven Pechler
; Note(s):        Based on _FileReadToArray of Jonathan Bennett <jon at hiddensoft com>
;
; When you export an Excel sheet to CSV it will really scramble the output:
; It uses the semicolon (;) as a separator (instead of a comma)
; When a field contains a double quote (") then Excel will surround it with additional double quotes 
; and it will double all double quotes in that field.


  Local $sLine   ; One line read from the file
  Local $Counter; Counter for each element in the array


  If $hFile = -1 Then; Check if the file handle is valid
    SetError( 1 )
    Return -1
  EndIf

  $sLine = FileReadLine( $hFile); Read one line from the file

  if @error <> 0 then   ; End of file reached ?
    SetError( -1 )
    Return -2
  Endif

  $aArray = StringSplit( $sLine , $sSeparator )   ; Fill the array 

  if @error <> 0 then       ; Filling the array was successful ?
    SetError( -2 )
    Return -3
  Endif


; Remove double double quoting ("") and replace it a single double quote (")
;
; then Trim surrounding "-characters, if present

  for $Counter = 1 to $aArray[0]

    $aArray[$Counter]=StringReplace($aArray[$Counter], '""', '"')

    if StringLeft($aArray[$Counter],1)='"' and StringRight($aArray[$Counter],1)='"' then
    $aArray[$Counter]=StringMid($aArray[$Counter],2,StringLen($aArray[$Counter])-2)
    endif
  next


  Return $aArray[0] 
EndFunc
Edited by SvenP
Link to comment
Share on other sites

I'd recommend a tab delimited ascii file: you can import / export this directly with EXCEL for easier viewing.

As for reading the data in .. you can read line by line and StringSplit by @TAB .. or you can read the entire file into a string which you StringSplit by @LF (StringReplace @CRLF to @LF first); then each element (row) in that array can be StringSplit by @TAB if/when necessary

HTH :lol:

PS: Welcome to AU3 :idiot:

Edit: looks like SvenP beat me to the post :D

Edited by trids
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...