Filesystem Module
const fs = require('fs')
The implementation tries to follow the CommonJSFilesystem/A/0specification where possible.
Working Directory
The directory functions below shouldn’t use the current working directory of the server like .
or ./test
. You will not be able to tell whether the environment the server is running in will permit directory listing, reading or writing of files.
You should either base your directories with getTempPath()
, or as a foxx service use themodule.context.basePath.
Single File Directory Manipulation
exists
checks if a file of any type or directory existsfs.exists(path)
Returns true if a file (of any type) or a directory exists at a givenpath. If the file is a broken symbolic link, returns false.
isFile
tests if path is a filefs.isFile(path)
Returns true if the path points to a file.
isDirectory
tests if path is a directoryfs.isDirectory(path)
Returns true if the path points to a directory.
size
gets the size of a filefs.size(path)
Returns the size of the file specified by path.
mtime
gets the last modification time of a filefs.mtime(filename)
Returns the last modification date of the specified file. The date isreturned as a Unix timestamp (number of seconds elapsed since January 1 1970).
pathSeparator
fs.pathSeparator
If you want to combine two paths you can use fs.pathSeparator instead of /
or \
.
join
fs.join(path, filename)
The function returns the combination of the path and filename, e.g.fs.join('folder', 'file.ext')
would return folder/file.ext
.
getTempFile
returns the name for a (new) temporary filefs.getTempFile(directory, createFile)
Returns the name for a new temporary file in directory directory.If createFile is true, an empty file will be created so no otherprocess can create a file of the same name.
Note: The directory directory must exist.
getTempPath
returns the temporary directoryfs.getTempPath()
Returns the absolute path of the temporary directory
makeAbsolute
makes a given path absolutefs.makeAbsolute(path)
Returns the given string if it is an absolute path, otherwise anabsolute path to the same location is returned.
chmod
sets file permissions of specified files (non windows only)fs.exists(path)
Returns true on success.
list
returns the directory listingfs.list(path)
The functions returns the names of all the files in a directory, inlexically sorted order. Throws an exception if the directory cannot betraversed (or path is not a directory).
Note: this means that list(“x”) of a directory containing “a” and “b” wouldreturn [“a”, “b”], not [“x/a”, “x/b”].
listTree
returns the directory treefs.listTree(path)
The function returns an array that starts with the given path, and all ofthe paths relative to the given path, discovered by a depth first traversalof every directory in any visited directory, reporting but not traversingsymbolic links to directories. The first path is always ""
, the pathrelative to itself.
makeDirectory
creates a directoryfs.makeDirectory(path)
Creates the directory specified by path.
makeDirectoryRecursive
creates a directoryfs.makeDirectoryRecursive(path)
Creates the directory hierarchy specified by path.
remove
removes a filefs.remove(filename)
Removes the file filename at the given path. Throws an exception if thepath corresponds to anything that is not a file or a symbolic link. If“path” refers to a symbolic link, removes the symbolic link.
removeDirectory
removes an empty directoryfs.removeDirectory(path)
Removes a directory if it is empty. Throws an exception if the path is notan empty directory.
removeDirectoryRecursive
removes a directoryfs.removeDirectoryRecursive(path)
Removes a directory with all subelements. Throws an exception if the pathis not a directory.
File IO
read
reads in a filefs.read(filename)
Reads in a file and returns the content as string. Please note that thefile content must be encoded in UTF-8.
read64
reads in a file as base64fs.read64(filename)
Reads in a file and returns the content as string. The file content isBase64 encoded.
readBuffer
reads in a filefs.readBuffer(filename)
Reads in a file and returns its content in a Buffer object.
readFileSync
fs.readFileSync(filename, encoding)
Reads the contents of the file specified in filename
. If encoding
is specified,the file contents will be returned as a string. Supported encodings are:
utf8
orutf-8
ascii
base64
ucs2
orucs-2
utf16le
orutf16be
hex
If noencoding
is specified, the file contents will be returned in a Bufferobject.
write
fs.write(filename, content)
Writes the content into a file. Content can be a string or a Bufferobject. If the file already exists, it is truncated.
writeFileSync
fs.writeFileSync(filename, content)
This is an alias for fs.write(filename, content)
.
append
fs.append(filename, content)
Writes the content into a file. Content can be a string or a Bufferobject. If the file already exists, the content is appended at theend.
Recursive Manipulation
copyRecursive
copies a directory structurefs.copyRecursive(source, destination)
Copies source to destination.Exceptions will be thrown on:
- Failure to copy the file
- specifying a directory for destination when source is a file
- specifying a directory as source and destination
CopyFile
copies a file into a target filefs.copyFile(source, destination)
Copies source to destination. If Destination is a directory, a fileof the same name will be created in that directory, else the copy will getthespecified filename.
move
renames a filefs.move(source, destination)
Moves source to destination. Failure to move the file, orspecifying a directory for destination when source is a file will throw anexception. Likewise, specifying a directory as source and destination willfail.
ZIP
unzipFile
unzips a filefs.unzipFile(filename, outpath, skipPaths, overwrite, password)
Unzips the zip file specified by filename into the path specified byoutpath. Overwrites any existing target files if overwrite is setto true.
Returns true if the file was unzipped successfully.
zipFile
zips a filefs.zipFile(filename, chdir, files, password)
Stores the files specified by files in the zip file filename. Ifthe file filename already exists, an error is thrown. The list of inputfiles files must be given as a list of absolute filenames. If chdir isnot empty, the chdir prefix will be stripped from the filename in thezip file, so when it is unzipped filenames will be relative.Specifying a password is optional.
Returns true if the file was zipped successfully.