Node.js fs.lstat() Method
The fs.lstat() method is similar to the fs.stat() method except that it is used to return information about the symbolic link that is being used to refer to a file or directory. The fs.Stat object returned has several fields and methods to get more details about the file.
Syntax:
fs.lstat( path, options, callback )
Parameters: This method accept three parameters as mentioned above and described below:
- path: It is a String, Buffer or URL that holds the path of the symbolic link.
- options: It is an object that can be used to specify optional parameters that will affect the output. It has one optional parameter:
- bigint: It is a boolean value which specifies if the numeric values returned in the fs.Stats object are bigint. The default value is false.
- callback: It is the function that would be called when the method is executed.
- err: It is an error that would be thrown if the method.
- Stats: It is the Stats object that contains the details of the file path.
Below examples illustrate the fs.lstat() method in Node.js:
Example 1: This example uses fs.lstat() method to get the details of a symbolic link to a file.
// Node.js program to demonstrate the // fs.lstat() method // Import the filesystem module const fs = require( 'fs' ); fs.symlinkSync(__dirname + "\\example_file.txt" , "symlinkToFile" , 'file' ); console.log( "Symlink to file created" ) fs.lstat( "symlinkToFile" , (err, stats) => { if (err) console.log(err); else { console.log( "Stat of symlinkToFile" ) console.log(stats); } }); |
Output:
Symlink to file created Stat of symlinkToFile Stats { dev: 3229478529, mode: 41398, nlink: 1, uid: 0, gid: 0, rdev: 0, blksize: 4096, ino: 281474976780939, size: 45, blocks: 0, atimeMs: 1585207132017.4473, mtimeMs: 1585207132017.4473, ctimeMs: 1585207132017.4473, birthtimeMs: 1585207132017.4473, atime: 2020-03-26T07:18:52.017Z, mtime: 2020-03-26T07:18:52.017Z, ctime: 2020-03-26T07:18:52.017Z, birthtime: 2020-03-26T07:18:52.017Z }
Example 2: This example uses fs.lstat() method to get the details of a symbolic link to a folder.
// Node.js program to demonstrate the // fs.lstat() method // Import the filesystem module const fs = require( 'fs' ); fs.symlinkSync(__dirname + "\\example_directory" , "symlinkToDir" , 'dir' ); console.log( "Symlink to directory created" ) fs.lstat( "symlinkToDir" , (err, stats) => { if (err) console.log(err); else { console.log( "Stat of symlinkToDir" ) console.log(stats); } }); |
Output:
Symlink to directory created Stat of symlinkToDir Stats { dev: 3229478529, mode: 41398, nlink: 1, uid: 0, gid: 0, rdev: 0, blksize: 4096, ino: 281474976780940, size: 46, blocks: 0, atimeMs: 1585207184224.7136, mtimeMs: 1585207184224.7136, ctimeMs: 1585207184224.7136, birthtimeMs: 1585207184224.7136, atime: 2020-03-26T07:19:44.225Z, mtime: 2020-03-26T07:19:44.225Z, ctime: 2020-03-26T07:19:44.225Z, birthtime: 2020-03-26T07:19:44.225Z }
Reference: https://nodejs.org/api/fs.html#fs_fs_lstat_path_options_callback
Please Login to comment...