我似乎无法获得任何解释如何执行此操作的搜索结果。
我想要做的就是能够知道给定路径是文件还是目录(文件夹)。
scandir()
返回的 Dirent
和 stat()
返回的 Stat
都具有 isFile()
和 isDirectory()
方法,但前者不遵循符号链接,而后者则遵循。
下面应该告诉你。从 docs:
fs.lstatSync(path_string).isDirectory()
从 fs.stat() 和 fs.lstat() 返回的对象属于这种类型。 stats.isFile() stats.isDirectory() stats.isBlockDevice() stats.isCharacterDevice() stats.isSymbolicLink() // (只对fs.lstat()有效) stats.isFIFO() stats.isSocket()
笔记:
上述解决方案将throw
一个Error
如果;例如,file
或 directory
不存在。
如果您想要 true
或 false
方法,请尝试 Joseph 在下面的评论中提到的 fs.existsSync(dirPath) && fs.lstatSync(dirPath).isDirectory();
。
更新:Node.Js >= 10
我们可以使用新的 fs.promises API
const fs = require('fs').promises;
(async() => {
const stat = await fs.lstat('test.txt');
console.log(stat.isFile());
})().catch(console.error)
任何 Node.Js 版本
以下是检测路径是异步的文件还是目录的方法,这是 node.js 中推荐的方法。使用 fs.lstat
const fs = require("fs");
let path = "/path/to/something";
fs.lstat(path, (err, stats) => {
if(err)
return console.log(err); //Handle error
console.log(`Is file: ${stats.isFile()}`);
console.log(`Is directory: ${stats.isDirectory()}`);
console.log(`Is symbolic link: ${stats.isSymbolicLink()}`);
console.log(`Is FIFO: ${stats.isFIFO()}`);
console.log(`Is socket: ${stats.isSocket()}`);
console.log(`Is character device: ${stats.isCharacterDevice()}`);
console.log(`Is block device: ${stats.isBlockDevice()}`);
});
使用同步 API 时注意:
使用同步形式时,会立即抛出任何异常。您可以使用 try/catch 来处理异常或允许它们冒泡。
try{
fs.lstatSync("/some/path").isDirectory()
}catch(e){
// Handle error
if(e.code == 'ENOENT'){
//no such file or directory
//do something
}else {
//do something else
}
}
说真的,问题存在五年而没有漂亮的外观?
function isDir(path) {
try {
var stat = fs.lstatSync(path);
return stat.isDirectory();
} catch (e) {
// lstatSync throws an error if path doesn't exist
return false;
}
}
[Error: EACCES: permission denied, scandir '/tmp/snap.skype']
当我提供 /tmp/
时,它是一个目录且可访问。
根据您的需要,您可能可以依赖节点的 path
模块。
您可能无法访问文件系统(例如,尚未创建文件)并且您可能希望避免访问文件系统,除非您确实需要额外的验证。如果您可以假设要检查的内容遵循 .<extname>
格式,只需查看名称即可。
显然,如果您正在寻找一个没有 extname 的文件,您需要点击文件系统来确定。但保持简单,直到你需要更复杂。
const path = require('path');
function isFile(pathItem) {
return !!path.extname(pathItem);
}
folder.txt
,这表示它是一个文件,或者该文件可以是 LICENSE
,没有扩展
如果您在遍历目录时需要它1
从 Node 10.10+ 开始,fs.readdir
具有 withFileTypes
选项,使其返回目录条目 fs.Dirent
而不仅仅是文件名。目录条目包含其 name
和有用的方法,例如 isDirectory
或 isFile
,因此您无需显式调用 fs.lstat
!
你可以像这样使用它:
import { promises as fs } from 'fs';
// ./my-dir has two subdirectories: dir-a, and dir-b
const dirEntries = await fs.readdir('./my-dir', { withFileTypes: true });
// let's filter all directories in ./my-dir
const onlyDirs = dirEntries.filter(de => de.isDirectory()).map(de => de.name);
// onlyDirs is now [ 'dir-a', 'dir-b' ]
1)因为这就是我发现这个问题的方式。
这是我使用的一个功能。没有人在这篇文章中使用 promisify
和 await/async
功能,所以我想我会分享。
const promisify = require('util').promisify;
const lstat = promisify(require('fs').lstat);
async function isDirectory (path) {
try {
return (await lstat(path)).isDirectory();
}
catch (e) {
return false;
}
}
注意:我不使用require('fs').promises;
,因为它已经实验了一年,最好不要依赖它。
上面的答案检查文件系统是否包含文件或目录的路径。但它并不能确定给定路径本身是文件还是目录。
答案是使用“/”来识别基于目录的路径。比如 --> "/c/dos/run/." <-- 尾随期间。
就像尚未写入的目录或文件的路径。或者来自不同计算机的路径。或同时存在同名文件和目录的路径。
// /tmp/
// |- dozen.path
// |- dozen.path/.
// |- eggs.txt
//
// "/tmp/dozen.path" !== "/tmp/dozen.path/"
//
// Very few fs allow this. But still. Don't trust the filesystem alone!
// Converts the non-standard "path-ends-in-slash" to the standard "path-is-identified-by current "." or previous ".." directory symbol.
function tryGetPath(pathItem) {
const isPosix = pathItem.includes("/");
if ((isPosix && pathItem.endsWith("/")) ||
(!isPosix && pathItem.endsWith("\\"))) {
pathItem = pathItem + ".";
}
return pathItem;
}
// If a path ends with a current directory identifier, it is a path! /c/dos/run/. and c:\dos\run\.
function isDirectory(pathItem) {
const isPosix = pathItem.includes("/");
if (pathItem === "." || pathItem ==- "..") {
pathItem = (isPosix ? "./" : ".\\") + pathItem;
}
return (isPosix ? pathItem.endsWith("/.") || pathItem.endsWith("/..") : pathItem.endsWith("\\.") || pathItem.endsWith("\\.."));
}
// If a path is not a directory, and it isn't empty, it must be a file
function isFile(pathItem) {
if (pathItem === "") {
return false;
}
return !isDirectory(pathItem);
}
节点版本:v11.10.0 - 2019 年 2 月
最后的想法:为什么还要访问文件系统?
.git
甚至 myFolder.txt
,该怎么办?
我可以使用以下方法检查目录或文件是否存在:
// This returns if the file is not a directory.
if(fs.lstatSync(dir).isDirectory() == false) return;
// This returns if the folder is not a file.
if(fs.lstatSync(dir).isFile() == false) return;
不定期副业成功案例分享
let isDirExists = fs.existsSync(dirPath) && fs.lstatSync(dirPath).isDirectory();
fs.lstat()
?文档说它始终是false
:“如果<fs.Stats>
对象是从fs.lstat()
获得的,则此方法 [<fs.Stats>.isDirectory()
] 将始终返回false
。这是因为fs.lstat()
返回有关符号的信息链接本身,而不是它解析到的路径。”