使用vbs脚本语言写简单的windows环境变量配置
主要是使用了cmd中的setx命令,所以在使用之前要检查一下在cmd中setx命令是否可以使用
首先在桌面中创建一个新建文本文档并且将后缀名改成vbs
然后使用文本编辑打开
输入代码即可
首先在第一行要声明一个windows对象库调用Shell.Application
Set shell = CreateObject("Shell.Application")
并且调用对象中的文件浏览器功能,并将选中的文件路径封装成对象
Set selFolder = shell.BrowseForFolder
Set selFolderItem = selFolder.Self
selPath = selFolderItem.Path
msgbox "java目录为"+selPath+"确定么?" // 这里是打印出选择的路径
下一步声明对象库并且调用cmd
dim wsh
Set wsh = WScript.CreateObject("WScript.Shell")
dosome1="cmd /c "
配置环境变量地址,以下为使用java环境作为举例
dosome2="@echo off&"
dosome3="setx JAVA_HOME "+ Chr(34) +selPath+ Chr(34)+" /M&"
dosome4="setx classpath .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar /M&"
dosome5="setx path "+Chr(34)+"%path%;%JAVA_HOME%\bin;"+Chr(34)+" /M&"
dosome6="echo 执行完成退出后重新打开cmd输入java -version检查是否配置成功&"
dosome7="pause&"
begindo = dosome1+dosome2+dosome3+dosome4+dosome5+dosome6+dosome7
wsh.run begindo
这里是使用setx命令创建并且修改环境变量,之后使用vbs封装工具
工具地址链接:
https://pan.baidu.com/s/1ywPBI6vDdbIU6ZvShbYCVA
提取码:148z
记住要添加为管理员列表,不然不能修改环境变量
之后得到一个封装好的exe文件 直接执行并且给予权限,选择要配置环境的路径就可以一键配置了
下面是源码示例
JAVA环境:
Set shell = CreateObject("Shell.Application")
Set selFolder = shell.BrowseForFolder(0, "请选择JAVA_HOME目录", 0, ssfDRIVES)
Set selFolderItem = selFolder.Self
selPath = selFolderItem.Path
msgbox "java目录为"+selPath+"确定么?"
dim wsh
Set wsh = WScript.CreateObject("WScript.Shell")
dosome1="cmd /c "
dosome2="@echo off&"
dosome3="setx JAVA_HOME "+ Chr(34) +selPath+ Chr(34)+" /M&"
dosome4="setx classpath .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar /M&"
dosome5="setx path "+Chr(34)+"%path%;%JAVA_HOME%\bin;"+Chr(34)+" /M&"
dosome6="echo 执行完成退出后重新打开cmd输入java -version检查是否配置成功&"
dosome7="pause&"
begindo = dosome1+dosome2+dosome3+dosome4+dosome5+dosome6+dosome7
wsh.run begindo
tomcat环境:
Set shell = CreateObject("Shell.Application")
Set selFolder = shell.BrowseForFolder(0, "请选择CATALINA_HOME目录", 0, ssfDRIVES)
Set selFolderItem = selFolder.Self
selPath = selFolderItem.Path
msgbox "tomcat目录为"+selPath+"确定么?"
dim wsh
Set wsh = WScript.CreateObject("WScript.Shell")
dosome1="cmd /c "
dosome2="@echo off&"
dosome3="setx CATALINA_HOME "+ Chr(34) +selPath+ Chr(34)+" /M&"
dosome4="setx path "+Chr(34)+"%path%;%CATALINA_HOME%\bin;"+Chr(34)+" /M&"
dosome5="echo 执行完成退出后重新打开cmd输入startup检查是否配置成功&"
dosome6="pause&"
begindo = dosome1+dosome2+dosome3+dosome4+dosome5+dosome6
wsh.run begindo
maven环境:
Set shell = CreateObject("Shell.Application")
Set selFolder = shell.BrowseForFolder(0, "请选择MAVEN_HOME目录", 0, ssfDRIVES)
Set selFolderItem = selFolder.Self
selPath = selFolderItem.Path
msgbox "maven目录为"+selPath+"确定么?"
dim wsh
Set wsh = WScript.CreateObject("WScript.Shell")
dosome1="cmd /c "
dosome2="@echo off&"
dosome3="setx MAVEN_HOME "+ Chr(34) +selPath+ Chr(34)+" /M&"
dosome4="setx path "+Chr(34)+"%path%;%MAVEN_HOME%\bin;"+Chr(34)+" /M&"
dosome5="echo 执行完成退出后重新打开cmd输入mvn -version检查是否配置成功&"
dosome6="pause&"
begindo = dosome1+dosome2+dosome3+dosome4+dosome5+dosome6
wsh.run begindo