readdir.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. const fs = require('fs')
  2. const path = require('path')
  3. var express = require("express");
  4. const router = express.Router();
  5. // var filelistname = '/upload/image' + '/' + global.filelistname
  6. // var logpath = path.join(filelistname)
  7. // readlogdir(path.join(logpath))
  8. // function readlogdir(path, res) {
  9. // fs.readdir(path, function (err, menu) {
  10. // res.send(menu);
  11. // if (!menu)
  12. // return;
  13. // menu.forEach(function (ele) {
  14. // fs.stat(path + "/" + ele, function (err, info) {
  15. // if (info.isDirectory()) {
  16. // //todo:
  17. // } else {
  18. // //is file
  19. // if ((ele.substring(ele.lastIndexOf('.') + 1)) == "INFO") {
  20. // fs.open(path + "/" + ele, 'r', function (err, fd) {
  21. // if (err) {
  22. // return console.error(err)
  23. // }
  24. // fs.read(fd, clusterBuffer, 0, clusterBuffer.length, 0, function (err, bytes) {
  25. // if (err) {
  26. // console.log(err)
  27. // }
  28. // console.log(clusterBuffer.slice(0, bytes).toString())
  29. // if (bytes > 0) {
  30. // //todo..
  31. // }
  32. // })//fs.read 
  33. // })//fs.open
  34. // }//if
  35. // }
  36. // })//fs.stat
  37. // })//menu.forEach
  38. // })//fs.readdir
  39. // }
  40. // function readlogdir(path, filesList) {
  41. // console.log(path);
  42. // fs.readdir(path, function (err, menu) {
  43. // // console.log(menu);
  44. // global.files = menu
  45. // console.log(global.files);
  46. // });
  47. // console.log(global.files + "11111111");
  48. // global.files.forEach(function (itm, index) {
  49. // var stat = fs.statSync(path + itm);
  50. // if (stat.isDirectory()) {
  51. // //递归读取文件
  52. // readlogdir(path + itm + "/", filesList)
  53. // } else {
  54. // var obj = {};//定义一个对象存放文件的路径和名字
  55. // obj.path = path;//路径
  56. // obj.filename = itm//名字
  57. // filesList.push(obj);
  58. // }
  59. // res.send(filesList);
  60. // })
  61. // }
  62. function readFileListAgain(path, res) {
  63. if (fs.existsSync(path)) {
  64. // console.log(path);
  65. // if (fs.statSync(path).isDirectory()) {
  66. var files = fs.readdirSync(path)
  67. res.send(files);
  68. // console.log(files);
  69. } else {
  70. res.send(false);
  71. }
  72. }
  73. function readFileList(path, res) {
  74. if (fs.existsSync(path)) {
  75. // console.log(path);
  76. // if (fs.statSync(path).isDirectory()) {
  77. var files = fs.readdirSync(path)
  78. res.send(files);
  79. // console.log(files);
  80. } else {
  81. res.send(false);
  82. }
  83. // // console.log(files);
  84. }
  85. function deleteFolder(req, res) {
  86. var path = req.body.path;
  87. var name = req.body.name
  88. // var files = [];
  89. console.log(path);
  90. if (fs.existsSync(path)) {
  91. if (fs.statSync(path).isDirectory()) {
  92. // files = fs.readdirSync(path);
  93. // files.forEach(function (file, index) {
  94. var curPath = path + "/" + name;
  95. console.log(curPath);
  96. if (fs.statSync(curPath).isFile()) {
  97. // deleteFolder(curPath);
  98. fs.unlinkSync(curPath);
  99. } else {
  100. res.send(curPath);
  101. }
  102. } else {
  103. res.send(path);
  104. }
  105. // else {
  106. }
  107. // });
  108. // fs.rmdirSync(path);
  109. // } else {
  110. // fs.unlinkSync(path);
  111. // }
  112. // }
  113. res.send(path);
  114. }
  115. function deleteFolderall(path, res) {
  116. // console.log(path);
  117. var files = [];
  118. if (fs.existsSync(path)) {
  119. if (fs.statSync(path).isDirectory()) {
  120. files = fs.readdirSync(path);
  121. // console.log(files);
  122. files.forEach(function (file, index) {
  123. var curPath = path + "/" + file;
  124. // console.log(curPath);
  125. if (fs.statSync(curPath).isDirectory()) {
  126. deleteFolderall(curPath);
  127. } else {
  128. fs.unlinkSync(curPath);
  129. }
  130. });
  131. fs.rmdirSync(path);
  132. } else {
  133. fs.unlinkSync(path);
  134. }
  135. }
  136. res.send(path);
  137. }
  138. module.exports = {
  139. readFileListAgain,
  140. readFileList,
  141. deleteFolder,
  142. deleteFolderall
  143. };