Jump to content

Read delimited file and process task for each line


Recommended Posts

I am looking for how to make AutoIt read a flat file line by line that has been delimited by a specified character like a "," or "^" and place each field in a variable to be processed by the script.

Currently what I do is edit the script by hand for each line and run the script.

I would like to make it do a while loop and execute the script line by line with the values from the csv. The csv has numerous lines with different items on each line.

CSV Format:

$NAME,$SIZE,$ID,$PRICE,$COST

$NAME,$SIZE,$ID,$PRICE,$COST

$NAME,$SIZE,$ID,$PRICE,$COST

Script:

WinWaitActive("This window's title")

Send("!n")

Sleep(1000)

Send("{TAB}G{TAB}YL{TAB}$NAME{TAB}{TAB}{TAB}$SIZE{TAB}{TAB}{TAB}{TAB}{TAB}$ID{TAB}{TAB}{TAB}$PRICE{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}$COST{TAB}$COST{TAB}{SPACE}")

Sleep(1000)

Any help would be appreicated.

--W

Link to comment
Share on other sites

I would like to make it do a while loop and execute the script line by line with the values from the csv.

Tis is taken from the helpfile. (FileReadLine)

$file = FileOpen("list.txt", 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    MsgBox(0, "Line read:", $line); <---just to control the lines. you can do your stuff with each line instead of the msgbox
Wend
FileClose($file)

arctor

Link to comment
Share on other sites

  • Developers

Thanks, but how do I parse the txt file with the delimiter specified and add each field to a variable?

--W

As tutor said... stringsplit will spilt up your record into an array which you then can use... have a look in the helpfile for stringsplit... :D

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Thanks, I did, but I don't understand it very well as I not really a programmer.

--W

Arrays still give me a hard time as a newbie as well

You're not starting off with a very easy project that's for sure. It would take me as much time or longer to write it for you than it would for you to do it.

You'll need to lookup For...Next and Ubound to get an idea of what to do with the array once you have used your stringsplit

Hope I helped a tad

Rick

Link to comment
Share on other sites

  • Developers

Thanks, I did, but I don't understand it very well as I not really a programmer.

--W

Well you do the following:

Assume your record is read into var $Irec:

"NAME,SIZE,ID,PRICE,COST"

Code:

$Rec = StringSplit($irec,",")

This will create an array will the following content:

$Rec[0] = 5 ; the number of fields...

$Rec[1] = "NAME"

$Rec[2] = "SIZE"

$Rec[3] = "ID"

$Rec[4] = "PRICE"

$Rec[5] = "COST"

Ok ?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Ok, so here is what I have come up with, but it keep printing NAME on each line.

$file = FileOpen("list.txt", 0)

; Check if file opened for reading OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

; Read in lines of text until the EOF is reached

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

$Rec = StringSplit($line,"^")

$Rec[0] = 5

$Rec[1] = "NAME"

$Rec[2] = "SIZE"

$Rec[3] = "ID"

$Rec[4] = "PRICE"

$Rec[5] = "COST"

WinWaitActive("Untitled - Notepad")

Send("!n")

Sleep(1000)

Send($Rec[1])

Send("{ENTER}")

Sleep(1000)

Wend

FileClose($file)

Link to comment
Share on other sites

  • Developers

I guess i did confuse you here... :D

I meant with $Rec[1] = "NAME" that $Rec[1] will contain the value "NAME"...

So try this and see if that works...

$FILE = FileOpen("list.txt", 0)
; Check if file opened for reading OK
If $FILE = -1 Then
   MsgBox(0, "Error", "Unable to open file.")
   Exit
EndIf
; Read in lines of text until the EOF is reached
While 1
   $LINE = FileReadLine($FILE)
   If @error = -1 Then ExitLoop
   $REC = StringSplit($LINE, "^")
   Send($REC[1])
   Send("{ENTER}")
   Sleep(1000)
Wend
FileClose($FILE)
Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Thanks, This works well, Here is what I came up with. Is this the best way to do it?

$file = FileOpen("list.txt", 0)

; Check if file opened for reading OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

; Read in lines of text until the EOF is reached

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

$Rec = StringSplit($line,"^")

WinWaitActive("Untitled - Notepad")

Send("!n")

Sleep(1000)

Send($Rec[1])

Send("{TAB}")

Send($Rec[2])

Send("{TAB}")

Send($Rec[3])

Send("{TAB}")

Send($Rec[4])

Send("{TAB}")

Send($Rec[5])

Send("{TAB}")

Send($Rec[5])

Send("{ENTER}")

Sleep(1000)

Wend

FileClose($file)

--W

Link to comment
Share on other sites

  • Developers

Looks fine .... (you do have 2 times $Rec[5] in there :D )...

If you have a lot more fields in a record you could do the send stuff with a for .. next loop but with this number of fields it isn't an issue.

Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Is this the best way to do it?

.. depends :D If you especially want to automate notepad, then you could do it that way.

But if you want to just replace the commas with tabs, you could simplify it like this (untested):

;Get the entire file into a string
    $sFile = "list.txt"
    $sFileText = FileRead($sFile , FileGetSize($sFile))
;Replace "," with @TAB
    $sFileText = StringReplace($sFileText, ",", @TAB)
;Rewrite the file
    FileWrite($sFile, $sFileText)

.. if you want to do other things like duplicate the fifth field, you could manipulate $sFileText with StringSplit as explained above .. but you don't need to use notepad to write your file :huh2:

HTH

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