fileManager.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. var fs = require('fs');
  2. var path = require('path');
  3. var deleteQueue = [];
  4. //电子邮件puhalskijsemen@gmail.com
  5. //源码网站 开vpn全局模式打开 http://web3incubators.com/
  6. //电报https://t.me/gamecode999
  7. //网页客服 http://web3incubators.com/kefu.html
  8. /**读取文件夹内容 */
  9. function readDir(dirPath, onComp) {
  10. var callback = onComp ? onComp : function (err, files) {
  11. if (err) {
  12. console.error(err);
  13. } else {
  14. console.log(files);
  15. console.log("文件夹读取成功!");
  16. }
  17. };
  18. fs.readdir(dirPath, callback);
  19. }
  20. /**同步读取文件内容 */
  21. function readDirSync(dirPath) {
  22. var files = fs.readdirSync(dirPath);
  23. if(files.length <= 0){
  24. console.warn(dirPath+" 中没有文件!");
  25. }else{
  26. console.log(files);
  27. }
  28. return files;
  29. }
  30. /**创建新文件夹若存在则创建失败 */
  31. function createDir(dirPath) {
  32. fs.mkdir(dirPath, function (err) {
  33. if (err) {
  34. console.error(err);
  35. } else {
  36. console.log("文件夹创建成功!");
  37. }
  38. });
  39. }
  40. /**同步创建新文件夹若存在则创建失败 */
  41. function createDirSync(dirPath) {
  42. fs.mkdirSync(dirPath);
  43. console.log("创建成功!");
  44. }
  45. /**删除空文件夹若不为空则失败 */
  46. function deleteEmptyDir(dirPath,onComp) {
  47. var callback = onComp ? onComp : function (err) {
  48. if (err) {
  49. console.error(err);
  50. } else {
  51. console.log("文件夹删除成功!");
  52. }
  53. };
  54. fs.rmdir(dirPath,callback);
  55. }
  56. /**同步删除空文件夹若不为空则失败 */
  57. function deleteEmptyDirSync(dirPath,onComp) {
  58. fs.rmdirSync(dirPath);
  59. onComp && onComp();
  60. // console.log("文件夹删除成功!");
  61. }
  62. /**同步删除队列中的文件夹 */
  63. function deleteDirQueue() {
  64. var len = deleteQueue.length;
  65. for(var i = 0; i < len;i++){
  66. deleteEmptyDirSync(deleteQueue[i]);
  67. }
  68. }
  69. /** 同步删除文件夹及其中的文件 */
  70. function deleteDirAndFileSync(dirPath,deleteSelf = false) {
  71. var files = readDirSync(dirPath);
  72. var len = files.length;
  73. for(var i = 0;i < len;i++){
  74. var file = files[i];
  75. let filePath = path.resolve(dirPath, file);//单个文件绝对路径
  76. var state = fs.statSync(filePath);//单个文件的状态
  77. if(state.isDirectory()){//如果是文件夹
  78. deleteDirAndFileSync(filePath,true);
  79. }else{//否则是文件,直接删除
  80. deleteFileSync(filePath);
  81. }
  82. }
  83. deleteSelf && deleteQueue.push(dirPath);
  84. }
  85. /**读取文件 */
  86. function readFile(filePath) {
  87. return fs.readFile(filePath, function (err) {
  88. if (err) {
  89. console.error(err);
  90. } else {
  91. console.log("文件读取成功!");
  92. }
  93. });
  94. }
  95. /**写入文件若文件不存在则创建文件并写入 */
  96. function writeFile(filePath, fileData) {
  97. fs.writeFile(filePath, fileData, function (err) {
  98. if (err) {
  99. console.error(err);
  100. } else {
  101. // console.log("文件写入成功!");
  102. }
  103. });
  104. }
  105. /**删除文件 */
  106. function deleteFile(filePath) {
  107. fs.unlink(filePath, function (err) {
  108. if (err) {
  109. console.error(err);
  110. } else {
  111. console.log("文件删除成功!");
  112. }
  113. });
  114. }
  115. /**同步删除文件 */
  116. function deleteFileSync(filePath) {
  117. fs.unlinkSync(filePath);
  118. console.log(filePath + " 删除成功!");
  119. }
  120. module.exports = {
  121. readDir,
  122. readDirSync,
  123. createDir,
  124. createDirSync,
  125. deleteEmptyDir,
  126. deleteEmptyDirSync,
  127. readFile,
  128. writeFile,
  129. deleteFile,
  130. deleteFileSync,
  131. deleteDirAndFileSync,
  132. deleteDirQueue,
  133. deleteQueue
  134. }