
windows 全局變量

Any system administrator who spends a good bit of time in the command prompt or batch scripts is probably well aware of built in environment variables Windows offers (i.e. Path, WinDir, ProgramFiles, UserProfile, etc.). If you find yourself using a particular value over and over, wouldn’t it be great if you had your own variable which you can use the same way as the built in values?
任何在命令提示符或批處理腳本中花費大量時間的系統管理員都可能清楚地了解Windows提供的內置環境變量(即Path,WinDir,ProgramFiles,UserProfile等)。 如果您發現自己一遍又一遍地使用特定值,那么如果您擁有自己的變量并且可以使用與內置值相同的方式,那豈不是很好嗎?
With a few clicks, you can create and maintain you own environment variables which are both global on the system and survive reboots.
只需單擊幾下,您就可以創建和維護自己的環境變量,這些變量既在系統上是全局的,又可以在重新引導后生存下來。
創建自定義系統環境變量 (Creating a Custom System Environment Variable)
Creating a new global system variable is quite simple and is one of those features hiding in plain sight. Please note the screenshots are for Windows Server 2008, however the process for most versions of Windows is almost identical with only a few of the screens different. In the Control Panel, open the System option (alternately, you can right-click on My Computer and select Properties). Select the “Advanced system settings” link.
創建新的全局系統變量非常簡單,并且是隱藏在其中的那些功能之一。 請注意,屏幕截圖是針對Windows Server 2008的,但是大多數Windows版本的過程幾乎相同,只有少數幾個屏幕不同。 在“控制面板”中,打開“系統”選項(或者,您可以右鍵單擊“我的電腦”,然后選擇“屬性”)。 選擇“高級系統設置”鏈接。

In the System Properties dialog, click “Environment Variables”.
在“系統屬性”對話框中,單擊“環境變量”。

In the Environment Variables dialog, click the New button underneath the “System variables” section.
在“環境變量”對話框中,單擊“系統變量”部分下方的“新建”按鈕。

Enter the name of your new variable as well the value and click OK.
輸入新變量的名稱以及值,然后單擊“確定”。

You should now see your new variable listed under the “System variables” section. Click OK to apply the changes.
現在,您應該在“系統變量”部分下看到新變量。 單擊確定以應用更改。

You can now access your new system environment variable like you would any other. You can use it from the command line or batch scripts without having to define it.
現在,您可以像訪問其他系統變量一樣訪問新的系統環境變量。 您可以從命令行或批處理腳本中使用它,而無需對其進行定義。

使用自定義環境變量 (Using the Custom Environment Variable)
As stated above, your custom environment variable is no different than any other system variable as you can reference it from the command line and inside of scripts. For a quick example, consider this batch script:
如上所述,您的自定義環境變量與任何其他系統變量沒有什么不同,因為您可以從命令行和腳本內部對其進行引用。 作為一個簡單的示例,請考慮以下批處理腳本:
@ECHO OFF TITLE Global Environment Variable Test ECHO. ECHO System NotifyEmail value ECHO NotifyEmail = %NotifyEmail% ECHO. SETLOCAL ECHO Overriding global variable in this script… SET NotifyEmail=jfaulkner@otheremail.com ECHO NotifyEmail = %NotifyEmail% ECHO. ECHO Exiting override script… ENDLOCAL ECHO. ECHO System NotifyEmail value ECHO NotifyEmail = %NotifyEmail% ECHO. ECHO. ECHO. PAUSE
@ECHO OFF TITLE全局環境變量測試ECHO。 ECHO系統的NotifyEmail值ECHO NotifyEmail =%NotifyEmail%ECHO。 SETLOCAL ECHO此腳本中的重寫全局變量…SET NotifyEmail=jfaulkner@otheremail.com ECHO NotifyEmail =%NotifyEmail%ECHO。 ECHO正在退出替代腳本…ENDLOCAL ECHO。 ECHO系統的NotifyEmail值ECHO NotifyEmail =%NotifyEmail%ECHO。 回聲。 回聲。 暫停
When executed, the output is exactly what you would expect:
執行后,輸出正是您所期望的:

使用思路 (Usage Ideas)
The real power of custom environment variables? enters when you use them in your scripts. In our example, we set a variable called “NotifyEmail” which we could reference in any number of scripts without having to hard code the value. So in the event we need to change the email address, we simply update the system variable and the impacted scripts will use this new value without us having to update each script individually.
當在腳本中使用自定義環境變量時,它們的真正作用就體現出來了。 在我們的示例中,我們設置了一個名為“ NotifyEmail”的變量,我們可以在任意數量的腳本中引用該變量,而無需對值進行硬編碼。 因此,如果需要更改電子郵件地址,我們只需更新系統變量,受影響的腳本將使用此新值,而無需我們分別更新每個腳本。
This is not only a time saver, but also protects against the situation where you forget to update a particular script and a “dead” value is being used. Additionally, in the event you need to override a system variable in a particular script, you can see in our example above this is fully supported.
這不僅節省了時間,而且還可以避免因忘記更新特定腳本而使用“無效”值的情況。 此外,如果您需要在特定腳本中覆蓋系統變量,則可以在上面的示例中看到,這是完全受支持的。
Here are some ideas where you could apply system variables in place of local scope variables:
這里有一些想法,您可以在其中應用系統變量代替局部范圍變量:
- Email addresses (like in our example) 電子郵件地址(例如我們的示例)
- Backup folder locations 備份文件夾位置
- URL and FTP sitesURL和FTP站點
- Metric and threshold values指標和閾值
Another great feature about using system variables is you have a single place where you can edit or view your variable values. Simply put, you could potentially apply updates to multiple scripts by editing the environment variables in a single location.
使用系統變量的另一個重要功能是您可以在一個地方編輯或查看變量值。 簡而言之,您可以通過在單個位置中編輯環境變量來將更新應用于多個腳本。
翻譯自: https://www.howtogeek.com/51807/how-to-create-and-use-global-system-environment-variables/
windows 全局變量