← All examples
Filesystem
File Watching
Filesystem change notifications
Starts a FileWatcher on a directory and prints every FileEvent (created/modified/removed) it receives, cancelling itself on the first event — a minimal example of ASGE's cross-platform filesystem-watching API.
console example — no window, see captured output below
Sample output
file_watching output
[INFO][20:36:21.453][FileWatcher] New File Watcher Created
[INFO][20:36:21.454][AddWatch] Added new watch for "C:\\Users\\ricca\\Desktop\\dev\\asge\\examples\\file_watching\\files" with mask Created, Deleted, Modified, Moved
FileEvent [
Timestamp: 1789497382466ms
Event: Created
Path: C:\Users\ricca\Desktop\dev\asge\examples\file_watching\files\trigger.txt
Type: File (2)
Size: 0 bytes
Raw OS ID: 0x1
]
Run it yourself
build just this example
cmake --build --preset windows-debug --target file_watching
# binary lands under bin/ (e.g. bin/Debug/file_watching.exe on Windows)
See Installation for the full build setup.
Source
examples/file_watching/main.cpp
#include <iostream>
#include <ASGE/Core/Filesystem/FileWatcher.hpp>
using namespace asge::filesystem;
int main()
{
FileWatcher fw;
fw.Start();
auto result = fw.AddWatch(
#ifdef _WIN32
"C:\\Users\\ricca\\Desktop\\dev\\asge\\examples\\file_watching\\files",
#else
"/home/ricca/personal/a-simple-game-engine/examples/file_watching/files",
#endif
[&fw]( FileEvent const& inEvent )
{
std::cout << inEvent << std::endl;
fw.Cancel();
}
);
if (!result)
{
auto const err = result.Error();
LOG_ERROR("Error adding watcher: ", err);
fw.Cancel();
fw.Join();
return 1;
}
fw.Join();
return 0;
}