1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| const fs = require('fs'); const xml2js = require('xml2js'); const path = require('path');
function findFilesInDirectory (dir, files = []) { const entries = fs.readdirSync(dir, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.join(dir, entry.name); if (entry.isDirectory()) { findFilesInDirectory(fullPath, files); } else { const relativePath = path.relative(process.cwd(), fullPath); files.push(relativePath); } } return files; }
const data = { gameList: { game: [ ] } };
let targetDirectory = "F:\\Roms\\SFC" try { const foundFiles = findFilesInDirectory(targetDirectory); console.log('找到的文件的相对路径:'); foundFiles.forEach(file => { let fileName = path.basename(file) let filePath = file.replace(targetDirectory, ".") console.log(fileName); console.log(filePath); data.gameList.game.push({ path: filePath, name: fileName, image: "", video: "" }) }); } catch (error) { console.error('查找文件时出错:', error); }
const builder = new xml2js.Builder();
const xml = builder.buildObject(data);
let xmlPath = path.join(targetDirectory, "gamelist.xml")
fs.writeFile(xmlPath, xml, 'utf8', (err) => { if (err) { console.error('写入文件时发生错误:', err); } else { console.log('XML 文件已成功生成: gamelist.xml'); } });
|