Home  >  Article  >  Web Front-end  >  How to determine whether it is a file or a folder in node

How to determine whether it is a file or a folder in node

青灯夜游
青灯夜游Original
2021-12-13 11:34:037492browse

In node, you can use the isFile() and isDirectory() methods to determine whether it is a file or a folder. The isFile() method can detect whether it is a regular file and returns "true" if it is; the isDirectory() method can detect whether it is a folder and returns "true" if it is.

How to determine whether it is a file or a folder in node

The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, DELL G3 computer.

node determines whether the file or folder is opened

var fs = require("fs");
//判断打开的是文件 还是 文件夹
var path = "wen.txt";
fs.stat(path,function(err,stat){
	if (err) {
		console.error(err);
		throw err;
	}
	console.log(stat);
	console.info(path+"是一个"+stat.isFile());
	console.info(path+"是一个"+stat.isDirectory());
});
console.info("打开文件成功··");

How to determine whether it is a file or a folder in node

Instructions:

isFile( )

  • ##Added in: v0.1.10

  • Returns:

  • If it is a regular file, return true.

isDirectory()

  • New in: v10.10.0

  • Return:

  • If it is a file system directory, return true.

For more node-related knowledge, please visit:

nodejs tutorial! !

The above is the detailed content of How to determine whether it is a file or a folder in node. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn