WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: sst001 on July 08, 2019, 10:12:11 AM

Title: How to process each line in a file?
Post by: sst001 on July 08, 2019, 10:12:11 AM
Hi guys,

I have a .txt file full of single line words, and phrases. For example, a .txt file with 20 lines, each line = 1 to 5 words.

I would like to send each line through some processing via a gosub routine until EOF.

Here is what I'm using:

;Read through file line by line
termfile = "c:\kde\searchterms.txt"
handle = FileOpen(termfile, "READ")
count = 0
While @TRUE ; Loop till break do us end
   line = FileRead(handle)
   If line == "*EOF*" Then Break
   count = count + 1
term = StrCat("",line)
gosub process_term
EndWhile
FileClose(handle)


Everything works fine until the gosub process_term return which errors out at 'line=FileRead (handle)'. So when the gosub is returned for the next line in the file, it bombs. I'm thinking I need a for loop under "count = count + 1"?

How do I send each line from termfile down gosub process_term for processing, and then when EOF hits processing stops?

Hope this enough info.

Thanks!
ST
Title: Re: How to process each line in a file?
Post by: td on July 08, 2019, 10:24:46 AM
It is impossible to answer your question without knowing the exact error you are receiving and the content of your subroutine.
Title: Re: How to process each line in a file?
Post by: sst001 on July 09, 2019, 06:33:02 AM
Thanks, this solved it for me:

filename = "c:\test\test.txt"
file = FileGet(filename)
file = StrReplace(file,@CRLF,@TAB)
llinecount = ItemCount(file,@TAB)

For ii = 1 To linecount

  line = ItemExtract(ii,file,@TAB)

  ; You would do your stuff here

Next

Exit
Title: Re: How to process each line in a file?
Post by: snowsnowsnow on July 09, 2019, 07:01:08 PM
I guess all's well that ends well.
Title: Re: How to process each line in a file?
Post by: td on July 10, 2019, 06:40:06 AM
Something involving a reference to sleeping dogs might be appropriate here.