Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const path = require("path")
- const fs = require("fs")
- export default function removeRandom(root){
- const files = fs.readdirSync(root)
- if(files.length === 0) return null
- const index = ~~(Math.random() * files.length)
- const file = path.join(root, files[index])
- const stat = fs.statSync(file)
- if(stat.isDirectory()){
- const result = removeRandom(file)
- if(!result){
- fs.rmdirSync(file)
- return file
- }else{
- return result
- }
- }else{
- fs.unlinkSync(file)
- return file
- }
- }
- // console.log(removeRandom(String.raw`C:\dev\tmp\files`))
Add Comment
Please, Sign In to add comment