- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章用VBS写的VBSCRIPT代码格式化工具VbsBeautifier由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
昨天在VBS吧看到一个精华帖《VBS代码格式化工具》,是用C++写的,区区VBS代码格式化,就不要劳C++大驾了吧,用VBS实现VBS代码格式化工具不是更自然么?
。
网上的VBS代码大部分都没有缩进,新手不知道要缩进,高手缩进了被某些个垃圾网站采集以后也就没有了缩进,还有以一些博客贴吧也会把缩进给吃掉。除了缩进之外,由于学VBS的大部分都是学批处理出身,代码风格还是跟写批处理一样难看。其实一般情况下用VbsEdit 5.2.4.0自带的代码格式化功能就行了,没有必要重复造轮子。只不过VbsEdit 5.2.4.0在格式化带有冒号的代码时不是很理想,加上我已经很久没有写过像样的VBS脚本了,所以还是决定造一下轮子.
因为代码比较长,所以贴在文章的最后,下面是VBS代码格式化工具的效果演示:
格式化前的VBS代码:
。
。
格式化后的VBS代码:
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
|
On
Error
Resume
Next
Set
fso = CreateObject(
"Scripting.FileSystemObject"
)
X = 0
T =
True
While
T
Input = InputBox(
"Filename Lowercase Batch Convertor"
& vbCrLf & vbCrLf & _
"Please input the destination folder name. e.g. C:\Webmaster"
& vbCrLf & vbCrLf & _
"Note: Do NOT add '\' in the end of folder name!"
,
"FLowercase Convertor"
,"C:\")
If
Input =
""
Then
MsgBox
"Folder name is empty!"
,48,
"Error!"
T =
True
Else
T =
False
End
If
WEnd
MsgBox
"All files names of "
& Input &
" will be converted to lowercase now..."
,64,
"Note"
fold(Input)
MsgBox
"Done! Total "
& X &
" file(s) were converted to lowercase."
,64,
"Done"
Sub
fold(Path)
Set
f = fso.GetFolder(Path)
Set
rf = fso.GetFolder(Path).files
Set
fc = f.SubFolders
For
Each
fff
In
rf
lcf1 = LCase(fso.GetAbsolutePathName(fff))
fso.MoveFile fff, lcf1
X = X + 1
Next
For
Each
f1
In
fc
fold(f1)
Set
file = fso.GetFolder(f1).files
For
Each
ff
In
file
lcf = LCase(fso.GetAbsolutePathName(ff))
fso.MoveFile ff,lcf
Next
Next
End
Sub
|
VBS代码格式化工具的源码:
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
Option
Explicit
If
WScript.Arguments.Count = 0
Then
MsgBox
"请将要格式化的代码文件拖动到这个文件上"
, vbInformation,
"使用方法"
WScript.Quit
End
If
'作者: Demon
'时间: 2011/12/24
'链接: http://demon.tw/my-work/vbs-beautifier.html
'描述: VBScript 代码格式化工具
'注意:
'1. 错误的 VBScript 代码不能被正确地格式化
'2. 代码中不能含有%[comment]% %[quoted]%等模板标签, 有待改进
'3. 由2可知, 该工具不能格式化自身
Dim
Beautifier, i
Set
Beautifier =
New
VbsBeautifier
For
Each
i
In
WScript.Arguments
Beautifier.BeautifyFile i
Next
MsgBox
"代码格式化完成"
, vbInformation,
"提示"
Class
VbsBeautifier
'VbsBeautifier类
Private
quoted, comments, code, indents
Private
ReservedWord, BuiltInFunction, BuiltInConstants, VersionInfo
'公共方法
'格式化字符串
Public
Function
Beautify(
ByVal
input)
code = input
code = Replace(code, vbCrLf, vbLf)
Call
GetQuoted()
Call
GetComments()
Call
GetErrorHandling()
Call
ColonToNewLine()
Call
FixSpaces()
Call
ReplaceReservedWord()
Call
InsertIndent()
Call
FixIndent()
Call
PutErrorHandling()
Call
PutComments()
Call
PutQuoted()
code = Replace(code, vbLf, vbCrLf)
code = VersionInfo & code
Beautify = code
End
Function
'公共方法
'格式化文件
Public
Function
BeautifyFile(
ByVal
path)
Dim
fso
Set
fso = CreateObject(
"scripting.filesystemobject"
)
BeautifyFile = Beautify(fso.OpenTextFile(path).ReadAll)
'备份文件以免出错
fso.GetFile(path).Copy path &
".bak"
,
True
fso.OpenTextFile(path, 2,
True
).Write(BeautifyFile)
End
Function
Private
Sub
Class_Initialize()
'保留字
ReservedWord =
"And As Boolean ByRef Byte ByVal Call Case Class Const Currency Debug Dim Do Double Each Else ElseIf Empty End EndIf Enum Eqv Event Exit Explicit False For Function Get Goto If Imp Implements In Integer Is Let Like Long Loop LSet Me Mod New Next Not Nothing Null On Option Optional Or ParamArray Preserve Private Property Public RaiseEvent ReDim Rem Resume RSet Select Set Shared Single Static Stop Sub Then To True Type TypeOf Until Variant WEnd While With Xor"
'内置函数
BuiltInFunction =
"Abs Array Asc Atn CBool CByte CCur CDate CDbl CInt CLng CSng CStr Chr Cos CreateObject Date DateAdd DateDiff DatePart DateSerial DateValue Day Escape Eval Exp Filter Fix FormatCurrency FormatDateTime FormatNumber FormatPercent GetLocale GetObject GetRef Hex Hour InStr InStrRev InputBox Int IsArray IsDate IsEmpty IsNull IsNumeric IsObject Join LBound LCase LTrim Left Len LoadPicture Log Mid Minute Month MonthName MsgBox Now Oct Randomize RGB RTrim Replace Right Rnd Round ScriptEngine ScriptEngineBuildVersion ScriptEngineMajorVersion ScriptEngineMinorVersion Second SetLocale Sgn Sin Space Split Sqr StrComp StrReverse String Tan Time TimeSerial TimeValue Timer Trim TypeName UBound UCase Unescape VarType Weekday WeekdayName Year"
'内置常量
BuiltInConstants =
"vbBlack vbRed vbGreen vbYellow vbBlue vbMagenta vbCyan vbWhite vbBinaryCompare vbTextCompare vbSunday vbMonday vbTuesday vbWednesday vbThursday vbFriday vbSaturday vbUseSystemDayOfWeek vbFirstJan1 vbFirstFourDays vbFirstFullWeek vbGeneralDate vbLongDate vbShortDate vbLongTime vbShortTime vbObjectError vbOKOnly vbOKCancel vbAbortRetryIgnore vbYesNoCancel vbYesNo vbRetryCancel vbCritical vbQuestion vbExclamation vbInformation vbDefaultButton1 vbDefaultButton2 vbDefaultButton3 vbDefaultButton4 vbApplicationModal vbSystemModal vbOK vbCancel vbAbort vbRetry vbIgnore vbYes vbNo vbCr vbCrLf vbFormFeed vbLf vbNewLine vbNullChar vbNullString vbTab vbVerticalTab vbUseDefault vbTrue vbFalse vbEmpty vbNull vbInteger vbLong vbSingle vbDouble vbCurrency vbDate vbString vbObject vbError vbBoolean vbVariant vbDataObject vbDecimal vbByte vbArray WScript"
'版本信息
VersionInfo = Chr(39) & Chr(86) & Chr(98) & Chr(115) & Chr(66) & Chr(101) & Chr(97) & Chr(117) & Chr(116) & Chr(105) & Chr(102) & Chr(105) & Chr(101) & Chr(114) & Chr(32) & Chr(49) & Chr(46) & Chr(48) & Chr(32) & Chr(98) & Chr(121) & Chr(32) & Chr(68) & Chr(101) & Chr(109) & Chr(111) & Chr(110) & Chr(13) & Chr(10) & Chr(39) & Chr(104) & Chr(116) & Chr(116) & Chr(112) & Chr(58) & Chr(47) & Chr(47) & Chr(100) & Chr(101) & Chr(109) & Chr(111) & Chr(110) & Chr(46) & Chr(116) & Chr(119) & Chr(13) & Chr(10)
'缩进大小
Set
indents = CreateObject(
"scripting.dictionary"
)
indents(
"if"
) = 1
indents(
"sub"
) = 1
indents(
"function"
) = 1
indents(
"property"
) = 1
indents(
"for"
) = 1
indents(
"while"
) = 1
indents(
"do"
) = 1
indents(
"for"
) = 1
indents(
"select"
) = 1
indents(
"with"
) = 1
indents(
"class"
) = 1
indents(
"end"
) = -1
indents(
"next"
) = -1
indents(
"loop"
) = -1
indents(
"wend"
) = -1
End
Sub
Private
Sub
Class_Terminate()
'什么也不做
End
Sub
'将字符串替换成%[quoted]%
Private
Sub
GetQuoted()
Dim
re
Set
re =
New
RegExp
re.Global =
True
re.Pattern =
""
".*?"
""
Set
quoted = re.Execute(code)
code = re.Replace(code,
"%[quoted]%"
)
End
Sub
'将%[quoted]%替换回字符串
Private
Sub
PutQuoted()
Dim
i
For
Each
i
In
quoted
code = Replace(code,
"%[quoted]%"
, i, 1, 1)
Next
End
Sub
'将注释替换成%[comment]%
Private
Sub
GetComments()
Dim
re
Set
re =
New
RegExp
re.Global =
True
re.Pattern =
"'.*"
Set
comments = re.Execute(code)
code = re.Replace(code,
"%[comment]%"
)
End
Sub
'将%[comment]%替换回注释
Private
Sub
PutComments()
Dim
i
For
Each
i
In
comments
code = Replace(code,
"%[comment]%"
, i, 1, 1)
Next
End
Sub
'将冒号替换成换行
Private
Sub
ColonToNewLine
code = Replace(code,
":"
, vbLf)
End
Sub
'将错误处理语句替换成模板标签
Private
Sub
GetErrorHandling()
Dim
re
Set
re =
New
RegExp
re.Global =
True
re.IgnoreCase =
True
re.Pattern =
"on\s+error\s+resume\s+next"
code = re.Replace(code,
"%[resumenext]%"
)
re.Pattern =
"on\s+error\s+goto\s+0"
code = re.Replace(code,
"%[gotozero]%"
)
End
Sub
'将模板标签替换回错误处理语句
Private
Sub
PutErrorHandling()
code = Replace(code,
"%[resumenext]%"
,
"On Error Resume Next"
)
code = Replace(code,
"%[gotozero]%"
,
"On Error GoTo 0"
)
End
Sub
'格式化空格
Private
Sub
FixSpaces()
Dim
re
Set
re =
New
RegExp
re.Global =
True
re.IgnoreCase =
True
re.MultiLine =
True
'去掉每行前后的空格
re.Pattern =
"^[ \t]*(.*?)[ \t]*$"
code = re.Replace(code,
"$1"
)
'在操作符前后添加空格
re.Pattern =
"[ \t]*(=|<|>|-|\+|&|\*|/|\^|\\)[ \t]*"
code = re.Replace(code,
" $1 "
)
'去掉<>中间的空格
re.Pattern =
"[ \t]*<\s*>[ \t]*"
code = re.Replace(code,
" <> "
)
'去掉<=中间的空格
re.Pattern =
"[ \t]*<\s*=[ \t]*"
code = re.Replace(code,
" <= "
)
'去掉>=中间的空格
re.Pattern =
"[ \t]*>\s*=[ \t]*"
code = re.Replace(code,
" >= "
)
'在行尾的 _ 前面加上空格
re.Pattern =
"[ \t]*_[ \t]*$"
code = re.Replace(code,
" _"
)
'去掉Do While中间多余的空格
re.Pattern =
"[ \t]*Do\s*While[ \t]*"
code = re.Replace(code,
"Do While"
)
'去掉Do Until中间多余的空格
re.Pattern =
"[ \t]*Do\s*Until[ \t]*"
code = re.Replace(code,
"Do Until"
)
'去掉End Sub中间多余的空格
re.Pattern =
"[ \t]*End\s*Sub[ \t]*"
code = re.Replace(code,
"End Sub"
)
'去掉End Function中间多余的空格
re.Pattern =
"[ \t]*End\s*Function[ \t]*"
code = re.Replace(code,
"End Function"
)
'去掉End If中间多余的空格
re.Pattern =
"[ \t]*End\s*If[ \t]*"
code = re.Replace(code,
"End If"
)
'去掉End With中间多余的空格
re.Pattern =
"[ \t]*End\s*With[ \t]*"
code = re.Replace(code,
"End With"
)
'去掉End Select中间多余的空格
re.Pattern =
"[ \t]*End\s*Select[ \t]*"
code = re.Replace(code,
"End Select"
)
'去掉Select Case中间多余的空格
re.Pattern =
"[ \t]*Select\s*Case[ \t]*"
code = re.Replace(code,
"Select Case "
)
End
Sub
'将保留字 内置函数 内置常量 替换成首字母大写
Private
Sub
ReplaceReservedWord()
Dim
re, words, word
Set
re =
New
RegExp
re.Global =
True
re.IgnoreCase =
True
re.MultiLine =
True
words = Split(ReservedWord,
" "
)
For
Each
word
In
words
re.Pattern =
"(\b)"
& word &
"(\b)"
code = re.Replace(code,
"$1"
& word &
"$2"
)
Next
words = Split(BuiltInFunction,
" "
)
For
Each
word
In
words
re.Pattern =
"(\b)"
& word &
"(\b)"
code = re.Replace(code,
"$1"
& word &
"$2"
)
Next
words = Split(BuiltInConstants,
" "
)
For
Each
word
In
words
re.Pattern =
"(\b)"
& word &
"(\b)"
code = re.Replace(code,
"$1"
& word &
"$2"
)
Next
End
Sub
'插入缩进
Private
Sub
InsertIndent()
Dim
lines, line, i, n, t, delta
lines = Split(code, vbLf)
n = UBound(lines)
For
i = 0
To
n
line = lines(i)
SingleLineIfThen line
t = delta
delta = delta + CountDelta(line)
If
t <= delta
Then
lines(i) =
String
(t, vbTab) & lines(i)
Else
lines(i) =
String
(delta, vbTab) & lines(i)
End
If
Next
code = Join(lines, vbLf)
End
Sub
'调整错误的缩进
Private
Sub
FixIndent()
Dim
lines, i, n, re
Set
re =
New
RegExp
re.IgnoreCase =
True
lines = Split(code, vbLf)
n = UBound(lines)
For
i = 0
To
n
re.Pattern =
"^\t*else"
If
re.Test(lines(i))
Then
lines(i) = Replace(lines(i), vbTab,
""
, 1, 1)
End
If
Next
code = Join(lines, vbLf)
End
Sub
'计算缩进大小
Private
Function
CountDelta(
ByRef
line)
Dim
i, re, delta
Set
re =
New
RegExp
re.Global =
True
re.IgnoreCase =
True
For
Each
i
In
indents.Keys
re.Pattern =
"^\s*\b"
& i &
"\b"
If
re.Test(line)
Then
'方便调试
'WScript.Echo line
line = re.Replace(line,
""
)
delta = delta + indents(i)
End
If
Next
CountDelta = delta
End
Function
'处理单行的If Then
Private
Sub
SingleLineIfThen(
ByRef
line)
Dim
re
Set
re =
New
RegExp
re.IgnoreCase =
True
re.Pattern =
"if.*?then.+"
line = re.Replace(line,
""
)
'去掉Private Public前缀
re.Pattern =
"(private|public).+?(sub|function|property)"
line = re.Replace(line,
"$2"
)
End
Sub
End
Class
'Demon, 于2011年平安夜
|
来源:http://demon.tw/my-work/vbs-beautifier.html 。
最后此篇关于用VBS写的VBSCRIPT代码格式化工具VbsBeautifier的文章就讲到这里了,如果你想了解更多关于用VBS写的VBSCRIPT代码格式化工具VbsBeautifier的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在使用下面的脚本来调用另一个脚本。问题是我必须将 WScript.Arguments 检索到的参数传递给我正在调用的第二个脚本。有人可以告诉我该怎么做吗。 Dim objShell Set obj
我正在使用下面的脚本来调用另一个脚本。问题是我必须将我通过 WScript.Arguments 检索的参数传递给我正在调用的第二个脚本。有人可以告诉我该怎么做吗。 Dim objShell Set o
我有一个非常简单的 vbscript 代码: Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Prog
我需要将名称基于日期的文件移动到另一个文件夹。 文件结构为: 来源: \\network_location\folder\Filename_09-11-2012.txt 目的地: C:\Dump\Fi
我有一个关于 VBScript 中变量作用域的问题。我知道有以下关键字(来自 autoitscript.com ): Dim = 局部作用域,如果变量名在全局不存在(在这种情况下它重用全局变量!) G
我正在尝试在 VBScript 中运行以下代码但它没有编译最后一条语句。是不是因为VBScript不允许命名参数? Filename_Argument = WScript.Arguments(0) S
在VBScript中,我需要确保用户输入一个整数。 这是我现在所拥有的: WScript.Echo "Enter an integer number : " Number = WScript.StdI
使用这些信息,我学会了here和here。 我在下面创建了短代码。 当我运行代码并有意识地输入任务计划程序库的“\”文件夹中存在的任务时,我收到“任务不存在” msgbox。 我尝试在传递给函数的变量
我试图仅用VBScript编写一个小的脚本供家庭使用,该脚本在Macrium Reflect中进行预定备份之前运行。 我被困在一个看似很小的问题上,这是在物理断开网络驱动器(即未连接电缆)时的错误处理
我阅读了 Eric Lippert 关于 VBScript 中默认属性语义的文章:http://blogs.msdn.com/b/ericlippert/archive/2005/08/30/4580
我正在尝试使用 VBScript 过滤二维数组,但 VBScript 的内置“过滤器”函数仅适用于单维数组。我使用的是“rs.GetRows()”数组,那么是否有一个可以处理二维数组的简单函数? 编辑
尝试在 HP-UFT 中运行时,下面的 VBScript 代码让我感到困惑,因为第一个语句打印 True 而不是 False (这似乎不合逻辑),而第二个打印 False (这似乎合乎逻辑) 代码:
我刚刚了解到 $ 需要转义字符。 VBScript 中还有哪些特殊字符? 还有一个 bool 函数可以用来判断一个字符是否是特殊字符? 最佳答案 嗯? WScript.Echo "$" 输出 $ 而不
我需要将批处理文件转换为 vbscript,但我对两者都不熟悉。如果我能理解批处理文件中发生的事情,我就可以很容易地计算出 vbscript。问题是批处理文件运行一些 cscript 命令,其语法应该
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
长期以来,人们一直在批处理文件中嵌入和执行 VBScript。但是我看到的所有已发布的解决方案(在最初提出这个问题时)都涉及编写临时 VBS 文件。例如:Embed VBScript inside W
如何检查文件夹中是否存在任意名称的文件?我还想忽略子文件夹。 谢谢。 编辑: 我想我已经明白了,但也感谢任何贡献...... If Folder.Files.Count > 0 Then 'Do
我对编程很陌生,我想制作一个程序,从 Vb 脚本的列表中随机选择一个名称或句子。 这是列表: Jacob James Jason Caleb Ashlee John 程序需要从该列表中选择一个随机名称
如何检查文件夹中是否存在任意名称的文件?我还想忽略子文件夹。 谢谢。 编辑: 我想我已经明白了,但也感谢任何贡献...... If Folder.Files.Count > 0 Then 'Do
我需要一个 Vbscript,它可以从我的 PC 打开一个图像文件,并在几分钟后自动关闭。我计划通过命令提示符运行脚本,非常感谢任何帮助。 最佳答案 使用 HTML Application 可能更容易
我是一名优秀的程序员,十分优秀!