您好,登錄后才能下訂單哦!
大學時期也用過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
效果展示:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。