본문 바로가기
Windows Script Host

WshEnvironment 개체

2023. 8. 17.

이 글은 도움말을 번역한 글입니다.

WshEnvironment 개체

Windows 환경 변수의 컬렉션에 대한 액세스를 제공합니다.

속성

Item Property

컬렉션에서 지정된 항목을 노출합니다.

Syntax

Object.Item(natIndex)

object : WshShell개체의 Environment 속성에서 반환되는 개체입니다.

natIndex : 검색할 항목입니다.

다음 코드는 특정 환경 변수의 값을 출력합니다.

Sub GetEnvironmentVariableValueWithWshShell()
    Dim variableName As String
    Dim variableValue As String
    
    variableName = "변수명" ' 가져올 환경 변수의 이름
    variableValue = GetEnvironmentVariableValue(variableName)
    
    Debug.Print variableName & " = " & variableValue
End Sub

Function GetEnvironmentVariableValue(variableName As String) As String
    Dim wshShell As Object
    Dim wshEnv As Object
    
    Set wshShell = CreateObject("WScript.Shell")
    Set wshEnv = wshShell.Environment("Process")
    
    GetEnvironmentVariableValue = wshEnv.item(variableName)
    
    Set wshEnv = Nothing
    Set wshShell = Nothing
End Function

Length Property

로컬 컴퓨터 시스템에서의 Windows 환경 변수의 수(Environment 컬렉션에 있는 항목의 수)를 반환합니다.

Syntax

object.length

object : WshEnvironment 개체입니다.

다음 코드는 환경 변수의 개수를 출력합니다.

Sub GetEnvironmentVariableCounts()
    Dim processVariableCount As Integer
    Dim systemVariableCount As Integer
    Dim userVariableCount As Integer

    processVariableCount = GetVariableCount("Process")
    systemVariableCount = GetVariableCount("System")
    userVariableCount = GetVariableCount("User")

    MsgBox "프로세스 환경 변수 개수: " & processVariableCount & vbCrLf & _
           "시스템 환경 변수 개수: " & systemVariableCount & vbCrLf & _
           "사용자 환경 변수 개수: " & userVariableCount
End Sub

Function GetVariableCount(ByVal category As String) As Integer
    Dim wshShell As Object
    Dim wshEnv As Object
    Dim variableCount As Integer

    Set wshShell = CreateObject("WScript.Shell")
    Set wshEnv = wshShell.Environment(category)

    variableCount = wshEnv.Length

    Set wshEnv = Nothing
    Set wshShell = Nothing

    GetVariableCount = variableCount
End Function

메서드

Count Method

로컬 컴퓨터 시스템에서의 Windows 환경 변수의 수(Environment 컬렉션에 있는 항목의 수)를 반환합니다.

Syntax

object.Count

object : WshEnvironment 개체입니다.

다음 코드는 환경 변수의 개수를 출력합니다.

Sub GetEnvironmentVariableCounts()
    Dim processVariableCount As Integer
    Dim systemVariableCount As Integer
    Dim userVariableCount As Integer

    processVariableCount = GetVariableCount("Process")
    systemVariableCount = GetVariableCount("System")
    userVariableCount = GetVariableCount("User")

    MsgBox "프로세스 환경 변수 개수: " & processVariableCount & vbCrLf & _
           "시스템 환경 변수 개수: " & systemVariableCount & vbCrLf & _
           "사용자 환경 변수 개수: " & userVariableCount
End Sub

Function GetVariableCount(ByVal category As String) As Integer
    Dim wshShell As Object
    Dim wshEnv As Object
    Dim variableCount As Integer

    Set wshShell = CreateObject("WScript.Shell")
    Set wshEnv = wshShell.Environment(category)

    variableCount = wshEnv.Count

    Set wshEnv = Nothing
    Set wshShell = Nothing

    GetVariableCount = variableCount
End Function

이 코드는 Length 속성 대신 Count 메서드로 바꾸었습니다.결과는 같습니다.

Remove Method

기존 환경 변수를 제거합니다.

Syntax

object.Remove(strName)

object : WshEnvironment 개체입니다.

strName : 제거할 환경 변수의 이름을 나타내는 문자열 값입니다.

주의

Remove 메서드는 PROCESS, USER, SYSTEM 및 VOLATILE 환경에서 환경 변수를 제거합니다. Remove 메서드로 제거된 환경 변수는 영구적으로 제거되지 않습니다. 현재 세션에 대해서만 제거됩니다.

다음 코드는 환경 변수를 생성한 다음 제거하는 예를 보여줍니다.

Sub CreateAndRemoveEnvironmentVariable()
    Dim variableName As String
    Dim variableValue As String
    variableName = "MY_NEW_VARIABLE"
    variableValue = "New Variable Value"
    
    ' 환경 변수 생성
    CreateEnvironmentVariable variableName, variableValue
    MsgBox "새로운 환경 변수 " & variableName & "이(가) 생성되었습니다."
    
    ' 환경 변수 제거
    RemoveEnvironmentVariable variableName
    MsgBox "환경 변수 " & variableName & "이(가) 제거되었습니다."
End Sub

Sub CreateEnvironmentVariable(variableName As String, variableValue As String)
    Dim wshShell As Object
    Set wshShell = CreateObject("WScript.Shell")
    
    Dim env As Object
    Set env = wshShell.Environment("User")
    
    ' 환경 변수 생성
    env(variableName) = variableValue
    
    Set env = Nothing
    Set wshShell = Nothing
End Sub

Sub RemoveEnvironmentVariable(variableName As String)
    Dim wshShell As Object
    Set wshShell = CreateObject("WScript.Shell")
    
    Dim env As Object
    Set env = wshShell.Environment("User")
    
    ' 환경 변수 제거
    env.Remove (variableName)
    
    Set env = Nothing
    Set wshShell = Nothing
End Sub

'Windows Script Host' 카테고리의 다른 글

WshShortcut Object  (0) 2023.08.29
WshNetwork 개체  (0) 2023.08.17
WshShell 개체  (0) 2023.08.15