samintz
Scott Mintz
- May
- 1,580
- 28
Code:
@echo off
setlocal
set regkey=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices
set fPersist=0
set fDel=0
set drive1=
set drive2=
do i=1 to %#
switch "%[%i]"
case "/p"
set fPersist=1
case "/d"
set fDel=1
case "/?" .or. "/h"
goto :usage
default
iff "%drive1" == "" then
set drive1=%[%i]
elseiff "%drive2" == "" then
set drive2=%[%i]
else
goto :usage
endswitch
enddo
iff %fPersist == 1 .and. %fDel == 1 then
goto :usage
endiff
iff "%drive2" != "" .and. %fDel == 1 then
goto :usage
endiff
iff "%drive1" == "" .and. (%fDel==1 .or. %fPersist==1) then
goto :usage
endiff
rem Make an existing virtual drive persistent
iff "%drive1" != "" .and. "%drive2" == "" .and. %fPersist==1 then
do d in /p subst
setdos /x-6
set drv=%@left[2,%d]
set vpath=\??\%@trim[%@word[">",1,%d]]
setdos /x+6
iff %drv == %drive1 then
echo Making %drv persistent
set r=%@regset["%regkey\%drv",REG_SZ,%vpath]
goto :display
endiff
enddo
echo Error %drive1 is not a SUBST'ed drive.
quit
endiff
rem Remove an existing drive
iff "%drive1" != "" .and. "%drive2" == "" .and. %fDel==1 then
do d in /p subst
setdos /x-6
set drv=%@left[2,%d]
setdos /x+6
iff %drv == %drive1 then
echo Removing %drv
set r=%@regset["%regkey\%drv",REG_SZ,]
subst %drv /d
goto :display
endiff
enddo
echo Error %drive1 is not a SUBST'ed drive.
quit
endiff
rem Create a substituted drive
iff "%drive2" != "" then
rem first try to run SUBST
subst %drive1 %drive2
iff %? != 0 then
quit
endiff
iff %fPersist==1 then
set r=%@regset["%regkey\%drive1",REG_SZ,\??\%@unquote[%drive2]]
endiff
endiff
rem Default to display mappings
:display
do d in /p subst
setdos /x-6
iff %@regquery["%regkey\%@left[2,%d]"] != -1 then
echos [*] ``
else
echos [ ] ``
endiff
echo %d
setdos /x+6
enddo
echo ^nItems marked with [*] are persistent
quit
:usage
text
PSUBST v1.0 Associates a path with a drive letter.
Manages persistent substituted (virtual) drives.
PSUBST [drive1: [drive2:]path] [/P]
PSUBST drive1: /D | /P
drive1: Specifies a virtual drive to which you want to assign a path.
[drive2:]path Specifies a physical drive and path you want to assign to
a virtual drive (no trailing backslash).
/D Deletes a substituted (virtual) drive.
/P Make a substituted drive persistent
Type PSUBST with no parameters to display a list of current virtual drives.
endtext
quit