大学时期也用过VBScript,不过都是基于ASP的,近期因工作需要,尝试在WSH(windows script host)下编程,实现列示oracle client下tnsnames.ora文件的主要信息(TNSname、HOST、SID),大体思路是:判断当前系统下oracle路径,从系统变量中读取具体path,通过Wscript下的文件对象读取文件,分隔path,截取ora文件中的需要信息,下面是具体实现的脚本:
创新互联建站网站建设公司,提供成都网站设计、
网站制作,网页设计,建网站,PHP网站建设等专业做网站服务;可快速的进行网站开发网页制作和功能扩展;专业做
搜索引擎喜爱的网站,是专业的做网站团队,希望更多企业前来合作!
option explicit
'on error resume next
call Main
Sub Main
dim homes
homes = ReadOracleHome
call ReadTNS(homes)
End Sub
'Date:2014-10-15
'Author:Alan
Function ReadTNS(homes)
dim i,j,k,fs,ts,f,strAll,strTSNname(100),strHost(100),strServiceName(100),contents,content(10),strTotal,txt
set fs =WScript.CreateObject("Scripting.FileSystemObject")
For i=0 to UBound(homes)
If homes(i) <> "" Then
'判断tns文件是否存在
If fs.FileExists(Mid(homes(i),1,Len(homes(i))-3)&"network\admin\tnsnames.ora") = true Then
'读取tnsnames.ora文件
set f=fs.getfile(Mid(homes(i),1,Len(homes(i))-3)&"network\admin\tnsnames.ora")
set ts = f.OpenAsTextStream(1,0)
do while ts.AtEndOfStream <> true
For j=0 to 100
if ts.AtEndOfStream =true then
exit for
End if
txt= ts.ReadLine
If InStr(StrReverse(Trim(txt)),"=")=1 and InStr(Trim(txt),"(")=0 Then
strTSNname(j)=StrReverse(Mid(Trim(StrReverse(txt)),InStr(Trim(StrReverse(txt)),"=")+1))
'msgbox strTSNname(j)
ElseIf InStr(txt,"(HOST =")>0 Then
If InStr(txt,")(PORT =")>0 Then
strHost(j)=Trim(SplitStr(txt,"(HOST =",")(PORT ="))
Else
strHost(j)=Trim(SplitStr(txt,"(HOST =",")"))
End If
'msgbox strHost(j)
ElseIf InStr(txt,"(SERVICE_NAME=")>0 or InStr(txt,"(SID =")>0 Then
IF InStr(txt,"(SERVICE_NAME=")>0 Then
strServiceName(j)=Trim(SplitStr(txt,"(SERVICE_NAME=",")"))
ElseIf InStr(txt,"(SID =")>0 Then
strServiceName(j)=Trim(SplitStr(txt,"(SID =",")"))
End IF
'msgbox strServiceName(j)
End If
'msgbox "TNSName="+strTSNname(j)+" HOST="+strHost(j)+" SERVUCE_NAME/SID="+strServiceName(j)+vbNewLine
Next
Loop
'去除数组中的空值
dim a,b,c,nstrTSNname,nstrHost,nstrServiceName
a=sumArrayNotNullValue(strTSNname)
b=sumArrayNotNullValue(strHost)
c=sumArrayNotNullValue(strServiceName)
nstrTSNname= dropArrayNullValue(strTSNname,maxThree(a,b,c))
nstrHost= dropArrayNullValue(strHost,maxThree(a,b,c))
nstrServiceName= dropArrayNullValue(strServiceName,maxThree(a,b,c))
'组织文本显示效果
For k=0 to maxThree(a,b,c)
IF nstrTSNname(k) <>"" and nstrHost(k)<>"" and nstrServiceName(k)<>"" Then
If contents="" Then
contents="TNSName="+addSpaceToString(getMaxLength(nstrTSNname),nstrTSNname(k))+"HOST="+addSpaceToString(getMaxLength(nstrHost),nstrHost(k))+" SERVUCE_NAME/SID="+nstrServiceName(k)+vbNewLine
'contents="TNSName="+nstrTSNname(k)+" "+"HOST="+nstrHost(k)+" "+"SERVUCE_NAME/SID="+nstrServiceName(k)+" "+vbNewLine
Else
contents=contents+"TNSName="+addSpaceToString(getMaxLength(nstrTSNname),nstrTSNname(k))+"HOST="+addSpaceToString(getMaxLength(nstrHost),nstrHost(k))+" SERVUCE_NAME/SID="+nstrServiceName(k)+vbNewLine
'contents=contents+"TNSName="+nstrTSNname(k)+" "+"HOST="+nstrHost(k)+" "+"SERVUCE_NAME/SID="+nstrServiceName(k) +" "+vbNewLine
End If
End If
Next
'msgbox contents
content(i)=Mid(homes(i),1,Len(homes(i))-3)&"network\admin\tnsnames.ora 中TNS信息如下:" & vbNewLine+contents
'置空本次循环的字符串变量,便于下次循环重新赋值
contents = ""
'关闭对象
set ts=nothing
set f= nothing
Else
MsgBox "在"& Mid(homes(i),1,Len(homes(i))-3)& "network\admin\ 路径下未找到tnsnames.ora文件。" ,,"Information"
End If
End If
Next
set fs = nothing
For i=0 to UBound(content)
IF content(i) <>"" Then
strTotal=strTotal+content(i)+vbNewLine
End IF
Next
call PopupShowStr(strTotal)
End Function
'读取ORACLE_HOME,返回一个homes数组(因为可能有多个oracle_home)
Function ReadOracleHome
dim WshShell,WshSysEnv,path,paths,i,oracle_homes,counter
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshSysEnv=WshShell.Environment("SYSTEM")
path= WshSysEnv("PATH")
'从系统环境变量PATH中分离出ORACLE_HOME
paths = Split(path,";",-1,1)
counter=0
For i=0 to UBound(paths)
If InStr(UCase(paths(i)),"\APP\")>0 Then
If InStr(UCase(paths(i)),"\PRODUCT\")>0 Then
'MsgBox "Found valid path: " & paths(i)
oracle_homes = paths(i)
paths(i)=""
paths(counter)=oracle_homes
counter=counter+1
End If
Else
paths(i)=""
End If
Next
'函数FilterOracleHome的返回值为paths数组
ReadOracleHome = paths
End Function
'定义截取指定字符串之间字符串的函数
Function SplitStr(mainStr,findStartStr,findEndStr)
dim strSplit,intStart,intLength
If InStr(mainStr,findStartStr) >0 Then
intStart=InStr(mainStr,findStartStr)+Len(findStartStr)
intLength= InStr(mainStr,findEndStr) - intStart
strSplit = Mid(mainStr,intStart,intLength)
End If
SplitStr = strSplit
End Function
'定义一个弹出框显示文本内容的函数
Function PopupShowStr(string)
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
'将文本内容显示在弹出框中
call WshShell.Popup(string,0,"TNS Lists:")
End Function
'定义一个去除数组中空值的函数
Function dropArrayNullValue(array,counter)
dim i,j
Redim arrNew(CInt(counter))
'如果array(i)为空,则后面的值往前移
For i=0 to UBound(array)
IF array(i)<>"" Then
arrNew(j)=array(i)
j=j+1
End IF
Next
dropArrayNullValue=arrNew
End Function
'定义计算数组中非空值的个数函数
Function sumArrayNotNullValue(array)
dim i,j
j=0
For i=0 to UBound(array)
IF array(i)<>"" Then
j=j+1
End IF
Next
sumArrayNotNullValue=j
End Function
'求三个数中的大值
Function maxThree(a,b,c)
dim max
max =a
if a>b then
if b>c then
max=a
else
if a>c then
max=a
else
max=c
end if
end if
else
if b>c then
max=b
else
max=c
end if
end if
maxThree=max
End Function
'获取字符串数组中值的大长度
Function getMaxLength(array)
dim i,max
max=Len(Trim(array(0)))
For i=0 to UBound(array)
if Len(Trim(array(i))) > max then
max=Len(Trim(array(i)))
end if
Next
getMaxLength=max
End Function
'统一字符串数组中各值的长度,长度不够补空格
Function addSpaceToString(max,str)
if max-Len(str)>0 then
addSpaceToString=str +String(max-Len(str)," ") ' Space(max-Len(str))
else
addSpaceToString=str
end if
End Function
效果展示:
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章题目:VBScript基于WSH编程-创新互联
分享路径:
http://jkwzsj.com/article/iosph.html