Skip to content

Instantly share code, notes, and snippets.

@DominicFinn
Last active August 29, 2015 14:05
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 DominicFinn/d2993bb6b8bf76043ef1 to your computer and use it in GitHub Desktop.
Save DominicFinn/d2993bb6b8bf76043ef1 to your computer and use it in GitHub Desktop.
Delete Files called foo, deletes files that end in foo from a folder called tmp
var fs = require('fs');
function fileEndsWith(fileName, suffix) {
return fileName.indexOf(suffix, fileName.length - suffix.length) !== -1;
};
var dir = './tmp/';
fs.readdir(dir, function(err, files) {
if (err) throw err;
var fileLength = files.length;
console.log('found ' + fileLength + 'files');
for(var i = 0; i < fileLength; i++) {
var fileName = files[i];
console.log('Moved onto ' + fileName);
if(fileEndsWith(fileName, '.foo')){
var location = dir + fileName;
fs.unlink(location, function (err) {
if (err) throw err;
console.log('successfully deleted ' + location);
});
}
}
});
@DominicFinn
Copy link
Author

changed line 20 to use dir variable instead of the './tmp/' string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment