## File File class port of ruby For node js only, does not work inside a browser. * [File](#File) * [.delete(file_name)](#File.delete) → string * [.getAbsolutePath(file_name, dir_string)](#File.getAbsolutePath) → string * [.getAccessTime(file_name)](#File.getAccessTime) → Date * [.getBasename(file_name, suffix)](#File.getBasename) * [.getBirthTime(file_name)](#File.getBirthTime) → Date * [.getDirname(file_name)](#File.getDirname) → string * [.getSize(file_name)](#File.getSize) → number * [.expandPath(file_name, dir_string, options, expand_user_dir)](#File.expandPath) → string * [.isDirectory(file_name)](#File.isDirectory) → boolean * [.isEmpty(file_name)](#File.isEmpty) → boolean * [.isExisting(file_name)](#File.isExisting) → boolean * [.isFile(file_name)](#File.isFile) → boolean * [.normalizePath(path)](#File.normalizePath) → string * [.read(file_name, opt)](#File.read) → string * [.rename(file_name, new_path)](#File.rename) * [.readAsDataUri(file_name)](#File.readAsDataUri) → string * [.write(name, data, opt)](#File.write) → string * [.getHomePath()](#File.getHomePath) → string ### File.delete(file_name) → string Delete file synchronously | Param | Type | Description | | --- | --- | --- | | file_name | string | path to file | ### File.getAbsolutePath(file_name, dir_string) → string Converts a pathname to an absolute pathname '~' are not resolved. **Returns**: string - absolute pathname | Param | Type | Description | | --- | --- | --- | | file_name | string | path of the file to expand | | dir_string | string | optional starting point of the given path | ### File.getAccessTime(file_name) → Date Returns the last access time for the file as a Date object. | Param | Type | | --- | --- | | file_name | string | ### File.getBasename(file_name, suffix) Get the last component of the given file name | Param | Type | Description | | --- | --- | --- | | file_name | string | | | suffix | string | If suffix is given and present at the end of file_name, it is removed. If suffix is '.*', any extension will be removed. | **Example** ```js File.getBasename('/home/user/documents/letter.txt') // => 'letter.txt' ``` **Example** ```js File.getBasename('/home/user/documents/letter.txt','.txt') // => 'letter' ``` **Example** ```js File.getBasename('/home/user/documents/image.jpg','.*') // => 'image' ``` ### File.getBirthTime(file_name) → Date Returns the birth time for the file as a Date object. | Param | Type | | --- | --- | | file_name | string | ### File.getDirname(file_name) → string Get all components of the given file name except of the last one | Param | Type | | --- | --- | | file_name | string | **Example** ```js File.getDirname('/home/user/documents/letter.txt') // => '/home/user/documents' ``` **Example** ```js File.getDirname('/home/user/documents/some_file_without_extension') // => '/home/user/documents' ``` **Example** ```js File.getDirname('/home/user/documents/') // => '/home/user' ``` ### File.getSize(file_name) → number Get the size of the given file in bytes **Returns**: number - size in bytes | Param | Type | | --- | --- | | file_name | string | **Example** ```js File.getSize('myFile.txt'); // => 12345 ``` ### File.expandPath(file_name, dir_string, options, expand_user_dir) → string Converts a pathname to an absolute pathname '~' is resolved to the home directory, '~user' to the given users home directory. **Returns**: string - absolute pathname | Param | Type | Default | Description | | --- | --- | --- | --- | | file_name | string | | path of the file to expand | | dir_string | string | | optional starting point of the given path | | options | Object | | | | expand_user_dir | boolean | true | | ### File.isDirectory(file_name) → boolean Check if given file name exists and is a directory **Returns**: boolean - true if file exists and is a directory, otherwise false | Param | Type | Description | | --- | --- | --- | | file_name | string | path of the file to check | ### File.isEmpty(file_name) → boolean Check if given file exists but has no content **Returns**: boolean - true if file exists and has zero content, otherwise false | Param | Type | Description | | --- | --- | --- | | file_name | string | path of the file to check | ### File.isExisting(file_name) → boolean Check if given file name exists **Returns**: boolean - true if file exists, otherwise false | Param | Type | Description | | --- | --- | --- | | file_name | string | path of the file to check | ### File.isFile(file_name) → boolean Check if given file name exists and is a file **Returns**: boolean - true if file exists and is a file, otherwise false | Param | Type | Description | | --- | --- | --- | | file_name | string | path of the file to check | ### File.normalizePath(path) → string Normalize path and replace all back slashes to slashes and remove trailing slashes **Returns**: string - normalized path | Param | Type | | --- | --- | | path | string | ### File.read(file_name, opt) → string Read file and return its content synchronously | Param | Type | Default | Description | | --- | --- | --- | --- | | file_name | string | | path to file | | opt | Object | | | | opt.encoding | 'utf8' \| 'binary' \| 'buffer' \| 'base64' | 'utf8' | | | opt.length | Number | | | | opt.offset | Number | | | ### File.rename(file_name, new_path) Rename the given file | Param | Type | Description | | --- | --- | --- | | file_name | string | path to original file | | new_path | string | path to new file | ### File.readAsDataUri(file_name) → string Read a file and return as data URI that can be embedded on HTML for example **Returns**: string - base64 encoded data URI | Param | Type | Description | | --- | --- | --- | | file_name | string | path to file | ### File.write(name, data, opt) → string Write into file synchronously. | Param | Type | Default | Description | | --- | --- | --- | --- | | name | string | | path to file | | data | String \| Buffer \| TypedArray \| DataView \| Object | | data | | opt | Object | | | | opt.encoding | 'utf8' \| 'binary' \| 'buffer' | 'utf8' | | | opt.flag | 'rs+' \| 'ws' \| 'as' | 'ws' | | ### File.getHomePath() → string Get the absolute path of the current users home directory.