-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12-重命名.js
73 lines (68 loc) · 1.88 KB
/
12-重命名.js
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
// 1. 导入fs模块
const { log } = require('console');
const fs = require('fs');
var dir = 'dir';
var newDir = 'newDir'
// 2. 创建一些文件
fs.stat(dir, (err, data) => {
if (err) {
log('ERROR: ' + err);
log('操作失败==>不存在此文件夹或……');
makeDir(dir);
return;
} else {
if (data.isDirectory) {
fs.rm(dir, { recursive: true }, err => {
if (err) {
log('删除失败');
return;
} else {
log('删除成功');
makeDir(dir);
}
});
} else {
log('dir不是一个文件夹')
}
}
});
function makeDir(dir) {
fs.mkdir(dir, err => {
if (err) {
log('创建失败');
return;
} else {
log('创建成功');
for (let i = 0; i <= 15; i++) {
log(i);
writeFile(i + '-文本.txt');
}
const files = fs.readdirSync(dir);
// log(files);
files.forEach(item => {
// split() 将字符串分割成数组
let data = item.split('-');
let [num, name] = data;
if (Number(num) < 10) {
num = '0' + num;
}
// 创建新的文件名
let newName = num + '-' + name;
// 重命名
fs.renameSync(dir + `/${item}`, dir + `/${newName}`);
log(newName);
});
}
});
}
function writeFile(fileName) {
const fs1 = require('fs');
fs1.writeFile(dir + '/' + fileName, fileName + 'haha', err => {
if (err) {
console.error('Error :', err);
log('写入失败');
} else {
log('写入成功');
}
});
}