Skip to content

Instantly share code, notes, and snippets.

@OldStarchy
Last active February 13, 2020 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OldStarchy/e11b07bd69bf39f4ed151dcd5e03a066 to your computer and use it in GitHub Desktop.
Save OldStarchy/e11b07bd69bf39f4ed151dcd5e03a066 to your computer and use it in GitHub Desktop.
Delete files in a directory that are more than 7 days old and optionally create a file named with a warning message.
const fs = require('fs');
const path = require('path');
/* Config */
const days = 7;
const protectedFileName = `items older than ${days} day${
days == 1 ? '' : 's'
} are automatically deleted`;
const shouldCreateProtectedFile = true;
const listEveryFile = false;
/* Get path from args */
const args = process.argv.slice(2);
if (args.length == 0) {
console.log('a path must be specified');
process.exit(1);
}
if (args.length > 1) {
console.log('too many arguments specified');
process.exit(1);
}
const rootDir = path.resolve(args[0]);
if (!fs.existsSync(rootDir)) {
console.log('specified path does not exist');
process.exit(1);
}
if (!fs.statSync(rootDir).isDirectory()) {
console.log('specified path is not a directory');
process.exit(1);
}
function daysToMs(days) {
return days * 24 * 60 * 60 * 1000;
}
/* Delete stuff */
console.log(
`Purging "${rootDir}" of items older than ${days} day${
days == 1 ? '' : 's'
}...`
);
if (listEveryFile) {
console.log();
}
const sevenDaysAgoMs = Date.now() - daysToMs(days);
let filesRemoved = 0;
let directoriesRemoved = 0;
let foundProtectedFile = false;
fs.readdirSync(rootDir)
.filter(subFileOrDirectory => {
const isProtected = subFileOrDirectory === protectedFileName;
foundProtectedFile |= isProtected;
return !isProtected;
})
.map(subFileOrDirectory => path.join(rootDir, subFileOrDirectory))
.forEach(purgeFileOrDirectory);
if (!foundProtectedFile && shouldCreateProtectedFile) {
fs.writeFileSync(path.join(rootDir, protectedFileName), '');
}
function purgeFileOrDirectory(fileOrDirectory) {
const stat = fs.statSync(fileOrDirectory);
if (stat.isFile() || stat.isSymbolicLink()) {
const file = fileOrDirectory;
if (listEveryFile) {
console.log(`Checking "${file}"`);
}
const modifiedTimeMs = stat.mtime.getTime();
if (modifiedTimeMs < sevenDaysAgoMs) {
fs.unlinkSync(file);
filesRemoved++;
}
} else if (stat.isDirectory()) {
const dir = fileOrDirectory;
fs.readdirSync(dir)
.map(subFileOrDirectory => path.join(dir, subFileOrDirectory))
.forEach(purgeFileOrDirectory);
if (listEveryFile) {
console.log(`Checking "${dir}"`);
}
if (fs.readdirSync(dir).length === 0) {
fs.rmdirSync(dir);
directoriesRemoved++;
}
}
}
const removed = [];
if (filesRemoved > 0) {
removed.push(`${filesRemoved} file${filesRemoved == 1 ? '' : 's'}`);
}
if (directoriesRemoved > 0) {
removed.push(
`${directoriesRemoved} director${directoriesRemoved == 1 ? 'y' : 'ies'}`
);
}
if (listEveryFile) {
console.log();
}
if (filesRemoved + directoriesRemoved === 0) {
console.log('No files or directories removed');
} else {
console.log(`Removed ${removed.join(' and ')}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment