123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- const fs = require('fs')
- const path = require('path')
- var express = require("express");
- const router = express.Router();
- // var filelistname = '/upload/image' + '/' + global.filelistname
- // var logpath = path.join(filelistname)
- // readlogdir(path.join(logpath))
- // function readlogdir(path, res) {
- // fs.readdir(path, function (err, menu) {
- // res.send(menu);
- // if (!menu)
- // return;
- // menu.forEach(function (ele) {
- // fs.stat(path + "/" + ele, function (err, info) {
- // if (info.isDirectory()) {
- // //todo:
- // } else {
- // //is file
- // if ((ele.substring(ele.lastIndexOf('.') + 1)) == "INFO") {
- // fs.open(path + "/" + ele, 'r', function (err, fd) {
- // if (err) {
- // return console.error(err)
- // }
- // fs.read(fd, clusterBuffer, 0, clusterBuffer.length, 0, function (err, bytes) {
- // if (err) {
- // console.log(err)
- // }
- // console.log(clusterBuffer.slice(0, bytes).toString())
- // if (bytes > 0) {
- // //todo..
- // }
- // })//fs.read
- // })//fs.open
- // }//if
- // }
- // })//fs.stat
- // })//menu.forEach
- // })//fs.readdir
- // }
- // function readlogdir(path, filesList) {
- // console.log(path);
- // fs.readdir(path, function (err, menu) {
- // // console.log(menu);
- // global.files = menu
- // console.log(global.files);
- // });
- // console.log(global.files + "11111111");
- // global.files.forEach(function (itm, index) {
- // var stat = fs.statSync(path + itm);
- // if (stat.isDirectory()) {
- // //递归读取文件
- // readlogdir(path + itm + "/", filesList)
- // } else {
- // var obj = {};//定义一个对象存放文件的路径和名字
- // obj.path = path;//路径
- // obj.filename = itm//名字
- // filesList.push(obj);
- // }
- // res.send(filesList);
- // })
- // }
- function readFileListAgain(path, res) {
- if (fs.existsSync(path)) {
- // console.log(path);
- // if (fs.statSync(path).isDirectory()) {
- var files = fs.readdirSync(path)
- res.send(files);
- // console.log(files);
- } else {
- res.send(false);
- }
- }
- function readFileList(path, res) {
- if (fs.existsSync(path)) {
- // console.log(path);
- // if (fs.statSync(path).isDirectory()) {
- var files = fs.readdirSync(path)
- res.send(files);
- // console.log(files);
- } else {
- res.send(false);
- }
- // // console.log(files);
- }
- function deleteFolder(req, res) {
- var path = req.body.path;
- var name = req.body.name
- // var files = [];
- console.log(path);
- if (fs.existsSync(path)) {
- if (fs.statSync(path).isDirectory()) {
- // files = fs.readdirSync(path);
- // files.forEach(function (file, index) {
- var curPath = path + "/" + name;
- console.log(curPath);
- if (fs.statSync(curPath).isFile()) {
- // deleteFolder(curPath);
- fs.unlinkSync(curPath);
- } else {
- res.send(curPath);
- }
- } else {
- res.send(path);
- }
- // else {
- }
- // });
- // fs.rmdirSync(path);
- // } else {
- // fs.unlinkSync(path);
- // }
- // }
- res.send(path);
- }
- function deleteFolderall(path, res) {
- // console.log(path);
- var files = [];
- if (fs.existsSync(path)) {
- if (fs.statSync(path).isDirectory()) {
- files = fs.readdirSync(path);
- // console.log(files);
- files.forEach(function (file, index) {
- var curPath = path + "/" + file;
- // console.log(curPath);
- if (fs.statSync(curPath).isDirectory()) {
- deleteFolderall(curPath);
- } else {
- fs.unlinkSync(curPath);
- }
- });
- fs.rmdirSync(path);
- } else {
- fs.unlinkSync(path);
- }
- }
- res.send(path);
- }
- module.exports = {
- readFileListAgain,
- readFileList,
- deleteFolder,
- deleteFolderall
- };
|