It is currently March 29th, 2024, 12:54 pm

How to read the newest file in a directory

Get help with creating, editing & fixing problems with skins
DigitalEssence
Posts: 27
Joined: January 29th, 2016, 6:43 pm

How to read the newest file in a directory

Post by DigitalEssence »

Hi,

I've an application that writes to a log directory that I want to display the last line to the screen via Rainmeter.

I've got this working using a script provided on the forum by Jeffrey Morley (see below) and will later pattern match through it to display the bits of info I want.

The issue I have is that the application writing the log files will create a new log file when the current one gets too large but there doesn't seem to be any pattern to the size.

Is there a way of reading in the newest file and getting Rainmeter/lua to check periodically if there is a newer file and if so, open and read that in instead?

This may be better off on a lua forum but I thought I'd try here.

Thanks.

Code: Select all

[Rainmeter]
Author=Jeffrey Morley
Update=1000
DynamicWindowSize=1

[MeasureLuaScript]
Measure=Script
ScriptFile="#CURRENTPATH#LuaTextFile.lua"
FileToRead=#CURRENTPATH#Test.txt


[MeterDisplay]
Meter=String
MeasureName=MeasureLuaScript
W=1000
H=500
FontFace=Segoe UI
FontSize=11
FontColor=255,255,255,255
SolidColor=0,0,0,1
AntiAlias=1
ClipString=1
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: How to read the newest file in a directory

Post by balala »

DigitalEssence wrote:Is there a way of reading in the newest file and getting Rainmeter/lua to check periodically if there is a newer file and if so, open and read that in instead?
Yeah, I think for such things, you could use the FileView plugin, a lua script is not needed. Eg the following code will get the newest file of the specified folder, showing its name and details:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1

[Variables]
IconSize=Large

;----------------------------------------------------
; Styles
;----------------------------------------------------

[TextStyle]
FontColor=255,255,255,255
AntiAlias=1

[TextHighlight]
FontColor=150,150,255,255

[IconStyle]
X=5
Y=r
AntiAlias=1

[HighlightStyle]
SolidColor=0,0,0,1
X=5
Y=5R
W=380
H=([Index1Icon:H] > [Index1Info:H] ? [Index1Icon:H] : [Index1Info:H])
DynamicVariables=1
MouseOverAction=[!SetOption #CURRENTSECTION# SolidColor "50,50,255,150"][!UpdateMeter #CURRENTSECTION#][!Redraw]
MouseLeaveAction=[!SetOption #CURRENTSECTION# SolidColor ""][!UpdateMeter #CURRENTSECTION#][!Redraw]

[InfoStyle]
X=5R
Y=r
Text="%1 #CRLF#%2 #CRLF#%3 "
AutoScale=1
AntiAlias=1

;----------------------------------------------------
; Measures
;----------------------------------------------------

[mPath]
Measure=Plugin
Plugin=FileView
Path= ADD HERE THE NEEDED PATH
Count=1
SortType=Date
SortDateType=Created
SortAscending=0
ShowDotDot=0

[mFolderCount]
Measure=Plugin
Plugin=FileView
Path=[mPath]
Type=FolderCount
Group=Children

[mFileCount]
Measure=Plugin
Plugin=FileView
Path=[mPath]
Type=FileCount
Group=Children

[mFolderSize]
Measure=Plugin
Plugin=FileView
Path=[mPath]
Type=FolderSize
Group=Children

;----------------------------------------------------
; Index 1

[mIndex1Name]
Measure=Plugin
Plugin=FileView
Path=[mPath]
Type=FileName
Index=1
Group=Children

[mIndex1Size]
Measure=Plugin
Plugin=FileView
Path=[mPath]
Type=FileSize
Index=1
Group=Children

[mIndex1Date]
Measure=Plugin
Plugin=FileView
Path=[mPath]
Type=FileDate
DateType=Created
Index=1
Group=Children

[mIndex1Icon]
Measure=Plugin
Plugin=FileView
Path=[mPath]
Type=Icon
IconSize=#IconSize#
Index=1
Group=Children

;----------------------------------------------------
; Meters
;----------------------------------------------------

[Index1]
Meter=Image
MeterStyle=HighlightStyle
LeftMouseDoubleClickAction=[!CommandMeasure mIndex1Name "FollowPath"][!UpdateMeasure mPath][!UpdateMeasureGroup Children][!UpdateMeter *][!Redraw]

[Index1Icon]
Meter=Image
MeasureName=mIndex1Icon
MeterStyle=IconStyle

[Index1Info]
Meter=String
MeasureName=mIndex1Name
MeasureName2=mIndex1Size
MeasureName3=mIndex1Date
MeterStyle=TextStyle | InfoStyle
The most important options are the followings:

Code: Select all

[mPath]
...
Count=1
SortType=Date
SortDateType=Created
SortAscending=0
ShowDotDot=0
The SortType option ensures that the files will be sorted by date. The SortDateType ensures that the creation date will be used, not the access or modification date. The SortAscending option tells the plugin to sort the files descending (take care that the default option here is the ascending sorting, in which case the code will show not the newest, but the oldest file). Finally the ShowDotDot options will ensure that the parent folder isn't taken into account.
Now the DateType=Created option of the [mIndex1Date] measure, tells this measure to show the creation date of the file, instead of any other date.
And finally, the [mPath]\[mIndex1Name] expression will return the path and name of the newest file of folder. Use this expression onto the appropriate measure or into the lua script.
DigitalEssence
Posts: 27
Joined: January 29th, 2016, 6:43 pm

Re: How to read the newest file in a directory

Post by DigitalEssence »

balala wrote:Yeah, I think for such things, you could use the FileView plugin, a lua script is not needed.
Brilliant, thank you. I will have a read over this and roll my sleeves up.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: How to read the newest file in a directory

Post by balala »

DigitalEssence wrote:Brilliant, thank you. I will have a read over this and roll my sleeves up.
Please let me know if you succeeded.