set

環境変数を表示、設定、または削除します。


set

  • help set

Output:

cmd.exe 環境変数を表示、設定、または削除します。

SET [変数名=[文字列]]

  変数名   環境変数名を指定します。
  文字列   変数に割り当てる文字列を指定します。

現在の環境変数を表示するには、パラメーターを指定せずに SET と入力してください。

コマンド拡張機能を有効にすると、SET は、次のように変更されます:

等号や値を指定せずに、変数名だけを指定して SET コマンドを実行すると、
SET コマンドに指定された名前にプレフィックスが一致するすべての変数の値が
表示されます。たとえば、

    SET P

と入力すると、文字 'P' で始まるすべての変数が表示されます。

変数名が現在の環境に見つからない場合は、SET コマンドは、ERRORLEVEL を
1 に設定します。

SET コマンドでは、変数の名前に等符号を使用することはできません。

SET コマンドには、2 つの新しいスイッチが追加されています:

    SET /A 
    SET /P 変数=[プロンプト文字列]

/A スイッチは、等号の右側の文字列が、評価する数式であることを
指定します。式の評価はごく単純で、次の操作がサポートされます。
操作は、優先順位の高い順に示されています:

    ()                  - グループ化
    ! ~ -               - 単項演算子
    * / %              - 算術演算子
    + -                 - 算術演算子
    << >>               - 論理シフト
    &                    - ビット演算子 AND
    ^                   - ビット演算子排他的 OR
    |                   - ビット演算子 OR
    = *= /= %= += -=   - 代入
      &= ^= |= <<= >>=
    ,                   - 式の区切り記号

論理演算子またはモジュール演算子を使う場合は、式文字列を
引用符で囲む必要があります。式内の数値以外の文字列は環境変数文字列として
処理され、使用される前に数値に変換されます。
指定された環境変数名が現在の環境で定義されていない場合は、
値として 0 が使用されます。
これにより、いくつもの % 記号を入力して値を取得しないでも、
環境変数の値を算術演算に使うことができます。
コマンド スクリプト外でコマンド ラインから SET /A を実行すると、
式の最終的な値が表示されます。
割り当て演算子を使うには、割り当て演算子
の左側に環境変数名が必要です。
数値は 10 進数ですが、プレフィックスとして 0x
を付けると 16 進数、0 を付けると 8 進数になります。従って、0x12 は 18、
あるいは 022 と同じです。
8 進表記を使う場合は、注意してください。08 や
09 は、8 と 9 が有効な 8 進数ではないため、
有効な数値ではありません。

/P はユーザーによって入力された入力行を変数の値として設定できるようにします。
入力行を読み取る前に、指定されたプロンプト文字列を表示します。プロンプト文
字列は空でもかまいません。

環境変数の置換は、次のように拡張されます:

    %PATH:文字列 1 = 文字列 2%

は、PATH 環境変数を展開し、その結果に含まれるすべての "文字列 1""文字列 2" に置き換えます。
"文字列 2" に空の文字列を指定すると、展開された出力からすべての "文字列 1"
を削除することができます。
"文字列 1"
をアスタリスクで始め、展開された出力の先頭から、文字列 1 の残りの部分
が最初に現れるまでのすべてを一致させることもできます。

また、展開の副文字列を指定することもできます。

    %PATH:~10,5%

は、PATH 環境変数を展開し、展開結果の 11 番目 (オフセット 10) の文字
から始まる 5 文字だけを使います。長さが指定されなかった場合は、変数の
値の残りの長さを既定値とします。オフセットまたは長さのどちらかが負の値
の場合、環境変数の値の長さに指定されたオフセットまたは長さをたしてその
数を使います。

    %PATH:~-10%

は、パス変数の最後の 10 文字が展開されます。

    %PATH:~0,-2%

は最後の 2 文字以外のすべてが展開されます。

最後に、遅延環境変数の展開が追加されました。このサポートは常に既定で
無効になっていますが、CMD.EXE の /V: のコマンド ライン スイッチを使
って有効または無効にできます。
CMD /? を参照してください。

