How to Parse Data from an ASCII file Using FileRead
Keywords: parse fileread
Question:
I want to read a line from a flat (ASCII) file. I need to
parse that data into different variables and then complete fields on a form
(screen) using the data. This process would loop until
the end of the file. How do I do this?
Answer:
There are two approaches. Here is the easier one.
(The harder one involves the Binary functions.
This code is just off the top. It never ever has actually been run,
so you will need to clean it up a bit.
Debug(@on) ; start off in debug mode for testing
fn="c:\data\mydatafile.txt"
fh=FileOpen(fn,"READ")
while 1 ; loop for ever
line=FileRead(fh)
if line="*EOF*" then break ; End of file found
; now is a tricky part, parsing the line
; Good Luck.
; For tab and *maybe* csv files use the ItemExtract Function
; For fixed size records use the strsub function
; Tab delimited example
name=ItemExtract(1,line,@tab)
addr1=ItemExtract(2,line,@tab)
; Fixed record size example
name=strsub(line,1,30)
addr1=strsub(line,31,60)
; Now to send your data to the application. I suggest
; passing the data thru the clipboard so you don't have
; to worry about weird characters in the input data
; confusing the SendKey statement
Clipput(name)
SendKeysTo("YourApp","!fo{TAB}{TAB}^v")
;I dunno what you need to do, but the above will send
; Alt-FO TAB TAB and then a Ctrl-v to paste the clipboard in there
Clipput(addr1)
SendKeysTo("{TAB}^v")
; and this does TAB to move to the next field and then pastes there
SendKeysTo("Yourapp","~") ; The Tilde is shorthand for ENTER
; and loop to do it again.
endwhile
FileClose(fh)
Good Luck. Drink LOTS of coffee.
Article ID: W13241
Filename: Parse Data from ASCII file using FileRead.txt