Spring774
2025-11-12 2379cc78112ecbe65a9fcb817d6441875f785803
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
 * Created by xujianfei on 18/2/28.
 */
 
var fs = require("fs");
var path = require('path');
 
 
module.exports = {
 
    moveDir: function(fromDir,toDir){
        var files = [];
        if (fs.existsSync(fromDir)) {
            files = fs.readdirSync(fromDir);
            files.forEach(function (file, index) {
                var curPath = fromDir + "/" + file;
                var toPath = toDir + "/" + file;
                if (fs.statSync(curPath).isDirectory()) { // recurse
                    this.moveDir(curPath,toPath);
                } else {
                    this.moveFile(curPath,toPath);
                }
                //fs.rmdir(fromDir);
            }.bind(this));
        }
    },
 
    moveFile: function(from,to){
        // console.log('移动文件 from ' + from + ', to ' + to);
        if(!fs.existsSync(from)){
            console.log('文件不存在!');
            return;
        }
 
        this.checkAndCreateDir(to);
 
        fs.renameSync(from, to, function (err) {
            if (err) throw err;
        });
    },
 
    copyDir: function(fromDir,toDir,filter){
        var files = [];
        if (fs.existsSync(fromDir)) {
            files = fs.readdirSync(fromDir);
            files.forEach(function (file, index) {
                var curPath = fromDir + "/" + file;
                var toPath = toDir + "/" + file;
                if (fs.statSync(curPath).isDirectory()) { // recurse
                    this.copyDir(curPath,toPath,filter);
                }
                else {
                    var need_copy = true;
                    //console.log('file = ' + file + " filter = " + filter);
                    if(filter) {
                        if(file.indexOf(filter) > -1){
                            need_copy = true;
                        }
                        else{
                            need_copy = false;
                        }
 
                    }
                    if(need_copy){
                        this.copyFile(curPath,toPath);
                    }
 
                }
            }.bind(this));
        }
    },
 
    copyFile: function(from,to){
        //console.log('copy file from ' + from + ', to ' + to);
        if(!fs.existsSync(from)){
            console.log('文件不存在!' + from);
            return;
        }
 
        this.checkAndCreateDir(to);
 
        //fs.createReadStream(from).pipe(fs.createWriteStream(to));
        fs.writeFileSync(to, fs.readFileSync(from));
    },
 
    checkAndCreateDir: function(dirOrFilePath) {
 
        var lineArray = dirOrFilePath.split('/');
        var root = '';
        for (var lineIdx = 0; lineIdx < lineArray.length; lineIdx++) {
            //目录
            var dic = lineArray[lineIdx];
            root += (dic + '/');
 
            //console.log('root = ' + root);
            if (dic.indexOf('.') < 0) {
                //创建目录
                if (!fs.existsSync(root)) {
                    //console.log('创建目录 ' + root);
                    fs.mkdirSync(root);
 
                }
            }
        }
    },
 
    deleteFile: function(filePath){
        fs.unlinkSync(filePath);
    },
 
    //删除文件夹
    deleteDir: function(path) {
        var self = this;
        var files = [];
        if( fs.existsSync(path) ) {
            files = fs.readdirSync(path);
            files.forEach(function(file,index){
                var curPath = path + "/" + file;
                if(fs.statSync(curPath).isDirectory()) {
                    // recurse
                    self.deleteDir(curPath);
                } else {
                    // delete file
                    fs.unlinkSync(curPath);
                }
            });
            fs.rmdirSync(path);
        }
    },
 
    //清空文件夹
    cleanDir: function(rootPath) {
 
        var deleteDir = function(path){
            var files = [];
            if( fs.existsSync(path) ) {
                files = fs.readdirSync(path);
                files.forEach(function(file,index){
                    var curPath = path + "/" + file;
                    if(fs.statSync(curPath).isDirectory()) {
                        // recurse
                        deleteDir(curPath);
                    } else {
                        // delete file
                        fs.unlinkSync(curPath);
                    }
                });
                if(path != rootPath){
                    fs.rmdirSync(path);
                }
            }
        };
 
        deleteDir(rootPath);
    },
 
    // 递归创建目录 同步方法
    mkDir : function(dirname) {
        var self = this;
        if (fs.existsSync(dirname)) {
            return true;
        } else {
            if (self.mkDir(path.dirname(dirname))) {
                fs.mkdirSync(dirname);
                return true;
            }
        }
    }
};