Jump to content

Persistent Variables With Ini File


Remco

Recommended Posts

  • Most Valued User

We see often that users want to store and retrieve settings via the ECR scripting. The solution given is often Persistent variables from the forum. But thiis solution requires database access. Users do not always have the needed database access. We do not provide our dartabase servers for such perposes as a Hosted Swyx provider.

We provide an alternative that always works and is much easyer to implement, with the use of INI files for storage:

Function ReadVar(myKey, myDefaultValue)

Function WriteVar(myKey, myValue)


How to use:

Dim Tel
Tel = ReadVar ("Vraagje", "133")
PBXScript.OutputTrace "--> Tel=" & Tel

WriteVar "Tel", Tel
PBXScript.OutputTrace "--> Tel=" & Tel

Tel = ReadVar("Tel", Tel)
PBXScript.OutputTrace "--> Tel=" & Tel

 

Add to Start block of your script:

 
'All vars used in the functions:
Const ForReading   = 1
Const ForWriting   = 2
Const ForAppending = 8

'-------------------------------------------------------------------
'  Function to Read a Named value from "INI-File"   
'   
'  Read "DestinationClosed"   
'  Result is "" when not exists!   
'   
'  File will be written in %APPDATA% filename ( C:\Users\[ADNAME]\AppData\Roaming\VARIABLES.INI )   
'  Written by Remco Eikhout   
'  Copywrite Intratel BV - Hollland   
'  support@intratel.nl    
'-------------------------------------------------------------------
Function ReadVar (myKey, myDefaultValue)   

  'All vars used in the functions:    
  Dim intEqualPos    
  Dim objFSO    
  Dim objIniFile    
  Dim wshShell    
  Dim strFilePath    
  Dim strKey    
  Dim strLeftString    
  Dim strLine    
  Dim strSection    
  Dim strValue    
  Dim blnInSection    
  Dim blnKeyExists    
  Dim blnSectionExists    

  On Error Resume Next    
  Err.Clear    

  Set objFSO = CreateObject( "Scripting.FileSystemObject" )    
  PBXScript.OutputTrace  "ReadVar - " & "FileSystemObject Created"    

  Set wshShell = CreateObject("WScript.Shell")    
  PBXScript.OutputTrace  "ReadVar - " & "ShellObject Created"    
  strFilePath = wshShell.ExpandEnvironmentStrings("%APPDATA%") & "\VARIABLES.INI"    
  set wshShell = Nothing    
  If Err<>0 Then        
    PBXScript.OutputTrace  "ReadVar - " & "*Err - Error " & Err.Description    
  Else        
    PBXScript.OutputTrace  "ReadVar - " & "INI filename=" & strFilePath       
  End If    

  ReadVar     = myDefaultValue    
  strSection  = "Variables"    
  strKey      = Trim( myKey )    

  If objFSO.FileExists( strFilePath ) Then        
    Set objIniFile = objFSO.OpenTextFile( strFilePath, ForReading, False )        
    PBXScript.OutputTrace  "ReadVar - " & "INI object created"        

    Do While objIniFile.AtEndOfStream = False            

      strLine = Trim( objIniFile.ReadLine )            
      If Err<>0 Then                
        PBXScript.OutputTrace  "ReadVar - " & "*Err - Error in section loop:" & Err.Description
        Exit Do            
      End if            

      ' Check if section is found in the current line            
      If LCase( strLine ) = "[" & LCase( strSection ) & "]" Then                
        PBXScript.OutputTrace  "ReadVar - " & "Section found!"                
        strLine = Trim( objIniFile.ReadLine )                

        ' Parse lines until the next section is reached                
        Do While Left( strLine, 1 ) <> "["                    
          ' Find position of equal sign in the line                    
          intEqualPos = InStr( 1, strLine, "=", 1 )                    

          If Err<>0 Then                        
            PBXScript.OutputTrace  "ReadVar - " & "*Err - Error in values loop:" & Err.Description
            Exit Do                    
          End if                    

          If intEqualPos > 0 Then                        
            strLeftString = Trim( Left( strLine, intEqualPos - 1 ) )

            ' Check if item is found in the current line
            If LCase( strLeftString ) = LCase( strKey ) Then
              PBXScript.OutputTrace  "ReadVar - Value found!"                            
              ReadVar = Trim( Mid( strLine, intEqualPos + 1 ) )                            
              PBXScript.OutputTrace  "ReadVar - " & "Value=" & ReadVar 
              ' In case the item exists but value is blank
              'If ReadVar = "" Then                            
              '    ReadVar = "0"                            
              'End If                            
              ' Abort loop when item is found                            
              Exit Do                        
            End If                    

          End If                    

          ' Abort if the end of the INI file is reached                    
          If objIniFile.AtEndOfStream Then Exit Do                    
          ' Continue with next line                    
          strLine = Trim( objIniFile.ReadLine )                
        Loop                

        Exit Do            

      Else                
        PBXScript.OutputTrace  "ReadVar - " & "Not correct section yet..."            
      End If        

    Loop        
  
    objIniFile.Close    
  
  Else        
    PBXScript.OutputTrace  "ReadVar - " & "File does not exist : " & strFilePath        
    PBXScript.OutputTrace  "ReadVar - " & "-> Use default value!"        
  End If    

  PBXScript.OutputTrace  "ReadVar - " & "Done!"    
  Set objIniFile = Nothing

End Function

