Find the boot drive in:
DOS has no accurate way to determine the boot drive in a batch file.
The best I could come up with was the
COMSPEC environment variable.
This will work in over 99% of all DOS environments, but it isn't 100%
accurate. The COMSPEC variable may
be changed by the SHELL statement in
CONFIG.SYS or by just setting the variable in AUTOEXEC.BAT or any other
batch file (or even on the command line).
Here is my original version of the batch file:
@ECHO OFF
:: BOOTDRV.BAT
:: Places the boot drive letter in environment variable BOOTDRV
::
:: Written by Rob van der Woude
:: http://www.robvanderwoude.com
::
:: Limitation: Uses COMSPEC to determine boot drive, so if a different
:: command processor was specified by either the SHELL
:: command or by a SET COMSPEC=... command, this batch file
:: will be fooled and display the wrong drive.
::
SET BOOTDRV=
ECHO %COMSPEC% | CHOICE /C:ABCDEFGHIJKLMNOPQRSTUVWXYZ > NUL
IF ERRORLEVEL 1 SET BOOTDRV=A
IF ERRORLEVEL 2 SET BOOTDRV=B
IF ERRORLEVEL 3 SET BOOTDRV=C
IF ERRORLEVEL 4 SET BOOTDRV=D
IF ERRORLEVEL 5 SET BOOTDRV=E
IF ERRORLEVEL 6 SET BOOTDRV=F
IF ERRORLEVEL 7 SET BOOTDRV=G
IF ERRORLEVEL 8 SET BOOTDRV=H
IF ERRORLEVEL 9 SET BOOTDRV=I
IF ERRORLEVEL 10 SET BOOTDRV=J
IF ERRORLEVEL 11 SET BOOTDRV=K
IF ERRORLEVEL 12 SET BOOTDRV=L
IF ERRORLEVEL 13 SET BOOTDRV=M
IF ERRORLEVEL 14 SET BOOTDRV=N
IF ERRORLEVEL 15 SET BOOTDRV=O
IF ERRORLEVEL 16 SET BOOTDRV=P
IF ERRORLEVEL 17 SET BOOTDRV=Q
IF ERRORLEVEL 18 SET BOOTDRV=R
IF ERRORLEVEL 19 SET BOOTDRV=S
IF ERRORLEVEL 20 SET BOOTDRV=T
IF ERRORLEVEL 21 SET BOOTDRV=U
IF ERRORLEVEL 22 SET BOOTDRV=V
IF ERRORLEVEL 23 SET BOOTDRV=W
IF ERRORLEVEL 24 SET BOOTDRV=X
IF ERRORLEVEL 25 SET BOOTDRV=Y
IF ERRORLEVEL 26 SET BOOTDRV=Z
IF "%BOOTDRV%"=="" ECHO Error checking boot drive
IF NOT "%BOOTDRV%"=="" ECHO Boot drive is %BOOTDRV%:
And here are some enhanced versions, created by
Laurence
Soucy.
The first one stores the boot drive letter in the environment
variable BOOTDRV:
:: bootdrv1.bat
@ECHO off
:: By Laurence Soucy
:: http://bigfoot.com/~batfiles/
::
:: To place drive letter into variable
ECHO %comspec%|choice.com/n/c%comspec% set bootdrv=>%temp%.\bootdrv$.bat
FOR %%c in (CALL DEL) do %%c %temp%.\bootdrv$.bat
ECHO "%bootdrv%"
The second version displays the drive letter only (I love batch files that actually consist of one single command line only):
:: bootdrv2.bat
@ECHO off
:: By Laurence Soucy
:: http://bigfoot.com/~batfiles/
::
:: To display drive letter only
ECHO %comspec%|choice.com/n/c:%comspec% " "
The "final" version combines the previous two and adds an online help screen:
:: bootdrv3.bat
@ECHO off
IF not "%1"=="?" IF not "%1"=="/?" GOTO start
ECHO.
ECHO BOOTDRV with no arguments will display drive letter.
ECHO BOOTDRV with any argument will set bootdrv variable
ECHO to drive letter specified by %%comspec%% variable.
ECHO Laurence Soucy
ECHO http://bigfoot.com/~batfiles/
GOTO end
:start
IF "%comspec%"=="" ECHO comspec variable not set
IF "%comspec%"=="" GOTO end
IF "%1"=="" GOTO display only
ECHO %comspec%|choice.com/c%comspec%/n set bootdrv=>%temp%.\bootdrv$.bat
FOR %%c in (CALL DEL) do %%c %temp%.\bootdrv$.bat
GOTO end
:display only
ECHO %comspec%|choice.com/c:%comspec%/n " "
:end
OS/2's Rexx gives us a more accurate way to determine the boot
drive.
In OS/2 Warp Connect and OS/2 Warp 4 there is an exact method, using
RLANUTIL.DLL.
Unless any of the future fixpacks for OS/2 Warp 3 change it, displaying
the drive where \OS2\SYSTEM resides will have to do (this fails only if
there are either more than one \OS2\SYSTEM directories or if a
subdirectory of \OS2\SYSTEM is added to the PATH; both are highly
unlikely, though not impossible):
/* Check for boot drive */
/* Techniques explained by Dick Goran in his January 1997 Rexx Column in */
/* OS/2 Magazine, found at http://www.toward.com/cfsrexx/os2-mag/9701.htm */
/* RLANUTIL.DLL, containg GetBootDrive, was added in Warp Connect */
call RxFuncAdd "GetBootDrive", "RLANUTIL", "GetBootDrive"
if RxFuncQuery( "GetBootDrive" ) = 0 then do
bootdrive = GetBootDrive()
end
else do
/* This should work in any pre Warp OS/2 version too */
path = value( "PATH", , "OS2ENVIRONMENT" )
parse upper value path with ":\OS2\SYSTEM" -1 bootdrive +2
end
say "Boot drive = "||bootdrive
Windows NT has lots of system information available as environment variables:
ECHO Boot drive = %SystemDrive%
In Windows XP Professional we can use WMIC to retrieve more information on the boot drive. This works for the local as well as remote computers.
@ECHO OFF
ECHO.
:: Check Windows version (XP Pro or later) and command line arguments (none)
IF NOT "%OS%"=="Windows_NT" GOTO Syntax
IF NOT "%~1"=="" GOTO Syntax
WMIC.EXE Alias /? >NUL 2>&1 || GOTO Syntax
:: Retrieve drive info
FOR /F "skip=2 tokens=1* delims=," %%A IN ('WMIC Path Win32_DiskPartition Where "BootPartition=true" Get DeviceID /Format:csv') DO SET BootPartition=%%B
FOR /F "tokens=1 delims=[]" %%A IN ('WMIC Path Win32_LogicalDiskToPartition Get Antecedentˆ,Dependent /Format:list ^| FIND /N "=" ^| FIND /I "%BootPartition%"') DO SET LineNum=%%A
SET /A LineNum+=1
FOR /F "tokens=3 delims==" %%A IN ('WMIC Path Win32_LogicalDiskToPartition Get Antecedentˆ,Dependent /Format:list ^| FIND /N "=" ^| FINDSTR /B /L /C:"[%LineNum%]"') DO SET BootDrive=%%~A
:: Format output
FOR /F "tokens=1,2 delims=," %%A IN ("%BootPartition%") DO (
SET BootDisk=%%A
SET BootPartition=%%B
)
SET BootPartition=%BootPartition:~1%
SET BootDrive=%BootDrive:"=%
:: Display the results:
SET Boot
:: Done
GOTO:EOF
:Syntax
ECHO BootDisk.bat, Version 1.00 for Windows XP Pro and later
ECHO Displays boot disk, partition and drive letter.
ECHO.
ECHO Usage: BOOTDISK
ECHO.
ECHO Notes: The result values are displayed on screen and stored in environment
ECHO variables named BootDisk, BootDrive and BootPartition.
ECHO This batch file uses WMIC, which is native in Windows XP Professional,
ECHO Windows Server 2003 and Windows Vista.
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
ECHO.