-
Notifications
You must be signed in to change notification settings - Fork 0
/
msts.js
274 lines (249 loc) · 6.46 KB
/
msts.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* 剧集改名
*Movie Subtitle Filename Sync
*Author deadivan[ A t ]gmail.com
*If you meet WScript " ** DoOpenPipeStream **" error message.The scrrun.dll may not
*registered properly.You need re-register it.
*From Start/Run, Type
*regsvr32 %SystemRoot%\system32\scrrun.dll
*It should be ok.
*/
var fso = new ActiveXObject("Scripting.FileSystemObject");
var wsh = WScript.CreateObject("WScript.Shell");
var regs = {
"movie" : /(\.avi$)|(\.mkv$)|(\.ts$)/i,
"sub" : /(\.srt$)|(\.idx$)|(\.sub$)/i,
"cd" : /(?!-|\.|^)cd\d+(?=\.|-)/i,
"rar" : /\.rar$/i,
"sample" : /((?:\.|-)sample(?=\.))/i,
"argv" : /^--.+/i,
"movie_id" : [/s0(\d)e(\d{2})/i , /(\d)x(\d{2})/i]
}
var subFormats = ["%S%%E%", "s0%S%e%E%", "%S%x%E%"];
//WScript.Echo(movieFilter.test(file));
var l = WScript.Arguments.length;
var argv = new Array();
for(var i = 0; i < l;i ++){
argv[i] = WScript.Arguments(i);
}
var flag = false;
var opts = findByFilter(argv, regs["argv"]);
var dirs = findByFilter(argv, regs["argv"], true);
if(argv.length == 0){
showHelp();
}else{
if(opts.length > 0){
//多个参数只使用第一个参数
switch(String(opts[0]).toLowerCase()){
case "--install":
install();
exit();
break;
case "--uninstall":
uninstall();
exit();
break;
case "--help":
default:
showHelp();
exit();
break;
}
}
var l = dirs.length;
for(var i = 0;i < l;i ++){
flag = tt(dirs[i]);
}
if(flag){
echo ("改名成功!");
}else{
echo ("失败!");
showHelp();
}
}
function showHelp(){
echo("剧集改名 [Movie Subtitle Filename Sync] v0.1.0 beta\n\n" +
"将一个或者数个剧集目录中的字幕文件名,根据同目录中的剧集文件名做修改。\n\n" +
"msfs.js [ --install | --uninstall | --help ] names\n\n" +
" names \t指定一个或者数个需要处理的目录的列表。\n" +
" --install t安装msfs.js到Windows目录,并且将\"字幕改名\"功能加入右键菜单。\n" +
" --uninstall\t删除Windows目录中的msfs.js,并且移除右键菜单中的\"字幕改名\"功能。\n" +
" --help \t显示本帮助信息。\n\n" +
"如果安装了本软件,可以选中一个或者数个剧集目录,右键选择\"字幕改名\"来进行改名\n操作。\n" +
"如果不使用任何参数,本软件将会显示帮助信息。\n" +
"如果剧集目录中有rar文件,也会将rar文件解压缩再进行改名操作,但是这个功能需要\n系统中安装WinRar。\n\n");
}
function echo(v){
wsh.PopUp(v.toString(), 0, "字幕改名");
}
function exit(){
WScript.Quit();
}
function install(){
var env = wsh.Environment("PROCESS");
var dest = env("SYSTEMROOT")+ "\\msfs.js";
wsh.RegWrite("HKCR\\Directory\\shell\\msfs\\",
"字幕改名");
wsh.RegWrite("HKCR\\Directory\\shell\\msfs\\command\\",
"WScript " + dest + " %1");
fso.CopyFile(WScript.ScriptFullName, dest);
echo("安装成功!");
}
function uninstall(){
var env = wsh.Environment("PROCESS");
var dest = env("SYSTEMROOT")+ "\\msfs.js";
try{
wsh.RegDelete("HKCR\\Directory\\shell\\msfs\\command\\");
wsh.RegDelete("HKCR\\Directory\\shell\\msfs\\");
fso.DeleteFile(dest);
}catch(e){
}
echo("成功卸载!");
}
function tt(v){
var f = false;
var af = getAllFiles(v);
var cf = {
"movie" : findByFilter(af, regs["movie"]),
"rar" : findByFilter(af, regs["rar"])
};
var cfrl = cf["rar"].length;
var t = true;
for(var i = 0;i < cfrl;i ++){
t = t && unrar(cf["rar"][i], dirname(cf["rar"][i]));
}
if(!t){
if(7 == wsh.Popup("解压缩文件的时候出现了错误,是否继续改名程序?", 0, "字幕改名", 4 + 48)){
WScript.Quit(0);
};
}
af = getAllFiles(v);
cf["sub"] = findByFilter(af, regs["sub"]);
var cfml = cf["movie"].length;
var t = findByFilter(cf["movie"], regs["sample"], true);
f = syncName(t, cf["sub"]);
return f;
}
/**
*@param mov Array movie files
*@param sub Array sub files
*/
function syncName(mov, sub){
var l = mov.length;
// var r = regs["cd"];//.compile(regs["cd"].source, "i");
var r = regs["movie_id"];
var f = false;
if(l > 1){
var tm = null;
for(var i = 0; i < l; i++){
for(idx in r){
tm = mov[i].match(r[idx]);
if(tm) break;
}
for(idx in subFormats){
var subFormat = subFormats[idx];
var tre = String(subFormat).replace("%S%", tm[1]);
tre = tre.replace("%E%", tm[2]);
var tre = new RegExp(tre, "i");
var as = findByFilter(sub, tre);
f = renameSub(mov[i], as);
}
}
}else if(l == 1){
f = renameSub(mov[0], sub);
}
return f;
}
/**
*@param mov String moviefile name
*@param sub Array sub files name
*/
function renameSub(mov, sub){
var l = sub.length;
for(var i = 0;i < l; i++){
var ext = "." + extName(sub[i]);
//keep .gb.srt .eng.srt .big5.srt 's lang info
var ext2 = "." + extName(mainName(sub[i]));
if(ext2.length > 8 || ext2 == ".") ext2 = "";
//echo([ext, ext2]);
//max is ".chinese"
var dest = dirname(mov) + "\\" + mainName(mov) +
String(ext2 + ext).replace(regs["cd"], "").replace(/\.+/, ".");
if(sub[i] == dest) continue;
var j = 1;
while(fso.FileExists(dest)){
dest = dirname(mov) + "\\" + mainName(mov) + ".(" + (j ++) + ")" +
String(ext2 + ext).replace(regs["cd"], "").replace(/\.+/, ".");
}
fso.MoveFile(sub[i],dest);
}
return true;
}
function unrar(f, d){
var wsh = WScript.CreateObject("WScript.Shell");
try{
var rarPath = wsh.RegRead("HKLM\\\SOFTWARE\\Microsoft\\" +
"Windows\\CurrentVersion\\App Paths\\WinRAR.exe\\Path");
}catch(e){
WScript.Echo("没有安装WinRar!无法解压缩rar文件!");
return false;
}
var cmd = "\"" + rarPath + "\\unrar.exe\" e -y " + " \"" +
String(f).replace(/\"/g, "\\\"") + "\" \"" +
String(d).replace(/\"/g, "\\\"") + "\" ";
return (wsh.Run(cmd, 7, true) == 0);
}
function basename(s){
var retval = s;
if(/\\/.test(s)){
retval = String(s).split(/\\/).slice(-1).toString();
}
return retval;
}
function dirname(s){
var retval = "";
if(/\\/.test(s)){
retval = String(s).split(/\\/).slice(0, -1).join("\\");
}
return retval;
}
function mainName(s){
var retval = "";
var l = basename(s)
if(l.length){
if(/\./.test(l)){
retval = String(l).split(".").slice(0, -1).join(".");
}
}
return retval;
}
function extName(s){
var retval = "";
var l = basename(s)
if(l.length){
if(/\./.test(l)){
retval = String(l).split(".").slice(-1).toString();
}
}
return retval;
}
function findByFilter(ary, reg, invert){
var invert = invert || false;
var retval = Array();var i = 0;
for(var t in ary){
if(invert != reg.test(ary[t])){
retval[i ++] = ary[t];
}
//echo([invert, reg.test(ary[t]), reg, ary[t]]);
}
return retval;
}
function getAllFiles(v){
var t = fso.GetFolder(v);
var retval = Array();
var folder = new Enumerator(t.files);
for(var i = 0; !folder.atEnd(); folder.moveNext(), i++){
retval[i] = String(folder.item());
}
return retval;
}