十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
大学时期也用过VBScript,不过都是基于ASP的,近期因工作需要,尝试在WSH(windows script host)下编程,实现列示oracle client下tnsnames.ora文件的主要信息(TNSname、HOST、SID),大体思路是:判断当前系统下oracle路径,从系统变量中读取具体path,通过Wscript下的文件对象读取文件,分隔path,截取ora文件中的需要信息,下面是具体实现的脚本:
为含山等地区用户提供了全套网页设计制作服务,及含山网站建设行业解决方案。主营业务为成都网站建设、成都网站设计、含山网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
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
效果展示: