以下是一个使用Node.js的解决方案,可以遍历子文件夹中的所有HTML文件并使用脚本命令行工具进行迭代运行:
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const rootFolder = 'path/to/root/folder';
function traverseFolder(folderPath) {
// 读取文件夹中的所有文件和子文件夹
const files = fs.readdirSync(folderPath);
files.forEach(file => {
const filePath = path.join(folderPath, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
// 如果是子文件夹,则递归遍历
traverseFolder(filePath);
} else if (path.extname(filePath) === '.html') {
// 如果是HTML文件,则执行命令行工具
try {
execSync(`your-command-line-tool ${filePath}`);
console.log(`Executed ${filePath} successfully.`);
} catch (error) {
console.error(`Error executing ${filePath}: ${error}`);
}
}
});
}
// 开始遍历根文件夹
traverseFolder(rootFolder);
请将'path/to/root/folder'
替换为实际的根文件夹路径,并将'your-command-line-tool'
替换为实际的命令行工具名称或路径。该代码会递归遍历根文件夹中的所有子文件夹,并对每个HTML文件执行命令行工具。成功执行的文件将显示成功消息,而执行失败的文件将显示错误消息。