//递归找出最后的名字
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
);