Constantly display the last five lines of an Excel or CSV file

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Chris70
Posts: 46
Joined: 17 Dec 2019, 12:25

Constantly display the last five lines of an Excel or CSV file

24 Jan 2020, 14:58

I am working with a .csv file that is updating every 1-2 minutes with appended new data. I am wanting a script that runs continuously and just displays the last 5 lines of data in the file (or less if there are less than 5 lines of data) in a window on the screen. I would like the csv header to be shown at the top and the running last 5 lines of data scrolling beneath. Has anyone done something similar before?

I have attached three files that simulate a few successive iterations of the file.

Using the last 5 lines from the "-01" file as an example, the screen would display a small window with something like this:

Pallet ID Quantity Product ID Loc. Code
RO00590089008 160 72673 TBD
RO00589689012 84 70191 TBD
RO00589700018 18 47957 TBD
RO00589700020 18 47957 TBD
RO00589273036 18 13606 TBD

And the next iteration (using the "-02" file) would be this:

Pallet ID Quantity Product ID Loc. Code
RO00589700019 18 47957 TBD
RO00589296008 20 80076 TBD
RO00589900012 160 72673 TBD
RO00590341024 54 69793 TBD
RO00589928004 60 18412 TBD

And the third iteration (using the "-03" file) would be this:

Pallet ID Quantity Product ID Loc. Code
RO00590083013 128 70187 TBD
RO00590108003 24 71130 WF
RO00590108004 24 71130 WF
RO00589273037 18 13606 TBD
RO00589273038 18 13606 TBD

I appreciate any suggestions/code samples to use.

Thanks!
Chris70
Attachments
Scan Log 01-23-2020-03.csv
(685 Bytes) Downloaded 24 times
Scan Log 01-23-2020-02.csv
(546 Bytes) Downloaded 15 times
Scan Log 01-23-2020-01.csv
(377 Bytes) Downloaded 16 times
User avatar
AlphaBravo
Posts: 491
Joined: 29 Sep 2013, 22:59

Re: Constantly display the last five lines of an Excel or CSV file

24 Jan 2020, 17:26

give this a try:

Code: Select all

Gui, +AlwaysOnTop
Gui, font, S12 Consolas
Gui, add, edit, w500 r6 vLast5
Gui, show
SetTimer, ReadCSV, 5000 	; 5 seconds, adjust timer frequency as you wish
return

GuiClose:
ExitApp
return

ReadCSV:
FileRead, data, Scan Log 01-23-2020-03.csv
data := Trim(data, "`r`n")
line := StrSplit(data, "`n", "`r")
result := line[1]
. (line.MaxIndex()>5 ?  "`r`n" line[line.MaxIndex()-4] : "")
. (line.MaxIndex()>4 ?  "`r`n" line[line.MaxIndex()-3] : "")
. (line.MaxIndex()>3 ?  "`r`n" line[line.MaxIndex()-2] : "")
. (line.MaxIndex()>2 ?  "`r`n" line[line.MaxIndex()-1] : "")
. (line.MaxIndex()>1 ?  "`r`n" line[line.MaxIndex()]   : "")
GuiControl,, Last5, % result
return
User avatar
Chris70
Posts: 46
Joined: 17 Dec 2019, 12:25

Re: Constantly display the last five lines of an Excel or CSV file

24 Jan 2020, 17:33

AlphaBravo -

Thank you for the post. I will give it a try and let you know how it works out! I am getting ready to leave for the weekend, but I will check it out on Monday and circle back. I really appreciate the help.

Thanks!

Chris70
User avatar
Chris70
Posts: 46
Joined: 17 Dec 2019, 12:25

Re: Constantly display the last five lines of an Excel or CSV file

24 Jan 2020, 17:47

AlphaBravo -

I just checked this out and it works perfectly! :bravo: I am live testing it on the file and it is running great. This is going to help me immensely, thank you thank you thank you!!!! :dance:

Have a fantastic weekend!

Thanks!
Chris70
User avatar
flyingDman
Posts: 860
Joined: 29 Sep 2013, 19:01

Re: Constantly display the last five lines of an Excel or CSV file

24 Jan 2020, 18:24

or:

Code: Select all

fileread,oVar,Scan Log 01-23-2020-03.csv
for x,y in strsplit(oVar,"`n")
	if (x=1) OR (x>=strsplit(oVar,"`n").length()-5)
		list .= y "`n"
msgbox % list
This as a label of a timer. As an alternative to a gui, you can use Progress. Something like this:

Code: Select all

settimer, label1, 5000
goto label1
return

label1:
fileread,oVar,Scan Log 01-23-2020-03.csv
arr := strsplit(oVar,"`n"), count := arr.length()
list := ""
for x,y in arr
	if (x=1) OR (x>=count-5)
		list .= y "`n"
Progress, m2 b fm10 zh0 x750 y500 WM1 CWFFFFFF w400 h110, ,%list%, 
return

esc::
exitapp

Return to “Ask For Help”

Who is online

Users browsing this forum: boiler, swagfag and 50 guests