bat获取当前磁盘的某个目录并写入环境变量的目录值
@echo off& color 0a& setlocal enabledelayedexpansion
:: 搜索的目录名 ugii
set "searchDir=UGII"
echo 搜索中,请耐心等待...
call :SearchDir
if not defined lastDir (
echo 没有找到【%searchDir%】安装UG的安装目录
) else (
echo UG安装目录是:!lastDir!
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v UGII_BASE_DIR /t REG_SZ /d !lastDir!\ /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v UGII_ROOT_DIR /t REG_SZ /d !lastDir!\UGII /f
)
echo 运行结束,按任意键退出&pause>nul& exit
:: 搜索文件
:SearchDir
set "lastDir="
for /f "delims=" %%i in ('dir /ad /s /b "%~d0\*%searchDir%" 2^>nul') do (
if /i "%%~ni" equ "%searchDir%" (
set findDir=%%~dpi
set findDir=!findDir:~0,-1!
for /f %%j in ("!findDir!") do (
:: 如果上一级需要跟上路径,就把%%~nj改成%%~dpnj
set "lastDir=%%~dpnj"
)
goto :eof
)
)
goto :eof
共有 0 条评论