当前位置: 洪哥笔记 > ASP > VBScript常用操作

 

VBScript常用操作


关键词

VBScript常用操作

摘要

VBScript常用操作

     

1、获取错误信息和显示错误信息

On Error Resume Next        '遇到错误不显示,继续执行
On Error Goto 0             '可以取消上面这一句的效果,恢复平常的一有错误就报并停止脚本

在设置了On Error Resume Next后遇到错误会保存在Err中。Err本身的值为错误号,也就是相当于Err.Number。初始时,也就是没有错误时值为0。

Err.Description属性         '显示错误的简短说明
Err.Number属性              '显示错误代码
Err.Clear方法               '清空错误
Err.Raise方法               '产生一个错误

一般方法为
On Error Resume Next
a = 100/0
If Err Then Msgbox Err.Description

-------------------------------------------------------

    2、随机产生一个N位的密码,密码值在str中选择

n = 20
str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
password = ""
Randomize
Do While Len(password)<n
password = password & Mid(str,Int(Rnd*Len(str))+1,1)
Loop

-------------------------------------------------------

3、获得一个文件所在的路径

filePath = "D:\wwwroot\test.asp"
folderPath = Mid(filePath,1,InStrRev(filePath,"\")-1)        'InStrRev为返回某个字符串在另一个字符串中最后出现的位置
MsgBox folderPath

-------------------------------------------------------

4、查看str2在str1中出现了几次

方法1.
str1 = "wjfwftestwe9r234testssdf9j"
str2 = "test"
Set rex = new RegExp
rex.global = True
rex.IgnoreCase = True
rex.Pattern = str2
Set matches = rex.Execute(str1)
MsgBox matches.count
Set matches = Nothing

方法2.
str1 = "wjfwftestwe9r234testssdf9j"
str2 = "test"
len1 = Len(str1)
len2 = Len(str2)
str1 = Replace(str1,str2,"")
MsgBox (len1 - Len(str1)) / len2

方法3.
str1 = "wjfwftestwe9r234testssdf9j"
str2 = "test"
arr = Split(str1,str2)
MsgBox UBound(arr)

-------------------------------------------------------

5、去除文件中重复行

'这里使用adodb将文本文件当成数据库处理,使用select distinct来得到不重复记录.注意,Data Source这里写文件所在路径,Select From这里写文件名称
inputfileName = "test.txt"
outputfileName = "result.txt"
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001
Set conn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
filePath = Left(WScript.ScriptFullName, InstrRev(WScript.ScriptFullName, "\"))
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filePath & ";Extended Properties=""Text;HDR=NO;FMT=Delimited"""
'参数见说明http://www.connectionstrings.com/textfile,其中HDR=Yes表示第一行作为列名,=NO时不作为列名
rs.Open "Select DISTINCT * FROM " & inputfileName, conn, adOpenStatic, adLockOptimistic, adCmdText
Set Fso = CreateObject("Scripting.FileSystemObject")
Set output = Fso.CreateTextFile(outputfileName)
Do While Not rs.EOF
    output.writeline rs.Fields.Item(0).Value  
    rs.MoveNext
Loop
rs.close
conn.close
output.close
MsgBox "去重复行的结果在 " & outputfileName & " 文件中"

 

要饭二维码

洪哥写文章很苦逼,如果本文对您略有帮助,可以扫描下方二维码支持洪哥!金额随意,先行谢过!大家的支持是我前进的动力!

文章的版权

本文属于“洪哥笔记”原创文章,转载请注明来源地址:VBScript常用操作:http://www.splaybow.com/post/vbscript2.html

如果您在服务器运维、网络管理、网站或系统开发过程有需要提供收费服务,请加QQ:8771947!十年运维经验,帮您省钱、让您放心!
亲,如果有需要,先存起来,方便以后再看啊!加入收藏夹的话,按Ctrl+D

« VBScript常用函数 VBScript之调用命令行 »

相关文章: