Home > @microsoft/node-core-library > FileSystem

FileSystem class

The FileSystem API provides a complete set of recommended operations for interacting with the file system.

Signature:

export declare class FileSystem 

Remarks

We recommend to use this instead of the native fs API, because fs is a minimal set of low-level primitives that must be mapped for each supported operating system. The FileSystem API takes a philosophical approach of providing “one obvious way” to do each operation. We also prefer synchronous operations except in cases where there would be a clear performance benefit for using async, since synchronous code is much easier to read and debug. Also, indiscriminate parallelism has been seen to actually worsen performance, versus improving it.

Note that in the documentation, we refer to “filesystem objects”, this can be a file, folder, symbolic link, hard link, directory junction, etc.

Methods

Method Modifiers Description
appendToFile(filePath, contents, options) static Writes a text string to a file on disk, appending to the file if it already exists. Behind the scenes it uses fs.appendFileSync().
changePosixModeBits(path, mode) static Changes the permissions (i.e. file mode bits) for a filesystem object. Behind the scenes it uses fs.chmodSync().
copyFile(options) static Copies a file from one location to another. By default, destinationPath is overwritten if it already exists. Behind the scenes it uses fs.copyFileSync().
createHardLink(options) static Creates a hard link. Behind the scenes it uses fs.linkSync().
createSymbolicLinkFile(options) static Creates a symbolic link to a file (on Windows this requires elevated permissionsBits). Behind the scenes it uses fs.symlinkSync().
createSymbolicLinkFolder(options) static Creates a symbolic link to a folder (on Windows this requires elevated permissionsBits). Behind the scenes it uses fs.symlinkSync().
createSymbolicLinkJunction(options) static Creates a Windows “directory junction”. Behaves like createSymbolicLinkToFile() on other platforms. Behind the scenes it uses fs.symlinkSync().
deleteFile(filePath, options) static Deletes a file. Can optionally throw if the file doesn’t exist. Behind the scenes it uses fs.unlinkSync().
deleteFolder(folderPath) static Deletes a folder, including all of its contents. Behind the scenes is uses fs-extra.removeSync().
ensureEmptyFolder(folderPath) static Deletes the content of a folder, but not the folder itself. Also ensures the folder exists. Behind the scenes it uses fs-extra.emptyDirSync().
ensureFolder(folderPath) static Recursively creates a folder at a given path. Behind the scenes is uses fs-extra.ensureDirSync().
exists(path) static Returns true if the path exists on disk. Behind the scenes it uses fs.existsSync().
formatPosixModeBits(modeBits) static Returns a 10-character string representation of a PosixModeBits value similar to what would be displayed by a command such as “ls -l” on a POSIX-like operating system.
getLinkStatistics(path) static Gets the statistics of a filesystem object. Does NOT follow the link to its target. Behind the scenes it uses fs.lstatSync().
getPosixModeBits(path) static Retrieves the permissions (i.e. file mode bits) for a filesystem object. Behind the scenes it uses fs.chmodSync().
getRealPath(linkPath) static Follows a link to its destination and returns the absolute path to the final target of the link. Behind the scenes it uses fs.realpathSync().
getStatistics(path) static Gets the statistics for a particular filesystem object. If the path is a link, this function follows the link and returns statistics about the link target. Behind the scenes it uses fs.statSync().
move(options) static Moves a file. The folder must exist, unless the ensureFolderExists option is provided. Behind the scenes it uses fs-extra.moveSync()
readFile(filePath, options) static Reads the contents of a file into a string. Behind the scenes it uses fs.readFileSync().
readFileToBuffer(filePath) static Reads the contents of a file into a buffer. Behind the scenes is uses fs.readFileSync().
readFolder(folderPath, options) static Reads the contents of the folder, not including “.” or “..”. Behind the scenes it uses fs.readdirSync().
updateTimes(path, times) static Updates the accessed and modified timestamps of the filesystem object referenced by path. Behind the scenes it uses fs.utimesSync(). The caller should specify both times in the times parameter.
writeFile(filePath, contents, options) static Writes a text string to a file on disk, overwriting the file if it already exists. Behind the scenes it uses fs.writeFileSync().