Skip to content

Instantly share code, notes, and snippets.

@MineClever
Last active September 9, 2022 20:18
Show Gist options
  • Save MineClever/7defc2c638f44fdd75e2f503fbb12844 to your computer and use it in GitHub Desktop.
Save MineClever/7defc2c638f44fdd75e2f503fbb12844 to your computer and use it in GitHub Desktop.
从当前文件夹位置创建Windows磁盘映射
@echo off
:: 以GBK环境运行
chcp 963
cls
:: 入口
:Main
::添加本地变量
set inputDrive=M
set driverSymbol=%inputDrive%
echo -----------------
echo Script Created By MineClever
echo Version : 0.0.3
echo -----------------
:: admin check
cacls.exe "%SystemDrive%\System Volume Information" >nul 2>nul
if %errorlevel%==0 goto :Admin
:BaseProcess
echo -----------------
echo 将当前文件夹注册为 盘符 “%driverSymbol%”
echo -----------------
echo .
pause
set ForcePath="%~dp0"
call :setLocalEnv
call :ClearDrive
call :SetMappingDrive
echo -----------------
echo 已使用用户权限注册 盘符 “%driverSymbol%”
echo -----------------
echo .
echo -----------------
echo 接下来将尝试以管理权权限注册, 请允许获取管理员权限
echo -----------------
echo .
echo -----------------
echo 如果无管理员权限, 软件却以管理员权限查询时,无法读取盘符
echo -----------------
pause
echo .
:: 从管理员运行
call :AdminRun
goto :EOF
:ClearDrive
net use %driverSymbol%: /delete 2>nul
subst %driverSymbol%: /d 2>nul
goto :EOF
:SetMappingDrive
subst %driverSymbol%: %ForcePath%
goto :EOF
:SetStartDocMappingDrive
REG DELETE "%dosRecSrv%" /v "%driverSymbol%:" /f 2>nul
set NamedForcePath=%~dp0
if "%NamedForcePath:~-1%" == "\" set NamedForcePath=%NamedForcePath:~0,-1%
REG ADD "%dosRecSrv%" /f /v "%driverSymbol%:" /t REG_SZ /d "\??\%NamedForcePath%"
goto :EOF
:setLocalEnv
:: 设置本地变量,避免无法读取到系统路径
set path=%SystemRoot%;%path%
set path=%SystemRoot%\system32;%path%
set sysRegEnvPath=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
set userRegEnvPath=HKEY_CURRENT_USER\Environment
set dosRecSrv=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices
goto :EOF
:AdminRun
::尝试以管理员权限运行
cd /d "%~dp0"
cacls.exe "%SystemDrive%\System Volume Information" >nul 2>nul
if %errorlevel%==0 goto :Admin
if exist "%temp%\getadmin.vbs" del /f /q "%temp%\getadmin.vbs"
echo Set RequestUAC = CreateObject^("Shell.Application"^)>"%temp%\getadmin.vbs"
echo RequestUAC.ShellExecute "%~s0","","","runas",1 >>"%temp%\getadmin.vbs"
::让Wscript执行WScript.Quit来结束进程,不然进程里会一直有脚本解释器在运行;
echo WScript.Quit >>"%temp%\getadmin.vbs"
"%temp%\getadmin.vbs" /f
if exist "%temp%\getadmin.vbs" del /f /q "%temp%\getadmin.vbs"
exit
goto :EOF
:Admin
::管理员权限运行命令
echo Admin Called !
set ForcePath="%~dp0"
call :setLocalEnv
call :ClearDrive
call :SetMappingDrive
call :SetStartDocMappingDrive
echo -----------------
echo 已使用管理员权限注册 盘符 “%driverSymbol%”
echo -----------------
pause
goto :EOF
:EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment