Can't figure out how fs.stat () works.

I am trying to write a function that tells me that a specific path is a directory.

var fs = require('fs'); console.log("+++++++++++++++++++++++++++++++++++++++"); fs.statSync(pathname, function(err, stats) { console.log(stats.isDirectory()); }); console.log("+++++++++++++++++++++++++++++++++++++++"); 

However, he never prints the answer.

If the path exists, it does not call the function. If it does not exist, it throws an exception: ENOENT not a file or directory . I do not want to know that this is the way, but I want to know if it is a directory.

Can someone help me fix this?

+8
stat
source share
2 answers

The synchronous version that does not use a callback is used. Instead, it simply returns the result. Therefore, either use the async fs.stat(path, callback) form, or use the synchronization form as follows:

 var fs = require('fs'); console.log("+++++++++++++++++++++++++++++++++++++++"); var stats = fs.statSync(pathname); console.log(stats.isDirectory()); console.log("+++++++++++++++++++++++++++++++++++++++"); 
+24
source share

How does fs.stat () work?

If you want to use the callback / async fs function, do not use the synchronous version, use fs.stat ():

 var fs = require('fs'); console.log("+++++++++++++++++++++++++++++++++++++++"); fs.stat(pathname, function(err, stats) { console.log(stats.isDirectory()); }); console.log("+++++++++++++++++++++++++++++++++++++++"); 

More information on fs.stat () . You can get a lot of information about the main object:

 fs.stat(path, function(err, stats) { console.log(stats) } 

Exit:

 { dev: 2049, ino: 305352, mode: 16877, nlink: 12, uid: 1000, gid: 1000, rdev: 0, size: 4096, blksize: 4096, blocks: 8, atime: '2009-06-29T11:11:55Z', mtime: '2009-06-29T11:11:40Z', ctime: '2009-06-29T11:11:40Z' } 

Many elements are often useless to us, yes. But here is the meaning of all these variables, in accordance with this article :

  • dev: identifier of the device containing the file
  • mode: file protection
  • nlink: number of hard links to a file
  • uid: user ID of the owner of the file.
  • gid: identifier of the group of file owners.
  • rdev: device identifier if the file is a special file.
  • blksize: block size for file system I / O.
  • ino: The inode file number. An indent is a file system data structure that is -
  • stores file information.
  • size: total file size in bytes.
  • blocks: the number of blocks allocated for the file.
  • atime: a date object representing the time the file was last accessed.
  • mtime: a date object representing the time the file was last modified.
  • ctime: a date object representing the last time the inode file was modified.

You can also, for example, the JS node documentation , get additional information, for example:

 stats.isFile() stats.isDirectory() stats.isBlockDevice() stats.isSymbolicLink() (only valid with fs.lstat()) stats.isCharacterDevice() stats.isFIFO() stats.isSocket() 

About stats.isSymbolicLink (), there is another function than fs.stat called fs.lstat (), and here is the difference between them:

  • stat follows symbolic links. When a path that is a symbolic link is given, it returns the target stat of the symbolic link.
  • lstat does not match symbolic links. When a path that is a symbolic link is given, it returns the stat of the symbolic link, not its purpose.
+1
source share

All Articles