infinity.fileName¶
Provides helper functions for extraction of file paths, names and extensions.
Module: infinity.fileSystem
correct()¶
Returns a valid filename (without path) from the given string: characters not allowed in filenames are removed, slashes and backslashes are replaced with -. If the result is longer than 255 characters, only the last 255 characters are kept.
Signature:
correct( fileName: string ): string
Parameters:
- fileName:
stringThe filename to be corrected.
Return type: string
Example:
infinity.loadModule('infinity.fileSystem');
let validFileName = infinity.fileName.correct('Report 2021/03.pdf');
console.debug(validFileName);
//Report 2021-03.pdf
extension()¶
Returns the extension of the given filename including the leading dot, converted to lower case. Returns an empty string if the filename has no extension.
Signature:
extension( fileName: string ): string
Parameters:
- fileName:
stringThe filename to examine.
Return type: string
Example:
infinity.loadModule('infinity.fileSystem');
let s = infinity.fileName.extension('test.TXT');
console.debug(s);
//.txt
expand()¶
Returns the given relative file path converted to an absolute one (relative to the folder with the used INFINITY.JS executable file). .. and . path elements are resolved.
Signature:
expand( fileName: string ): string
Parameters:
- fileName:
stringA relative file path.
Return type: string
Example:
infinity.loadModule('infinity.fileSystem');
let s = infinity.fileName.expand('../config.ini');
console.debug(s);
//d:/dev/infinity/bin/config.ini
extract()¶
Strips path data and, optionally, the file extension leaving just the name of the file.
Signature:
extract( fileName: string, stripExtension?: boolean ): string
Parameters:
-
fileName:
stringThe data to examine.
-
stripExtension:
boolean, optionalWhether to strip the extension too. Default:
false.
Return type: string
Example:
infinity.loadModule('infinity.fileSystem');
let s = infinity.fileName.extract('c:/tmp/test.txt', true);
console.debug(s);
//test
extractDirectory()¶
Extracts the directory part (including the trailing /) from a file path string. A trailing / of the given path is ignored, so for a directory path the parent directory is returned.
Signature:
extractDirectory( fileName: string ): string
Parameters:
- fileName:
stringThe file path.
Return type: string
Example:
infinity.loadModule('infinity.fileSystem');
let s = infinity.fileName.extractDirectory('c:/tmp/test.txt');
console.debug(s);
//c:/tmp/
isValid()¶
Returns whether the given filename (without path) is valid for the operating system: it must not be empty, must not start or end with whitespace, must not end with a dot, must not contain characters that are invalid in filenames (including path delimiters) and must not be a reserved device name such as CON, NUL, PRN, AUX, COM1-COM9 or LPT1-LPT9 (with any extension; checked on all platforms).
Signature:
isValid( fileName: string ): boolean
Parameters:
- fileName:
stringThe filename to be checked.
Return type: boolean
Example:
infinity.loadModule('infinity.fileSystem');
if ( infinity.fileName.isValid(fileName) ) {
//...
}
quote()¶
Puts double quotes around file names containing spaces (double quotes inside the name are doubled). File names without spaces are returned unchanged.
Signature:
quote( fileName: string ): string
Parameters:
- fileName:
stringThe filename to be checked.
Return type: string
Example:
infinity.loadModule('infinity.fileSystem');
let path = infinity.fileName.quote('c:/tmp 2/test.txt');
console.debug(path);
//"c:/tmp 2/test.txt"