遅延環境変数の展開は、実行時ではなく、テキスト行を読み取るときに展開
されるという現在の制限を避けるために役立ちます。
次の例は即時変数展開の問題を説明しています。

    set VAR=before
    if "%VAR%" == "before" (
        set VAR=after;
        if "%VAR%" == "after" @echo If you see this, it worked
    )

この例は、論理的には IF 文が別の IF 文の本体に含まれる複合文なので、
両方の IF 文の %VAR% が、最初の IF 文を読み取ったときに展開されます。
このため、メッセージは決して表示されません。
複合文の中の IF では "before""after" が比較され、
決して等しくはなりません。
同様に次の例も期待したようには動作しません。

    set LIST=
    for %i in (*) do set LIST=%LIST% %i
    echo %LIST%

この例では、現在のディレクトリのファイルの一覧は作成されず、代わりに最後
に見つけられたファイルが LIST 変数に設定されます。
これは %LIST% が FOR 文が読み取られるとき、
一度だけ展開され、そのときは LIST 変数が空だからです。
つまり、実際に実行されている FOR ループは

    for %i in (*) do set LIST= %i

で、LIST に最後に見つけられたファイルを設定し続けているだけです。

遅延環境変数の展開では、実行時に環境変数を展開するために異なった文字
(感嘆符) を使うことができます。
遅延環境変数の展開が有効な場合、上記の
例は次のように書くと意図したように動作します。

    set VAR=before
    if "%VAR%" == "before" (
        set VAR=after
        if "!VAR!" == "after" @echo If you see this, it worked
    )

    set LIST=
    for %i in (*) do set LIST=!LIST! %i
    echo %LIST%

コマンド拡張機能が有効な場合、SET によって表示される変数の一覧には
現れないいくつかの動的な環境変数があります。
これらの変数の値は、変数の値が展開されるときに
動的に計算されます。
ユーザーがこれらの名前の変数を明示的に定義する場合、
その定義は下記の動的な定義を無効にします。

%CD%            - 現在のディレクトリ文字列に展開します。

%DATE%          - DATE コマンドと同じフォーマットで現在の日付に展開します。

%TIME%          - TIME コマンドと同じフォーマットで現在の時刻に展開します。

%RANDOM%        - 0 から 32767 の間の任意の 10 進数に展開します。

%ERRORLEVEL%    - 現在の ERRORLEVEL の値に展開します。

%CMDEXTVERSION% - 現在のコマンド プロセッサ拡張機能のバージョン番号に
                    展開します。

%CMDCMDLINE%    - コマンド プロセッサを起動したオリジナル コマンド ライン
                    に展開します。

%HIGHESTNUMANODENUMBER%
                  - このコンピューター上の最大の NUMA ノード番号に展開します。
Displays, sets, or removes cmd.exe environment variables.

SET [variable=[string]]

  variable  Specifies the environment-variable name.
  string    Specifies a series of characters to assign to the variable.

Type SET without parameters to display the current environment variables.

If Command Extensions are enabled SET changes as follows:

SET command invoked with just a variable name, no equal sign or value
will display the value of all variables whose prefix matches the name
given to the SET command.  For example:

    SET P

would display all variables that begin with the letter 'P'

SET command will set the ERRORLEVEL to 1 if the variable name is not
found in the current environment.

SET command will not allow an equal sign to be part of the name of
a variable.

Two new switches have been added to the SET command:

    SET /A expression
    SET /P variable=[promptString]

The /A switch specifies that the string to the right of the equal sign
is a numerical expression that is evaluated.  The expression evaluator
is pretty simple and supports the following operations, in decreasing
order of precedence:

    ()                  - grouping
    ! ~ -               - unary operators
    * / %               - arithmetic operators
    + -                 - arithmetic operators
    << >>               - logical shift
    &                   - bitwise and
    ^                   - bitwise exclusive or
    |                   - bitwise or
    = *= /= %= += -=    - assignment
      &= ^= |= <<= >>=
    ,                   - expression separator

