vmic environment
' 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,"操作完成"