Guest User

Untitled

a guest
Jul 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. const path = require("path")
  2. const fs = require("fs")
  3.  
  4. export default function removeRandom(root){
  5. const files = fs.readdirSync(root)
  6. if(files.length === 0) return null
  7. const index = ~~(Math.random() * files.length)
  8. const file = path.join(root, files[index])
  9. const stat = fs.statSync(file)
  10. if(stat.isDirectory()){
  11. const result = removeRandom(file)
  12. if(!result){
  13. fs.rmdirSync(file)
  14. return file
  15. }else{
  16. return result
  17. }
  18. }else{
  19. fs.unlinkSync(file)
  20. return file
  21. }
  22. }
  23.  
  24. // console.log(removeRandom(String.raw`C:\dev\tmp\files`))
Add Comment
Please, Sign In to add comment