' fso判斷環境變量添加腳本是否存在
Set fso = CreateObject("Scripting.FileSystemObject")
' 以管理員權限運行環境變量添加腳本
Set UAC = CreateObject("Shell.Application")
tmpFile = ".\addToPath.bat"
If fso.FileExists(tmpFile) Then
UAC.ShellExecute ".\addToPath.bat", "", "", "runas", 1
Else
' 文件缺失,生成環境變量添加腳本
Set batFile = fso.CreateTextFile(".\addToPath.bat", True)
batFile.WriteLine("@echo off")
batFile.WriteLine(":: 獲取當前文件夾絕對路徑")
batFile.WriteLine("set tmpDir=%~dp0")
batFile.WriteLine("wmic ENVIRONMENT where " & chr(34) & "name='PATH' and username='<system>'" & chr(34) & " set VariableValue=" & chr(34) & "%tmpDir%;%PATH%" & chr(34))
UAC.ShellExecute ".\addToPath.bat", "", "", "runas", 1
End If
MsgBox "已將當前路徑添加到環境變量PATH中", vbOkOnly+vbInformation,"操做完成"orm