'-------------------------------------------------------------------
'Function to Write a Named value from "INI-File"    
'    
'  Write "DestinationClosed", "100"    
'    
'  File will be written in %APPDATA% filename ( C:\Users\[ADNAME]\AppData\Roaming\VARIABLES.INI )    
'  Written by Remco Eikhout    
'  Copywrite Intratel BV - Hollland    
'  support@intratel.nl    
'-------------------------------------------------------------------
Function WriteVar(myKey, myValue)    

  Dim intEqualPos    
  Dim objFSO    
  Dim objIniFile    
  Dim objOrgIni    
  Dim objNewIni    
  Dim wshShell    
  Dim strFilePath    
  Dim strKey    
  Dim strLeftString    
  Dim strLine    
  Dim strSection    
  Dim strTempDir    
  Dim strTempFile    
  Dim strValue    
  Dim blnInSection    
  Dim blnKeyExists    
  Dim blnSectionExists    
  Dim blnWritten    

  On Error Resume Next    
  Err.Clear    
  
  PBXScript.OutputTrace  "WriteVar - " & "Started"    
  strSection  = "Variables"    
  strKey      = Trim( myKey )    
  strValue    = Trim( myValue )    
  if strKey = "" then        
    strKey = "<DELETE_THIS_VALUE>"    
  end if    
  
  Set objFSO   = CreateObject( "Scripting.FileSystemObject" )    
  PBXScript.OutputTrace  "WriteVar - " & "FileSystem Object created"    
  
  Set wshShell = CreateObject( "WScript.Shell" )    
  PBXScript.OutputTrace  "WriteVar - " & "Shell Object created"    
  strFilePath = wshShell.ExpandEnvironmentStrings("%APPDATA%") & "\VARIABLES.INI"    
  strTempDir  = wshShell.ExpandEnvironmentStrings( "%TEMP%" )    
  If Err<>0 Then        
    PBXScript.OutputTrace  "WriteVar - " & "*Err - Error " & Err.Description    
  Else        
    PBXScript.OutputTrace  "WriteVar - " & "INI filename=" & strFilePath       
  End If    
  
  strTempFile = objFSO.BuildPath( strTempDir, objFSO.GetTempName )    
  Set objOrgIni = objFSO.OpenTextFile( strFilePath, ForReading, True )    
  Set objNewIni = objFSO.CreateTextFile( strTempFile, False, False )    
  If Err<>0 Then        
    PBXScript.OutputTrace  "WriteVar - " & "*Err - Error " & Err.Description    
  Else        
    PBXScript.OutputTrace  "WriteVar - " & "All objects created"    
  End If    
  
  blnInSection     = False    
  blnSectionExists = False    
  
  ' Check if the specified key already exists    
  blnKeyExists     = ( ReadVar( strKey ) <> "" )    
  blnWritten       = False    
  
  While Err=0 and objOrgIni.AtEndOfStream = False        

    strLine = Trim( objOrgIni.ReadLine )        
    If blnWritten = False Then            
      If LCase( strLine ) = "[" & LCase( strSection ) & "]" Then
        blnSectionExists = True                
        blnInSection = True            
      ElseIf InStr( strLine, "[" ) = 1 Then                
        blnInSection = False            
      End If        
    End If        

    If blnInSection Then            
      If blnKeyExists Then                
        intEqualPos = InStr( 1, strLine, "=", vbTextCompare )
        If intEqualPos > 0 Then                    
          strLeftString = Trim( Left( strLine, intEqualPos - 1 ) )
          If LCase( strLeftString ) = LCase( strKey ) then
            ' Only write the key if the value isnt empty
            ' Modification by Johan Pol
            If strValue <> "<DELETE_THIS_VALUE>" Then
              PBXScript.OutputTrace  "WriteVar - Write new Value " & strValue
              objNewIni.WriteLine strKey & "=" & strValue
            End If                        
            blnWritten   = True                        
            blnInSection = False                    
          End If                
        End If                

        If Not blnWritten Then                    
          objNewIni.WriteLine strLine                
        End If            

      Else                
        objNewIni.WriteLine strLine                
        ' Only write the key if the value isnt empty                
        ' Modification by Johan Pol                
        If strValue <> "<DELETE_THIS_VALUE>" Then                    
          objNewIni.WriteLine strKey & "=" & strValue               
        End If                
        blnWritten   = True                
        blnInSection = False            
      End If          

    Else            
      objNewIni.WriteLine strLine        
    End If    

  Wend    

  If blnSectionExists = False Then 
    ' section doesnt exist        
    PBXScript.OutputTrace  "WriteVar - Write new section"        
    objNewIni.WriteLine        
    objNewIni.WriteLine "[" & strSection & "]"        
    ' Only write the key if the value isnt empty        
    ' Modification by Johan Pol        
    If strValue <> "<DELETE_THIS_VALUE>" Then            
      PBXScript.OutputTrace  "WriteVar - Write new value"            
      objNewIni.WriteLine strKey & "=" & strValue        
    End If    
  End If    
  
  objOrgIni.Close    
  objNewIni.Close    
  
  ' Delete old INI file    
  objFSO.DeleteFile strFilePath, True    
  ' Rename new INI file    
  objFSO.MoveFile strTempFile, strFilePath    
  Set objOrgIni = Nothing    
  Set objNewIni = Nothing    
  Set objFSO    = Nothing    
  Set wshShell  = Nothing
  
End Function

 

PersVarsINI.zip

 

 
Link to comment
Share on other sites


Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and have taken note of our Privacy Policy.
We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.