// Name: Merge CSV Files // Description: Drop multiple CSV files to merge them into a single CSV file // Author: zacjones93 import "@johnlindquist/kit" const droppedFiles = await drop("Drop CSV files to merge") if (!Array.isArray(droppedFiles) || droppedFiles.length === 0) { await div(md("No files were dropped")) exit() } const csvFiles = droppedFiles.filter(file => file.path.toLowerCase().endsWith('.csv') ) if (csvFiles.length === 0) { await div(md("No CSV files found in the dropped files")) exit() } let mergedData = [] let headers = null for (const file of csvFiles) { const content = await readFile(file.path, 'utf-8') const lines = content.trim().split('\n') if (lines.length === 0) continue // Use first file's headers as the master headers if (headers === null) { headers = lines[0] mergedData.push(headers) } // Add data rows (skip header row for subsequent files) const dataRows = headers === lines[0] ? lines.slice(1) : lines.slice(1) mergedData.push(...dataRows) } if (mergedData.length <= 1) { await div(md("No data found to merge")) exit() } const mergedCsv = mergedData.join('\n') const outputPath = home("Downloads", `merged-${Date.now()}.csv`) await writeFile(outputPath, mergedCsv) await revealFile(outputPath) await div(md(` # CSV Files Merged Successfully! **Files processed:** ${csvFiles.length} **Total rows:** ${mergedData.length - 1} (excluding header) **Output file:** ${outputPath} The merged CSV file has been saved to your Downloads folder. `))