次の方法で共有


set (環境変数)

環境変数 cmd.exe 表示、設定、または削除します。 If used without parameters, set displays the current environment variable settings.

Note

このコマンドには、既定で有効になっているコマンド拡張機能が必要です。

The set command can also run from the Windows Recovery Console, using different parameters. 詳細については、「Windows Recovery Environment (WinRE)」を参照してください。

Syntax

set [<variable>=[<string>]]
set [/p] <variable>=[<promptString>]
set /a <variable>=<expression>

Parameters

Parameter Description
<variable> 設定または変更する環境変数を指定します。
<string> 指定した環境変数に関連付ける文字列を指定します。
/p <variable>の値を、ユーザーが入力した入力行に設定します。
<promptstring> ユーザーに入力を求めるメッセージを指定します。 This parameter must be used with the /p parameter.
/a 評価される数値式に <string> を設定します。
<expression> 数値式を指定します。
/? コマンド プロンプトにヘルプを表示します。

Remarks

  • If command extensions are enabled (the default) and you run set with a value, it displays all of the variables that begin with that value.

  • 文字 <>|&、および ^ は特殊なコマンド シェル文字であり、エスケープ文字 (^) の前に付けるか、 <string> で使用する場合は引用符で囲む必要があります (例: "StringContaining&Symbol")。 いずれかの特殊文字を含む文字列を引用符で囲む場合、引用符は環境変数値の一部として設定されます。

  • 環境変数を使用して、一部のバッチ ファイルとプログラムの動作を制御し、Windows と MS-DOS サブシステムの表示方法と動作方法を制御します。 The set command is often used in the Autoexec.nt file to set environment variables.

  • If you use the set command without any parameters, the current environment settings are displayed. These settings usually include the COMSPEC and PATH environment variables, which are used to help find programs on disk. Two other environment variables used by Windows are PROMPT and DIRCMD.

  • <variable><string>の値を指定すると、指定した<variable>値が環境に追加され、<string>はその変数に関連付けられます。 変数が環境内に既に存在する場合は、新しい文字列値によって古い文字列値が置き換えられます。

  • If you specify only a variable and an equal sign (without <string>) for the set command, the <string> value associated with the variable is cleared (as if the variable isn't there).

  • If you use the /a parameter, the following operators are supported, in descending order of precedence:

    Operator Operation performed
    ( ) Grouping
    ! ~ - Unary
    * / % Arithmetic
    + - Arithmetic
    << >> Logical shift
    & Bitwise AND
    ^ ビット演算子排他的 OR
    = *= /= %= += -= &= ^= = <<= >>=
    , Expression separator
  • 論理 (&& または ||) 演算子または剰余 (%) 演算子を使用する場合は、式の文字列を引用符で囲みます。 式内の数値以外の文字列は環境変数の名前と見なされ、それらの値は処理される前に数値に変換されます。 現在の環境で定義されていない環境変数の名前を指定すると、値 0 が割り当てられるため、% を使用せずに環境変数の値を使用して算術演算を実行して値を取得できます。

  • If you run set /a from the command line outside of a command script, it displays the final value of the expression.

  • 数値は、16 進数の場合は 0×、8 進数の場合は 0 で始まる場合を除き、10 進数です。 したがって、0×12 は 18 と同じであり、これは 022 と同じです。

  • Delayed environment variable expansion support is disabled by default, but you can enable or disable it by using cmd /v.

  • When creating batch files, you can use set to create variables, and then use them in the same way that you would use the numbered variables %0 through %9. You can also use the variables %0 through %9 as input for set.

  • バッチ ファイルから変数値を呼び出す場合は、値をパーセント記号 (%) で囲みます。 For example, if your batch program creates an environment variable named BAUD, you can use the string associated with BAUD as a replaceable parameter by typing %baud% at the command prompt.

Examples

To set the value TEST^1 for the environment variable named testVar, type:

set testVar=TEST^^1

The set command assigns everything that follows the equal sign (=) to the value of the variable. したがって、「 set testVar=TEST^1」と入力すると、 testVar=TEST1次の結果が得られます。

To set the value TEST&1 for the environment variable testVar, type:

set testVar=TEST^&1

To set an environment variable named include so the string c:\directory is associated with it, type:

set include=c:\directory

You can then use the string c:\directory in batch files by enclosing the name include with percent signs (%). For example, you can use dir %include% in a batch file to display the contents of the directory associated with the include environment variable. After this command is processed, the string c:\directory replaces %include%.

To use the set command in a batch program to add a new directory to the path environment variable, type:

@echo off
rem ADDPATH.BAT adds a new directory
rem to the path environment variable.
set path=%1;%path%
set

To display a list of all of the environment variables that begin with the letter p, type:

set p

現在のデバイス上のすべての環境変数の一覧を表示するには、次のように入力します。

set