vbs读取ini文件中特定section下某keyName的value值

本人刚刚学习VBS,记录学习过程中遇到的问题及其解决方法(网搜or自写),希望能帮助到其他伙伴。

REM getOperationType
REM iniPath is the path of ini file that you want to read
REM sectionName and keyName which are the keyName you want to find in the sectionName
Function getOperationType(iniPath , sectionName ,keyName)
  Const ForReading = 1
  Dim fso , objFile ,textLine , tmpArray
  Dim operationType : operationType = "Not Found"
  Dim status:status = 0
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set objFile = fso.OpenTextFile(iniPath , ForReading)

  REM AtEndOfStream is available for only-read TextFile
  Do Until objFile.AtEndOfStream
    REM Every execution,textFile.readline reads the new line
    textLine = objFile.readline
    REM status indicates you find the sectionName, then begin to find the keyName under it
    If InStr(textLine , sectionName) <> 0 And InStr(textLine , "[") And InStr(textLine , "]") <> 0 Then
      status = 1
    End If
    If status = 1 Then
      If InStr(textLine , keyName) <> 0 Then
        tmpArray = Split(textLine , "=" , -1 , 1)
        operationType = Lcase(Trim(tmpArray(1)))
        Exit Do
      End If
    End If
  Loop
  objFile.close
  getOperationType = operationType
  Set objFile = Nothing
  Set fso = Nothing
End Function

 

posted @ 2014-04-18 13:33  huihui1989  阅读(486)  评论(0编辑  收藏  举报