Created
December 17, 2016 14:14
-
-
Save pluswave/704ce165d9b5eeb9a969e215c5490fbc to your computer and use it in GitHub Desktop.
重命名一组视频文件,去掉前面重复的头,中文序号变为标准3个阿拉伯数字序号
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var chnNumChar = { | |
零:0, | |
一:1, | |
二:2, | |
三:3, | |
四:4, | |
五:5, | |
六:6, | |
七:7, | |
八:8, | |
九:9 | |
}; | |
var chnNameValue = { | |
十:{value:10, secUnit:false}, | |
百:{value:100, secUnit:false}, | |
千:{value:1000, secUnit:false}, | |
万:{value:10000, secUnit:true}, | |
亿:{value:100000000, secUnit:true} | |
}; | |
function ChineseToNumber(chnStr){ | |
var rtn = 0; | |
var section = 0; | |
var number = 0; | |
var secUnit = false; | |
var str = chnStr.split(''); | |
for(var i = 0; i < str.length; i++){ | |
var num = chnNumChar[str[i]]; | |
if(typeof num !== 'undefined'){ | |
number = num; | |
if(i === str.length - 1){ | |
section += number; | |
} | |
}else if( typeof chnNameValue[str[i]] != 'undefined' ) { | |
var unit = chnNameValue[str[i]].value; | |
secUnit = chnNameValue[str[i]].secUnit; | |
if(secUnit){ | |
section = (section + number) * unit; | |
rtn += section; | |
section = 0; | |
}else{ | |
if( number == 0 && unit == 10 ){ | |
number = 1; | |
} | |
section += (number * unit); | |
} | |
number = 0; | |
} | |
} | |
return rtn + section; | |
} | |
function fixedNum(n){ | |
var s=n.toString(10); | |
while(s.length < 3){ | |
s='0' + s; | |
} | |
return s; | |
} | |
var path=require('path'); | |
var fs=require('fs'); | |
var files=fs.readdirSync('.') | |
for (var index in files){ | |
var file = files[index] | |
if ( file.startsWith('陈') && file.endsWith('.mp4') ){ | |
console.log(file) | |
var nFileName=file.substring(9) | |
console.log(nFileName) | |
fs.renameSync(file, nFileName) | |
} | |
else if( file.endsWith('.mp4')) { | |
var co = ':' | |
n_and_f = file.split(co) | |
if( n_and_f.length < 2){ | |
continue; | |
} | |
console.log(n_and_f) | |
var s=ChineseToNumber(n_and_f[0]); | |
var f=n_and_f[1] | |
var f_and_exe = f.split('.'); | |
var f = f_and_exe[0]; | |
var ext='mp4'; | |
f=f.replace(/[ -Youtube\[\]]/g,''); | |
f=f.replace('p', ''); | |
console.log(f) | |
var nName=fixedNum(s) + f + "." + ext; | |
console.log(nName); | |
fs.renameSync(file, nName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment