Hello. I have a question about whether something is possible to do with this library. Say I have a folder of many files of many different types -- how can I pick out the files which are SQLite databases from files of other types (without relying on file extensions)? Thanks
const Database = require('better-sqlite3');
function isSqliteDatabase(filepath) {
try {
new Database(filepath, { fileMustExist: true });
return true;
} catch (err) {
return false;
}
}
This should generally work too (without using this library), but may rarely give false positives:
const fs = require('fs');
function isSqliteDatabase(filepath) {
const file = fs.openSync(filepath);
try {
const buf = Buffer.alloc(16);
fs.readSync(file, buf, 0, 16, 0);
return buf.toString() === 'SQLite format 3\0';
} catch (err) {
return false;
} finally {
fs.closeSync(file);
}
}
Thank you for responding and for the help :+1: