-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6700519
commit 01b0d1a
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
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
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; |