Skip to content

infinity.file

Provides file manipulation and examination functionality.

Module: infinity.fileSystem

Example:

infinity.loadModule('infinity.fileSystem');

let filename = 'test.txt';
infinity.file.writeString(filename, 'Hello World');

let string = infinity.file.readString(filename);
let size = infinity.file.size(filename);
let age = infinity.file.age(filename);

console.debug('String: "' + string + '"; Size: ' + size + ' bytes; Changed: ' + new Date(age));
//String: "Hello World"; Size: 11 bytes; Changed: 2021-03-17T16:51:09.604+01:00

infinity.file.remove(filename);
console.debug(infinity.file.exists(filename));
//false

Functions

age()

Returns the date of the last change of the specified file as a Unix timestamp in milliseconds (can be passed to new Date()). If the file does not exist, the current time is returned.

Signature:

age( fileName: string ): number

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file to examine.

Return type: number

Example:

infinity.loadModule('infinity.fileSystem');
let age = infinity.file.age('test.txt');

copy()

Creates a file with the source file's contents at the specified location. An existing destination file is overwritten. Returns false if the source file does not exist or the copy failed.

Signature:

copy( srcFileName: string, dstFileName: string ): boolean

Parameters:

  • srcFileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file which should be copied.

  • dstFileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file which should contain the copied contents.

Return type: boolean

Example:

infinity.loadModule('infinity.fileSystem');
if ( infinity.file.copy('mail.eml', 'test.txt') ) {
  //...
}

exists()

Returns whether the specified file exists.

Signature:

exists( fileName: string ): boolean

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file being checked for.

Return type: boolean

Example:

infinity.loadModule('infinity.fileSystem');
if ( infinity.file.exists('test.txt') ) {
  //...
}

getMode()

Returns the permission bits of the specified file (e.g. 493 = octal 0755). Returns -1 if the file does not exist. Under Windows, always returns -1.

Signature:

getMode( fileName: string ): number

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file to examine.

Return type: number

Example:

infinity.loadModule('infinity.fileSystem');
let mode = infinity.file.getMode('test.sh');
if ( mode >= 0 && (mode & 64) === 0 ) {
  //owner has no execute permission (64 = octal 0100)
}

move()

Moves the given file to the specified location. An existing destination file is overwritten. If the destination is on another file system, the file is copied and the source file is deleted. Returns false if the source file does not exist or the move failed.

Signature:

move( srcFileName: string, dstFileName: string ): boolean

Parameters:

  • srcFileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file which should be moved.

  • dstFileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the the new location of the file.

Return type: boolean

Example:

infinity.loadModule('infinity.fileSystem');
if ( infinity.file.move('test.txt', '../test.txt') ) {
  //...
}

read()

Reads the given file and returns its contents parsed as JSON (counterpart of write()). Returns undefined if the file is empty. Throws an error if the file cannot be opened.

Signature:

read( fileName: string, encoding?: infinity.encoding ): any

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file to be read.

  • encoding: infinity.encoding, optional

    The encoding of the file contents. Default: infinity.encoding.utf8.

Return type: any

Example:

infinity.loadModule('infinity.fileSystem');
infinity.file.write('test.json', {name: 'test', count: 3});
let data = infinity.file.read('test.json');
console.debug(data.count);
//3

readString()

Returns the contents of the given file as a string. A byte order mark (BOM) of the given encoding at the beginning of the file is skipped. Throws an error if the file cannot be opened. Files that report a size of 0 (e.g. most files under /proc on Linux) are read as an empty string.

Signature:

readString( fileName: string, encoding?: infinity.encoding ): string

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file to be read.

  • encoding: infinity.encoding, optional

    The encoding of the file contents. Default: infinity.encoding.utf8.

Return type: string

Example:

infinity.loadModule('infinity.fileSystem');
let string = infinity.file.readString('test.txt');

remove()

Deletes the specified file. Returns false if the file could not be deleted (e.g. because it does not exist).

Signature:

remove( fileName: string ): boolean

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file to be deleted.

Return type: boolean

Example:

infinity.loadModule('infinity.fileSystem');
if ( infinity.file.remove('test.txt') ) {
  //...
}

setMode()

Sets the permission bits of the specified file (like chmod), e.g. to make a file executable. Only the lower 12 bits of mode are used. Returns true on success. Under Windows nothing is changed; the function only returns whether the file exists.

Signature:

setMode( fileName: string, mode: number ): boolean

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file.

  • mode: number

    The permission bits to set, e.g. 493 (octal 0755).

Return type: boolean

Example:

infinity.loadModule('infinity.fileSystem');
if ( !infinity.file.setMode('bin/tool', parseInt('755', 8)) ) {
  //...
}

size()

Returns the size of the specified file in bytes. Returns 0 if the file does not exist or cannot be opened. Note that files of virtual file systems like /proc under Linux report a size of 0.

Signature:

size( fileName: string ): number

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file to be examined.

Return type: number

Example:

infinity.loadModule('infinity.fileSystem');
let size = infinity.file.size('test.txt');

write()

Serializes the given value as JSON and writes it to the specified file. The file can be read back with read(). An existing file is overwritten unless append is true. Throws an error if the file cannot be written.

Signature:

write( fileName: string, value: any, encoding?: infinity.encoding, append?: boolean, writeBom?: boolean ): void

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file, to which the data should be written.

  • value: any

    The value (object, array, string, number, boolean or null) to be serialized as JSON and written.

  • encoding: infinity.encoding, optional

    The target encoding. Default: infinity.encoding.utf8.

  • append: boolean, optional

    Whether to append the data to the end of the file. Default: false.

  • writeBom: boolean, optional

    Whether to write a Byte Order Mark (BOM) at the beginning of the file. Ignored when appending. Default: false.

Example:

infinity.loadModule('infinity.fileSystem');
infinity.file.write('test.json', {name: 'test', count: 3});

writeString()

Writes the given string to the specified file. An existing file is overwritten unless append is true. Throws an error if the file cannot be written.

Signature:

writeString( fileName: string, value: string, encoding?: infinity.encoding, append?: boolean, writeBom?: boolean ): void

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file, to which the string should be written.

  • value: string

    The string to be written.

  • encoding: infinity.encoding, optional

    The target encoding of the string. Default: infinity.encoding.utf8.

  • append: boolean, optional

    Whether to append the string to the end of the file. Default: false.

  • writeBom: boolean, optional

    Whether to write a Byte Order Mark (BOM) at the beginning of the file. Ignored when appending. Default: false.

Example:

infinity.loadModule('infinity.fileSystem');
infinity.file.writeString('test.txt', 'Hello World');