Last active
August 29, 2015 14:05
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
changed line 20 to use dir variable instead of the './tmp/' string