const path = require('path');
// 检查路径是否有中文名的文件夹或文件
async function findChineseNames(dirPath) {
return new Promise((resolve, reject) => {
fs.readdir(dirPath, (err, files) => {
if (err) {
reject(`读取目录出错: ${err}`);
return;
}
const chineseRegex = /[\u4e00-\u9fa5]/; // 中文字符的正则表达式
let chineseNames = []; // 用于存储中文名的文件和文件夹
console.log(`开始检查目录: ${dirPath}`);
// 遍历文件和文件夹
const promises = files.map(file => {
const fullPath = path.join(dirPath, file);
return new Promise((res) => {
fs.stat(fullPath, (err, stat) => {
if (err) {
console.error(`获取文件状态出错: ${err}`);
res(); // 继续处理其他文件
} else {
// 检查文件或文件夹名是否包含中文
if (chineseRegex.test(file)) {
chineseNames.push(fullPath); // 记录中文名
console.log(`发现中文名: ${fullPath}`);
}
if (stat.isDirectory()) {
console.log(`检查文件夹: ${file}`);
// 递归检查子目录
findChineseNames(fullPath).then(subChineseNames => {
chineseNames = chineseNames.concat(subChineseNames);
res();
});
} else {
console.log(`检查文件: ${file}`);
res();
}
}
});
});
});
// 等待所有文件检查完成
Promise.all(promises).then(() => resolve(chineseNames));
});
});
}
// 主函数
async function main() {
const dirPath = path.resolve(__dirname, 'webcode'); // 使用绝对路径
// 检查目录是否存在
fs.access(dirPath, fs.constants.F_OK, async (err) => {
if (err) {
console.error(`目录 "${dirPath}" 不存在,正在创建...`);
fs.mkdir(dirPath, { recursive: true }, (err) => {
if (err) {
console.error('创建目录失败:', err);
} else {
console.log('目录创建成功:', dirPath);
}
});
} else {
console.log(`目录 "${dirPath}" 已存在`);
// 检查是否包含中文名的文件或文件夹
const chineseNames = await findChineseNames(dirPath);
if (chineseNames.length > 0) {
console.log('总结: 发现以下中文名的文件或文件夹:');
chineseNames.forEach(name => console.log(name));
} else {
console.log('总结: 所有文件和文件夹均不含中文名');
}
}
});
}
main().catch(err => console.error(err));