Need help creating a script to show the contents of the five newest files in a folder

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

Need help creating a script to show the contents of the five newest files in a folder

05 Mar 2020, 17:06

Hello AHKers,

I am still very new to using AHK and I have a script I want to modify that I need help with. I have a script that I am using to show the contents of a .csv file in a GUI window. It currently reads the newest .csv file in the referenced folder location. It displays the header row and the last five rows of the file which works perfect for the application I developed it for. However, I am now needing to have a display window that will read the five newest .csv files in a specified folder and display their contents similar to the script I have here (except instead of the last five lines in one file, I want the contents of the last five files to show sequentially in the window). I have two thoughts on how to do this, but I am not sure how to accomplish either. Any help would be greatly appreciated!

Option 1: Have the script determine the five newest files in the folder and display their contents in the GUI with the GUI window fixed in size like the current one and it will just scroll through the contents of each file in order (oldest on top, newest on bottom), with the screen updating every 5s.

Option 2: Create a separate .csv file (in a different folder) that contains the appended contents of the five newest .csv files, then use the script I already have to read that file except have it show all the lines in the file and not just the last five lines (need to know how to do that as well).

Here is the script I have to start with:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Persistent
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force

Newest := 0
NewestFile := ""

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

GuiClose:
ExitApp
return

ReadCSV:
Folder = C:\My Documents ; target folder for file
Loop, %Folder%\Scan Log*.csv
{
	if (A_LoopFileTimeCreated > Newest) { ;if this file is newer continue
		Newest := A_LoopFileTimeCreated ;reset variable to higher value
		NewestFile := A_LoopFileLongPath ;reset varaible to newest file
	}
}
CopyToFile  := NewestFile
FileRead, data, %CopyToFile%  
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
Any other suggestions on other options would be appreciated as well.

Thanks!
Chris70
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Need help creating a script to show the contents of the five newest files in a folder

06 Mar 2020, 09:43

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Persistent
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force

Newest := 0
NewestFile := ""

Gui, +AlwaysOnTop
Gui, font, S10 Arial Bold
Gui, add, edit, w500 r20 vLast5
Gui, show
gosub, LoadFiles
SetTimer, LoadFiles, 5000
return

GuiClose:
ExitApp
return

LoadFiles:
Last5FilesContents := "", str := ""
Loop, Files, C:\My Documents\Scan Log*.csv
   str .= A_LoopFileTimeModified  "`t" A_LoopFileFullPath "`n" 
str := RTrim(str,"`n")
sort, str
for k, v in arr:=StrSplit(str,"`n","`r") {   
   if ( k > arr.Maxindex() - 5) {
      fileName := StrSplit(v,"`t")[2]
      FileRead, Allfile, % fileName
      Last5FilesContents .= Allfile "`n" 
   }
}
GuiControl,, Last5, % Last5FilesContents
FileDelete, \SomeOtherFolder\Last5Files.txt
FileAppend, % Last5FilesContents, \SomeOtherFolder\Last5Files.txt
return

ExitApp
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
User avatar
Chris70
Posts: 46
Joined: 17 Dec 2019, 12:25

Re: Need help creating a script to show the contents of the five newest files in a folder

06 Mar 2020, 10:27

@Odlanir -

Thank you so much for the help. I tested what you provided and it works perfectly! My only question is, I am trying to follow the subroutine where you are creating the .txt file and appending the data from each batch file, which line in your code adds the extra line space between the data files' data list? I know it's one of the "''n" entries, but I did not want to remove the wrong one if I want this space deleted.

Code: Select all

LoadFiles:
Last5FilesContents := "", str := ""
Loop, Files, C:\My Documents\Scan Log*.csv
   str .= A_LoopFileTimeModified  "`t" A_LoopFileFullPath "`n" 
str := RTrim(str,"`n")
sort, str
for k, v in arr:=StrSplit(str,"`n","`r") {   
   if ( k > arr.Maxindex() - 5) {
      fileName := StrSplit(v,"`t")[2]
      FileRead, Allfile, % fileName
      Last5FilesContents .= Allfile "`n" 
   }

I have attached a screen shot for reference to what I am asking about. Again, thank you.

Chris70
Attachments
Last 5 Batch Files - TEST screenshot 03-06-2020.JPG
Last 5 Batch Files - TEST screenshot 03-06-2020.JPG (18.73 KiB) Viewed 215 times
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Need help creating a script to show the contents of the five newest files in a folder

06 Mar 2020, 10:38

Seems like your csv files ends up with a line with only an extra carriage return.
If so you can avoid adding the newline in the FileAppend:

Code: Select all

Last5FilesContents .= Allfile "`n" ; replace this ....
;
Last5FilesContents .= Allfile ; ... with this
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Chunjee, Delta Pythagorean, kczx3, Lem2001, mikeyww, pgarza, XMCQCX and 44 guests