-
Notifications
You must be signed in to change notification settings - Fork 69
/
del_pyc.js
131 lines (109 loc) · 3.2 KB
/
del_pyc.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
/// Global ----------------------------------------------------
var consoleOnly = true;
var defaultTimeout = 1;
var WSShell;
var fso;
var currentFolder;
var ForReading = 1, ForWriting = 2, ForAppending = 8;
var CharSetDefault = -2, CharSetUnicode = -1, CharSetAscii = 0;
var AttrNormal = 0, AttrReadOnly = 1, AttrHidden = 2, AttrSystem = 4,
AttrVolume = 8, AttrDirectory = 16, AttrArchive = 32, AttrAlias = 1024,
AttrCompressed = 2048;
Init();
Main();
function Init() {
// detect command line
try {
WScript.StdOut.WriteLine(" ");
} catch (e) {
consoleOnly = false;
}
// initialize
WSShell = new ActiveXObject("WScript.Shell");
fso = new ActiveXObject("Scripting.FileSystemObject");
currentFolder = GetCurrentFolder();
}
function Main() {
var text;
text = "开始处理.";
Out(text, true);
COut("当前工作目录: /r/n" + currentFolder);
var files = FindFiles("[.]*[.]pyc");
COut("共有 " + files.length + " 个 .pyc 文件");
var deleted = 0;
for(var i=0;i<files.length;i++){
var f = fso.GetFile(files[i]);
f.Delete(true);
deleted++;
}
Out("成功删除了 " + deleted + " 个 .pyc 文件", false);
}
/// Files ------------------------------------------------------
// getcurrent folder
function GetCurrentFolder() {
return fso.GetFolder(fso.GetFile(WScript.ScriptFullName).ParentFolder);
}
/// Output ------------------------------------------------------
// output
function Out(text, useTimeout) {
if (useTimeout) {
useTimeout = defaultTimeout;
} else {
useTimeout = -1;
}
if (consoleOnly) {
WScript.StdOut.WriteLine(text);
} else {
WSShell.Popup(text, useTimeout, "删除 .pyc 文件");
}
}
// output
function COut(text, useTimeout) {
if (useTimeout) {
useTimeout = defaultTimeout;
} else {
useTimeout = -1;
}
if (consoleOnly) {
WScript.StdOut.WriteLine(text);
}
}
function ReadFile(file) {
var stream = file.OpenAsTextStream(ForReading, CharSetDefault);
text = stream.ReadAll();
stream.Close();
return text;
}
function WriteFile(file, text) {
var ro = ((file.Attributes & AttrReadOnly) != 0);
if (ro) file.Attributes -= AttrReadOnly;
var stream = file.OpenAsTextStream(ForWriting, CharSetDefault);
stream.Write(text);
stream.Close();
if (ro) file.Attributes += AttrReadOnly;
}
// determine, if filename matches given mask
function MatchesMask(file, mask) {
return new RegExp(mask).test(file);
}
// find files
function FindFiles(mask) {
return GetFiles(currentFolder, mask);
}
// get files in current folder & subfolders
function GetFiles(folder, mask) {
var result = new Array();
// do files in current folder
var files = new Enumerator(folder.Files);
for (; !files.atEnd(); files.moveNext()) {
if (MatchesMask(files.item(), mask)) {
result.push("" + files.item());
}
}
// do subfolders in current folder
var folders = new Enumerator(folder.SubFolders);
for (; !folders.atEnd(); folders.moveNext()) {
result = result.concat(GetFiles(folders.item(), mask));
}
return result;
}