files/en-us/web/api/filesystemchangerecord/index.md
{{APIRef("File System API")}}
The FileSystemChangeRecord dictionary of the {{domxref("File System API", "File System API", "", "nocode")}} contains details of a single change observed by a {{domxref("FileSystemObserver")}}.
The records argument passed into the {{domxref("FileSystemObserver.FileSystemObserver", "FileSystemObserver()")}} constructor's callback function is an array of FileSystemChangeRecord objects.
changedHandle
: A reference to the file system handle that the change was observed on.
This property will be null for records with a "disappeared", "errored", or "unknown" type.
relativePathComponents
root to the changedHandle, including the changedHandle filename.relativePathMovedFrom
root to the changedHandle's former location, in the case of observations with a "moved" type. If the type is not "moved", this property will be null.root
observe() call that started the observation. Again, this can be a {{domxref("FileSystemFileHandle")}}, {{domxref("FileSystemDirectoryHandle")}}, or {{domxref("FileSystemSyncAccessHandle")}}.type
appeared
root file structure.disappeared
root file structure. To find out which file or directory disappeared, you can query the relativePathComponents property.errored
root of the observation) is deleted or moved. In this case, a "disappeared" observation will be recorded, followed by an "errored" observation. In such cases, you may wish to stop observing the file system using {{domxref("FileSystemObserver.disconnect()")}}.modified
moved
[!NOTE] On Windows,
"moved"observations aren't supported between directories. They are reported as a"disappeared"observation in the source directory and an"appeared"observation in the destination directory.
unknown
Depending on the operating system, not all observations will be reported with the same level of detail, for example, when the contents of a directory change recursively. At best, the website will receive a detailed change record containing the type of change and a handle to the affected path. At worst, the website will receive a more generic change record (that is, an "unknown" type) that still requires it to enumerate the directory to figure out which handle changed.
This is still an improvement over polling, since the directory enumeration can be kicked off on-demand from the callback function, rather than needing to poll for changes periodically.
FileSystemObserverBefore you can start observing file or directory changes, you need to initialize a FileSystemObserver to handle the observations. This is done using the {{domxref("FileSystemObserver.FileSystemObserver", "FileSystemObserver()")}} constructor, which takes a callback function as an argument:
const observer = new FileSystemObserver(callback);
The callback function body can be specified to return and process file change observations in any way you want. Each object inside the records array is a FileSystemChangeRecord object:
const callback = (records, observer) => {
for (const record of records) {
console.log("Change detected:", record);
const reportContent = `Change observed to ${record.changedHandle.kind} ${record.changedHandle.name}. Type: ${record.type}.`;
sendReport(reportContent); // Some kind of user-defined reporting function
}
observer.disconnect();
};
Not currently part of a specification. See https://github.com/whatwg/fs/pull/165 for the relevant specification PR.