Skip to content

Instantly share code, notes, and snippets.

@vinayakg
Created March 24, 2024 13:03
Show Gist options
  • Save vinayakg/82345251d7ff12f2ffc742d6c0fe91b2 to your computer and use it in GitHub Desktop.
Save vinayakg/82345251d7ff12f2ffc742d6c0fe91b2 to your computer and use it in GitHub Desktop.
count rows xlsx
const XLSX = require('xlsx');
const fs = require('fs');
const path = require('path');
const { createHash } = require('crypto');
let overallRowCount = 0;
try {
// Function to count rows in a single .xlsx file
const countRows = (filePath) => {
try {
const workbook = XLSX.readFile(filePath);
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
return XLSX.utils.sheet_to_json(worksheet).length;
} catch (error) {
console.log(error);
}
};
const generateMD5 = (filePath) => {
const fileBuffer = fs.readFileSync(filePath);
const hashSum = createHash('md5');
hashSum.update(fileBuffer);
const hex = hashSum.digest('hex');
return hex;
};
// Function to count rows of all .xlsx files in a folder
const countRowsInFolder = (folderPath) => {
const files = fs.readdirSync(folderPath);
//console.log(`path: ${folderPath}`)
try {
files.forEach((file) => {
//console.log(`file: ${file}`)
if (path.extname(file) === '.xlsx') {
const filePath = path.join(folderPath, file);
//console.log(`filePath: ${filePath}`);
const rowCount = countRows(filePath);
overallRowCount = overallRowCount + rowCount;
const md5Hash = generateMD5(filePath);
console.log(`${file}, rows: ${rowCount}, hash: ${md5Hash}, overall: ${overallRowCount}`);
}
});
} catch (error) {
console.log(error);
}
};
// Replace 'your/folder/path' with the path to the folder containing your .xlsx files
countRowsInFolder('/Users/vinayak/Downloads/GSM-Bulk-Registrations');
} catch (error) {
console.log(error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment