const fs = require('fs'); const path = require('path'); const iconv = require('iconv-lite'); const dirPath = __dirname; const outputFile = path.join(dirPath, 'merged.csv'); // CSVファイルを取得 const files = fs.readdirSync(dirPath) .filter(file => file.endsWith('.csv') && file !== 'merged.csv') .sort(); if (files.length === 0) { console.log('CSVファイルが見つかりません'); process.exit(1); } let mergedContent = ''; files.forEach((file, index) => { const filePath = path.join(dirPath, file); // shift-jisでバッファとして読み込み、iconv-liteでデコード const buffer = fs.readFileSync(filePath); const content = iconv.decode(buffer, 'shift_jis'); const lines = content.split('\n'); if (index === 0) { // 最初のファイルはヘッダを含める mergedContent += content; } else { // 2ファイル目以降はヘッダをスキップ mergedContent += lines.slice(1).join('\n'); } if (index < files.length - 1 && !mergedContent.endsWith('\n')) { mergedContent += '\n'; } }); // shift-jisでエンコードして書き込み fs.writeFileSync(outputFile, iconv.encode(mergedContent, 'shift_jis')); console.log(`マージ完了: ${outputFile}`);