-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_copyFilesTosub.js
47 lines (40 loc) · 1.11 KB
/
1_copyFilesTosub.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
// rootDir > 子資料夾 > ***.en.srt |全部srt檔案移動到| rootDir > 子資料夾 > sub > ***.en.srt
// rootDir > ***.en.srt 不會動作
const fs = require("fs")
const rootDir = require('./lib/path.js')
const getDirectories = require('./lib/findAllDir.js')
let oldDir = rootDir+'/'
let newDir = rootDir+'/sub/'
let dirs = getDirectories(rootDir)
// console.log(dirs)
dirs.forEach( dir => {
let files = fs.readdirSync(dir)
console.log(dir)
let names = []
files.forEach( file => {
if (file.match(/en\.srt$/)){
names.push(file)
}
})
// console.log(names)
if (names !== []){
// let opath = oldDir +
names.forEach(name => {
// console.log(dir+ '/' + name)
// console.log(dir+ '/')
// console.log(name)
copyFile(dir + '/' + name, dir + '/sub/', name, () => {
console.log(`move to ${dir}/sub/`)
})
})
}
})
//======
function copyFile(source, targetDir, file) {
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir)
}
fs.createReadStream(source)
.pipe(fs.createWriteStream(targetDir + file));
console.log(`copy to ${targetDir}`)
}