//递归找出最后的名字
async existFile(str: string, type: string, n: number) {
const joinStr: string = str + (n ? `(${n})` : '') + type;
const res: any = await fs.existsSync(joinStr);
if (res) {
return this.existFile(str, type, ++n);
} else {
return joinStr;
}
}
调用
let jsonLastName = await this.existFile(
fileName,
'.json',
0
);
这个博客内容涉及一个异步函数asyncexistFile,它用于递归查找以特定字符串开头、携带.json后缀的文件。函数通过拼接字符串和递增计数器来生成可能的文件路径,并使用fs.existsSync检查文件是否存在。如果存在,则继续递归;否则返回当前尝试的文件路径。
989

被折叠的 条评论
为什么被折叠?