If you use any of the logical or modulus operators, you will need to
enclose the expression string in quotes.  Any non-numeric strings in the
expression are treated as environment variable names whose values are
converted to numbers before using them.  If an environment variable name
is specified but is not defined in the current environment, then a value
of zero is used.  This allows you to do arithmetic with environment
variable values without having to type all those % signs to get their
values.  If SET /A is executed from the command line outside of a
command script, then it displays the final value of the expression.  The
assignment operator requires an environment variable name to the left of
the assignment operator.  Numeric values are decimal numbers, unless
prefixed by 0x for hexadecimal numbers, and 0 for octal numbers.
So 0x12 is the same as 18 is the same as 022. Please note that the octal
notation can be confusing: 08 and 09 are not valid numbers because 8 and
9 are not valid octal digits.

The /P switch allows you to set the value of a variable to a line of input
entered by the user.  Displays the specified promptString before reading
the line of input.  The promptString can be empty.

Environment variable substitution has been enhanced as follows:

    %PATH:str1=str2%

would expand the PATH environment variable, substituting each occurrence
of "str1" in the expanded result with "str2".  "str2" can be the empty
string to effectively delete all occurrences of "str1" from the expanded
output.  "str1" can begin with an asterisk, in which case it will match
everything from the beginning of the expanded output to the first
occurrence of the remaining portion of str1.

May also specify substrings for an expansion.

    %PATH:~10,5%

would expand the PATH environment variable, and then use only the 5
characters that begin at the 11th (offset 10) character of the expanded
result.  If the length is not specified, then it defaults to the
remainder of the variable value.  If either number (offset or length) is
negative, then the number used is the length of the environment variable
value added to the offset or length specified.

    %PATH:~-10%

would extract the last 10 characters of the PATH variable.

    %PATH:~0,-2%

would extract all but the last 2 characters of the PATH variable.

Finally, support for delayed environment variable expansion has been
added.  This support is always disabled by default, but may be
enabled/disabled via the /V command line switch to CMD.EXE.  See CMD /?

Delayed environment variable expansion is useful for getting around
the limitations of the current expansion which happens when a line
of text is read, not when it is executed.  The following example
demonstrates the problem with immediate variable expansion:

    set VAR=before
    if "%VAR%" == "before" (
        set VAR=after
        if "%VAR%" == "after" @echo If you see this, it worked
    )

would never display the message, since the %VAR% in BOTH IF statements
is substituted when the first IF statement is read, since it logically
includes the body of the IF, which is a compound statement.  So the
IF inside the compound statement is really comparing "before" with
"after" which will never be equal.  Similarly, the following example
will not work as expected:

    set LIST=
    for %i in (*) do set LIST=%LIST% %i
    echo %LIST%

in that it will NOT build up a list of files in the current directory,
but instead will just set the LIST variable to the last file found.
Again, this is because the %LIST% is expanded just once when the
FOR statement is read, and at that time the LIST variable is empty.
So the actual FOR loop we are executing is:

    for %i in (*) do set LIST= %i

which just keeps setting LIST to the last file found.

Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time.  If delayed variable expansion is enabled, the above
examples could be written as follows to work as intended:

    set VAR=before
    if "%VAR%" == "before" (
        set VAR=after
        if "!VAR!" == "after" @echo If you see this, it worked
    )

    set LIST=
    for %i in (*) do set LIST=!LIST! %i
    echo %LIST%

If Command Extensions are enabled, then there are several dynamic
environment variables that can be expanded but which don't show up in
the list of variables displayed by SET.  These variable values are
computed dynamically each time the value of the variable is expanded.
If the user explicitly defines a variable with one of these names, then
that definition will override the dynamic one described below:

%CD% - expands to the current directory string.

%DATE% - expands to current date using same format as DATE command.

%TIME% - expands to current time using same format as TIME command.

%RANDOM% - expands to a random decimal number between 0 and 32767.

%ERRORLEVEL% - expands to the current ERRORLEVEL value

%CMDEXTVERSION% - expands to the current Command Processor Extensions
    version number.

%CMDCMDLINE% - expands to the original command line that invoked the
    Command Processor.

%HIGHESTNUMANODENUMBER% - expands to the highest NUMA node number
    on this machine.

Return Code: 1


Built with Hugo
テーマ StackJimmy によって設計されています。