FileWatcher

Package: Libraries.System · Inherits from: Libraries.Language.Object

The FileWatcher class notices when files and folders change on disk, for example when another program saves a file. Give it a folder with Watch. Changes are kept until the program asks for them, so they never arrive in the middle of other work. There are three ways to get them. DispatchChanges sends each change to every FileChangeListener. GetChanges returns the changes without waiting. WaitForChanges waits for a change, up to a time limit. A Game can also call DispatchChanges by itself every frame, through Game:AddFileWatcher. Several quick changes to one file are merged into one change. For example, a file that is created and then written to is reported once, as CREATED. A file that is created and deleted again before it settles is not reported at all. See SetSettleTime. Changes can arrive late. On Mac computers they can take about 2 seconds, and watching many folders there costs more time, so skip the folders you do not need with ExcludeFolderName. Changes can also be lost when very many happen at once. Then an OVERFLOW event is sent, and the program should look at the folder again. If a watched folder itself is deleted, a DELETED event is sent for it, and it is no longer watched, even if it is made again. File watching works on Windows, Mac and Linux. It does not work on the web or on iOS. Use IsSupported to find out.

Example Code

use Libraries.System.File
use Libraries.System.FileWatcher
use Libraries.System.FileChangeEvent
use Libraries.Containers.Array

File folder
folder:SetAbsolutePath("/Users/me/Documents/Notes")

FileWatcher watcher
watcher:Watch(folder, true)

integer counter = 0
repeat while counter < 10
Array<FileChangeEvent> changes = watcher:WaitForChanges(5000)
integer i = 0
repeat while i < changes:GetSize()
    FileChangeEvent change = changes:Get(i)
    File changed = change:GetFile()
    output changed:GetAbsolutePath()
    i = i + 1
end
counter = counter + 1
end
watcher:Stop()

Actions

AddListener(Libraries.System.FileChangeListener listener)

Adds a listener. It is told about changes when DispatchChanges runs.

Parameters

DispatchChanges()

Sends every waiting change to every listener, then returns how many changes there were. It never waits. The listeners run on the thread that calls this action.

Returns

integer: the number of changes sent.

ExcludeFolderName(text name)

Skips every file and folder with this name, and everything inside such a folder, for example ".git" or "Build". Call it before Watch. A watched folder itself is never skipped.

Parameters

  • name text: the folder name to skip.

GetChanges()

Returns every waiting change. It never waits. Listeners are not told.

Returns

Libraries.Containers.Array<Libraries.System.FileChangeEvent>: the changes, maybe none.

GetSettleTime()

Returns the settle time. See SetSettleTime.

Returns

integer: the settle time in milliseconds.

GetWatchedFolders()

Returns the folders passed to Watch that are still watched.

Returns

Libraries.Containers.Array<Libraries.System.File>: a copy of the list of watched folders.

IgnoreChangesTo(Libraries.System.File file, integer milliseconds)

Ignores changes to one file for a short time. Use it just before a program writes the file itself. On Mac computers changes can arrive about 2 seconds late, so use 3000 or more there. A change made by another program during that time is lost too.

Parameters

IsSupported()

Returns true if file watching works on this platform.

Returns

boolean: true on Windows, Mac and Linux.

IsWatching()

Returns true if at least one folder is watched.

Returns

boolean: true if watching.

RemoveListener(Libraries.System.FileChangeListener listener)

Removes a listener.

Parameters

SetSettleTime(integer milliseconds)

Sets how long a file must stay unchanged before its change is reported. Several quick changes to one file become one event. The default is 200.

Parameters

  • milliseconds integer: the time to wait.

Stop()

Stops watching every folder and forgets changes not yet taken. Listeners, excluded folder names and the settle time are kept. The watcher can be used again with Watch. Calling Stop twice is safe.

Unwatch(Libraries.System.File folder)

Stops watching a folder that was passed to Watch. Changes from it that were not taken yet are dropped.

Parameters

WaitForChanges(integer milliseconds)

Waits until there is a change or the time is up, then returns every waiting change. Listeners are not told.

Parameters

  • milliseconds integer: the longest time to wait.

Returns

Libraries.Containers.Array<Libraries.System.FileChangeEvent>: the changes, maybe none.

Watch(Libraries.System.File folder, boolean recursive)

Starts watching a folder. If recursive is true, every folder inside it is watched too, including folders made later. Watching a folder that is already watched starts it again with the new choice. If the folder does not exist, a FileNotFoundError is raised. If file watching does not work on this platform, an Error is raised.

Parameters

  • folder Libraries.System.File: the folder to watch.
  • recursive boolean: true to also watch the folders inside it.

Watch(Libraries.System.File folder)

Starts watching a folder, but not the folders inside it.

Parameters

Inherited Actions

3 actions from Libraries.Language.Object