Best way to read file line by line and add to varaible

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
gchichester
Pro Scripter
Posts: 132
Joined: Mon Dec 22, 2008 4:56 pm
Location: St Augustine FL

Best way to read file line by line and add to varaible

Post by gchichester » Fri Mar 08, 2013 4:53 pm

What's the correct way to read a comma delimited .txt file one line at a time assign to a varaible, read the new varaibles, get the next line from the file and assign to the same varaibles names and read them again, repeat until the EOF.
here's what I have so far, works only for the first line of the file, then ##NOFILE## error.

Thanks in advance for any and all suggestions.
Gil

Code: Select all

If>InldNum>1
GoSub>GetInldFileData
Wait>sw
SRT>GetInldFileData
Let>k=0
Label>Start
Let>k=k+1
ReadLn>C:\CP_Macros\JohnW\Europe\Inland\%InldFileName%,k,strLine
If>strLine=##EOF##,end
Separate>strLine,comma,InldFileName
Let>vInvAmt=InldFileName_1
Let>vExpAmt=InldFileName_2
Let>vVcode=InldFileName_3
Send>vInlandSC
Press Tab * 3
Send>vInvAmt
Press Tab * 2
Send>vExpAmt
Press Tab
Send>vVcode
Press Tab
Wait>sw
Let>FIP_SCANPIXELS=ALL
FindImagePos>%SCRIPT_DIR%\InldSCCode.bmp,SCREEN,0,1,XArr,YArr,numFound
If>numFound=0
  MessageModal>Image Not Found
  Exit>0
    Endif
Endif
MouseMove>XArr_0,YArr_0
LClick
Wait>.50
    Goto>Start
Label>end
End>GetInldFileData


Jerry Thomas
Macro Veteran
Posts: 267
Joined: Mon Sep 27, 2010 8:57 pm
Location: Seattle, WA

Post by Jerry Thomas » Fri Mar 08, 2013 8:00 pm

You are reading InldFileName and then using that same variable to store your separated line text.

...
ReadLn>C:\CP_Macros\JohnW\Europe\Inland\%InldFileName%,k,strLine
If>strLine=##EOF##,end
Separate>strLine,comma,InldFileName
...
Thanks,
Jerry

[email protected]

hoangvo81
Pro Scripter
Posts: 69
Joined: Tue Feb 07, 2012 8:02 pm

Post by hoangvo81 » Fri Mar 08, 2013 8:48 pm

There might be better suggestion as i m still pretty new to MS, but i would readfile into a variable, and separate it into variables and then separate each line into another variable. how fast it runs compare to reading it line by line. i m not sure, but the readfile alone should be faster than line by line.

Code: Select all

readfile>%file%,content
//separate the contenct by  carraiage return / linefeed into lines array

separate>content,crlf,lines
let>k=0
//go through the array

repeat>k
  add>k,1
  //seaparate each line's content by comma 
  separate>lines_%k%,%COMMA%,fields
 
  send>vInlandSC
  press tab*3

 //send from the variable fields 
  send>fields_1
  press tab*2
  send>fields_2
  press tab
  send>fields_3
  press tab
  wait>sw
  .....
//repeat until k reach line's count
Until>k=lines_count
[/code]

Jerry Thomas
Macro Veteran
Posts: 267
Joined: Mon Sep 27, 2010 8:57 pm
Location: Seattle, WA

Post by Jerry Thomas » Sat Mar 09, 2013 12:38 am

I haven't compared the 2 techniques to see which one is faster, but my gut feeling is that they would be very close. You are doing the same amount of work, just in a different order.

It is just a personal preference for me to read a line, do whatever needs to be done, and then read the next line.

If someone is working with files that are 10,000s of lines, then it would be worth the time to compare.

The one BIG difference, is that the file can be released much faster in your suggested approach. So if the file is on a server, your suggestion would be safer.

Speaking of different techniques...
I personally don't like using Labels. My approach would be something like this:

Code: Select all

...
Let>COMMA=,
Let>DataFile>C:\CP_Macros\JohnW\Europe\Inland\%InldFileName%
Let>cnt=1
ReadLn>DataFile,cnt,LineData

While>LineDataNOT_EQUAL##EOF##   
//NOT_EQUAL = LessThan followed by GreaterThan symbols. LessThan doesn't behave in samples like this
  Separate>LineData,COMMA,LineDataArr
  Let>vInvAmt=LineDataArr_1
  Let>vExpAmt=LineDataArr_2
  Let>vVcode=LineDataArr_3
  ...
  Let>cnt=cnt+1
  ReadLn>DataFile,cnt,LineData
EndWhile
Thanks,
Jerry

[email protected]

hoangvo81
Pro Scripter
Posts: 69
Joined: Tue Feb 07, 2012 8:02 pm

Post by hoangvo81 » Mon Mar 11, 2013 9:00 pm

as you can see there's many option.
here's another option to think about, if you have jet 4 driver and since this is a csv file.

replace the HDR=Yes, to HDR=No, if the first line is actual data and not header column name.
[code]
let>filename=abc.txt

let>constr=Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%SCRIPT_DIR%\;Extended Properties="text;HDR=Yes;FMT=Delimited";
dbConnect>%constr%,dbH
dbquery>dbH,select * from %filename%,rst,rNum,fNum
if>rNum>0
let>k=0
repeat>k
add>k,1
//insert you set of codes.
.....
until>k=fNum
endif

[/code]

gchichester
Pro Scripter
Posts: 132
Joined: Mon Dec 22, 2008 4:56 pm
Location: St Augustine FL

Best way to read file line by line and add to varaible

Post by gchichester » Tue Mar 12, 2013 11:14 am

Thanks to all for responding with great suggestions and options.
Jerry's pointing out my varaible porblem and his post using the While is what worked best for me because I using small files.
On to the next piece of the project.

Thanks again for your help!
Gilc

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts