Skip to content

Commit

Permalink
Added rm command
Browse files Browse the repository at this point in the history
  • Loading branch information
hemanth-hk committed Mar 31, 2020
1 parent 6700519 commit 01b0d1a
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions commands/rm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//importing filesystem
const fs = require('fs');

//function removing the files
function rm(path, FileName) {
//if it contains '*', all files with the extension are deleted
if (FileName.includes('*')) {
extension = FileName.slice(1);
fs.readdir(path, (err, files) => {
files.forEach(file => {
if (file.match(extension)) {
new_path = path.concat(file);
fs.unlink(new_path, (err) => {
if (err) {
console.log("File does not exist with this name");
} else {
console.log(`${file} has been deleted`);
}
});
}
});
});
} else if (!(FileName.includes('.'))) {
var new_path = path.concat(FileName);
fs.rmdir(new_path, { recursive: true }, (err) => {
if (err) {
console.log("Folder does not exist with this name");
} else {
console.log(`${FileName} has been deleted`);
}
});
}
//else the particular filename is deleted
else {
var new_path = path.concat(FileName);
fs.unlink(new_path, (err) => {
if (err) {
console.log("File does not exist with this name");
} else {
console.log(`${FileName} has been deleted`);
}
});
}

}

//exporting the rm function
module.exports = rm;

0 comments on commit 01b0d1a

Please sign in to comment.