Follow

Watch a file grow and get each new part as it arrives: a log, another program's output, anything that appends.

FileService:Follow(path, offset?, interval?) gives one without waiting, a child of FileService. With offset nil it starts at the file's end, with 0 at its start, and it looks every interval seconds, 0.25 by default. Text hands over each new part once and in order; Reset says the file got shorter. A live Follow keeps the run going, and Destroy() ends it.

ShotInstancenot creatableFiles

FileService:Follow(path) gives one.

Example

Follow a growing logShotScript
-- Follows app.log from its start while this program adds lines to it, then ends the follow.
use FileService
use TimeService
FileService:WriteAsync("app.log", "")
local follow = FileService:Follow("app.log", 0, 0.1)
follow.Text:Connect(function(added : string)
	for _, line in string.split(added, "\n") do
		if line ~= "" then
			print("followed: " .. line)
		end
	end
end)
for _, line in { "started", "loaded Notes", "saved todo.txt" } do
	FileService:AppendAsync("app.log", line .. "\n")
	TimeService:WaitAsync(0.2)
end
-- Offset is how many bytes it has handed over: wait (at most 5 s) until it reaches the file's end
local size = FileService:SizeAsync("app.log") or 0
local deadline = TimeService:Clock() + 5
while follow.Offset < size and TimeService:Clock() < deadline do
	TimeService:WaitAsync(0.1)
end
print("Path " .. follow.Path .. ", Offset " .. follow.Offset .. ", a child of FileService: " .. tostring(follow.Parent == FileService))
follow:Destroy()
FileService:RemoveAsync("app.log")
What it printedOutput
followed: started
followed: loaded Notes
followed: saved todo.txt
Path app.log, Offset 36, a child of FileService: true

Properties

NameTypeDefaultDescription
Offsetnumber0
read-only
How many bytes have been handed over so far; without an offset it jumps to the file's end at the first look.
Pathstringread-onlyThe path given to FileService:Follow, kept as written, a relative path included.

Events

NamePassesDescription
Reset()Fires, passing nothing, when the file got shorter or was replaced; Offset goes back to 0 and following starts again from the start.
Text(string)Fires with the text added since the last look, once and in order; one part may hold several lines.
From ShotInstance 33