1. Home
  2. Docs
  3. Scense Embedded Scripting...
  4. Scense Scripting Examples
  5. System Settings

System Settings

Sample script for managing system settings.

Add a folder to the Path

This script will intelligently add a new folder to the Path environment variable.
This script uses the ‘Environment’ and the ‘Registry’ services.

'<ScenseScriptHeader>
' <ScriptParameters>
'  <Parameter Name="Operation" ScriptVariableName="vOperation">
'   <GUI Control="DropdownList">
'     <ListElements>
'       <Element Name="Add" />
'       <Element Name="Remove" />
'     </ListElements>
'   </GUI>   
'  </Parameter>
'  <Parameter Name="Folder Name" ScriptVariableName="vPath">
'   <GUI Control="TextBox" />
'  </Parameter>
' </ScriptParameters>
'</ScenseScriptHeader>
'<ParameterCodeBlock>
vOperation = "Add"
vPath = "c:\SomePath"
'</ParameterCodeBlock>vOperation = "Add"
vPath = "c:\SomePath"
'
' *******************************************************
' Description  : Add or remove a folder to/from the user path
' *******************************************************
Sub Scense_Main()
    Dim NewPath
    Dim HKCU
    Dim REGSZ
    Dim HKEY
    
    HKCU  = &H80000001
    HKLM  = &H80000002
    REGSZ = &H00000001

    Scense.WriteScenseLog "Start: " & vOperation & " Folder '" & vPath & "' to PATH (User)"
    If vOperation = "Add" Then
        'ADD a folder to the path
        With Environment
            .EnvironmentType = 1 'User
            .EnvironmentVar = "PATH"
            .EnvironmentVal = Empty
            If .GetEnvironmentVar Then
                Scense.WriteScenseLog "Current PATH: " & .EnvironmentVal
                If InStr(1, .EnvironmentVal, vPath , vbTextCompare) > 0 Then
                    'Already in path
                    Scense.WriteScenseLog "Path was already set..."
                Else
                    'Not in path -> Update current process first
                    .EnvironmentVal = .EnvironmentVal & ";" & vPath 
                    .SetEnvironmentVar

                    'Update the registry
                    'Handle HKCU ---------------------------------------------------
                    If Registry.GetKeyValue(, HKCU, "Environment", "Path") Then
                        If InStr(1, Registry.KeyValue, vPath , vbTextCompare) > 0 Then

                            Scense.WriteScenseLog "Already in registry -> " & "just activate"
                            'Already in registry (CurrentUser) -> just activate
                            Call .ActivateEnvironmentVar
                            Scense.WriteScenseLog "Path was updated..."
                        Else
                            Scense.WriteScenseLog "NOT in registry -> " & "Add and activate"
                            If Not Registry.WriteKeyValue(, HKCU, "Environment", "Path", Registry.KeyValue & ";" & vPath , REGSZ) Then
                                Scense.WriteScenseLog "Unable to write the" & " environment to the registry. (" & Registry.ResultCode & ") " & Registry.Result
                            Else
                                'Activate new environment value
                                Call .ActivateEnvironmentVar
                                Scense.WriteScenseLog "Path was updated..."
                            End If
                        End If
                    Else
                        Scense.WriteScenseLog "NOT in registry -> " & "Create and activate"
                        If Not Registry.WriteKeyValue(, HKCU, "Environment", "Path", vPath , REGSZ) Then
                            Scense.WriteScenseLog "Unable to write the" & " environment to the registry. (" & Registry.ResultCode & ") " & Registry.Result
                        Else
                            'Activate new environment value
                            Call .ActivateEnvironmentVar
                            Scense.WriteScenseLog "Path was updated..."
                        End If
                    End If
                End If
            Else
                Scense.WriteScenseLog "Unable to retrieve the environment" & " variable PATH"
            End If
        End With
        Scense.WriteScenseLog "Stop: Add Folder '" & vPath & "' to PATH"
    Else
        'REMOVE a folder from the path
        With Environment
            .EnvironmentType = 1 'User
            .EnvironmentVar = "PATH"
            .EnvironmentVal = Empty
            If Registry.GetKeyValue(, HKCU, "Environment", "Path") Then
                Scense.WriteScenseLog "Old PATH: " & Registry.KeyValue
                TempArray = Split(Registry.KeyValue, ";")
 
                TempString = ""
                For i = LBound(TempArray) To UBound(TempArray)
                    If StrComp(TempArray(i), vPath , vbTextCompare) <> 0 Then
                        TempString = TempString & TempArray(i) & ";"
                    End If    
                Next
            
                If Len(TempString) > 0 Then
                    'Remove the last ;
                    TempString = Left(TempString, Len(TempString)- 1)
                End If
            
                .EnvironmentVal = TempString
                .SetEnvironmentVar
            
                'Update the registry
                If Registry.GetKeyValue(, HKCU, "Environment", "Path") Then
                    If InStr(1, Registry.KeyValue, vPath , vbTextCompare) = 0 Then
                        Scense.WriteScenseLog "NOT in registry -> Just activate"
                        Call .ActivateEnvironmentVar
                        Scense.WriteScenseLog "Just activate"
                    Else
                        Scense.WriteScenseLog "In registry -> Remove and activate"                      
                        If Not Registry.WriteKeyValue(, HKCU, "Environment", "Path", .EnvironmentVal, REGSZ) Then
                            Scense.WriteScenseLog "Unable to write the" & " environment to the registry. (" & Registry.ResultCode & ") " & _
Registry.Result
                        Else
                            'Activate new environment value
                            Call .ActivateEnvironmentVar
                            Scense.WriteScenseLog "Path was updated: " & vPath & " was removed!"
                        End If
                    End If
                End If            
            Else
                Scense.WriteScenseLog "Path was not set!"
 
            End If
        End With
        Scense.WriteScenseLog "Stop: Remove Folder '" & vPath & "' from PATH"              
        
    End If
End Sub