VBS: Check an INI File For A Value And Doing Things Based On What It Says

Discussion in 'Software' started by Heaney, Sep 23, 2011.

  1. Heaney

    Heaney Private E-2

    So I have an INI file like this:

    Code:
    Version=
    
    With a number after the =.

    Basically, in vbs can I do this:

    • If Version is > or = 181 Then Run Some VBS
    • If Version is < 181 or Version.ini doesn't even exist, Run Some Different VBS

    ?

    Oh, and the script I want to run for the second bit contains an if function.

    Just to make sure, you CAN have an if function inside an if function, right?
     
  2. GermanOne

    GermanOne Guest

    Nested If-statements are possible.

    Have a look at how ini files could look like.
    Are there different sections? (in this case the ini file could contain more than one Version-value)

    Regards
    GermanOne
     
  3. Heaney

    Heaney Private E-2

    I made the ini file, it's JUST version =
    Nothing else.
     
  4. GermanOne

    GermanOne Guest

    Try something like that:
    Code:
    Const strINIFullName = "C:\wherever\file.ini"
    Const strName = "Version"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists(strINIFullName) Then
      Set objRegEx = New RegExp
      objRegEx.IgnoreCase = False
      objRegEx.Pattern = "(^|\n)\b" & strName & "=\d+\b\r*"
      Set objFile = objFSO.OpenTextFile(strINIFullName, 1)
      Set colMatches = objRegEx.Execute(objFile.ReadAll)
      For Each objMatch In colMatches
        strLine = objMatch.Value
      Next
      objFile.Close
      Set objFile = Nothing
      Set colMatches = Nothing
      objRegEx.Pattern = "\d+"
      Set colMatches = objRegEx.Execute(strLine)
      For Each objMatch In colMatches
        iVer = CInt(objMatch.Value)
      Next
      Set colMatches = Nothing
      Set objRegEx = Nothing
    End If
    Set objFSO = Nothing
    
    Set objWSH = CreateObject("WScript.Shell")
    If iVer > 180 Then
      objWSH.Run "aaa.vbs"
    Else
      objWSH.Run "bbb.vbs"
    End If
    Set objWSH = Nothing
    
    Regards
    GermanOne
     
  5. Heaney

    Heaney Private E-2

    I'll try this tomorrow!
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds