Knowledge Base Glossary       Exit
(how to) Search  

Browse by Category
Knowledge Base > Operating Systems > Windows > NT/2K/XP/2003 > Selecting a Hard Drive from a Windows Command Script

Selecting a Hard Drive from a Windows Command Script

The Windows 2000+ command script shown below uses the TeraByte Unlimited EMBR_NT.EXE utility to (in effect) present a hard drive selection menu. Such a menu could be used for any purpose where a Windows command script is being run and a hard drive number needs to be selected.  For example, after some customization, the script could be used to allow a hard drive number to be selected prior to performing a restore to it using Image for Windows.

By default, the script queries hard drive numbers 0 through 9. For any drive where the query succeeds, the EMBR_NT.EXE utility displays basic information relating to the partitions/volumes present on that drive (name, size, type, ID, file system). If a drive does not exist, the utility displays a simple error message ("Error: Unable to open hard drive N.").

Beyond presenting the menu described, this script performs no actions. Where indicated near the bottom of the script, you can add your own code to perform whatever action is desired with the selected hard drive number.

The EMBR_NT.EXE utility can be obtained from the following links:

http://www.terabyteunlimited.com/downloads/EMBR_NT.ZIP
ftp://www.terabyteunlimited.com/EMBR_NT.ZIP

The script begins below. To use it, select all applicable text below and paste it into a text editor such as Notepad. Then save it with the .BAT or .CMD extension.

@echo off & cls & setlocal

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: This Windows 2000+ command script uses the TeraByte Unlimited EMBR_NT.EXE utility
:: to (in effect) present a hard drive selection menu. By default, the script queries
:: hard drive numbers 0 through 9. For any drive where the query succeeds, the
:: EMBR_NT.EXE utility displays basic information relating to the partitions/volumes
:: present on that drive (name, size, type, ID, file system). If a drive does not exist,
:: the utility displays a simple error message ("Error: Unable to open hard drive N.").
::
:: Beyond presenting the menu described, this script performs no actions. Where indicated
:: near the bottom of the script, you can add your own code to perform whatever action
:: is desired with the selected hard drive number.
::
:: The EMBR_NT.EXE utility can be obtained from the following links:
::
:: http://www.terabyteunlimited.com/downloads/EMBR_NT.ZIP
:: ftp://www.terabyteunlimited.com/EMBR_NT.ZIP
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: Check that the script is running under a proper version of Windows:
ver | find "Version 5." >nul
set VerCheck1=%ErrorLevel%
ver | find "Version 6." >nul
set VerCheck2=%ErrorLevel%
set VerCheck=%VerCheck1%%VerCheck2%
if "%VerCheck%"=="11" (
   cls
   echo.
   echo Error running %0:
   echo.
   echo This script runs only under Windows 2000 or later.
   echo.
   pause
   goto :EOF          )

:: Check for the existence of EMBR_NT.EXE:
embr_nt.exe >nul 2>&1
if ErrorLevel 9009 (
   cls
   echo.
   echo Error running %0:
   echo.
   echo EMBR_NT.EXE was not found. The utility may be downloaded here:
   echo.
   echo http://www.terabyteunlimited.com/downloads/EMBR_NT.ZIP
   echo ftp://www.terabyteunlimited.com/EMBR_NT.ZIP
   echo.
   echo Place EMBR_NT.EXE in a folder that is in the system path.
   echo %WinDir% is generally the best choice, for simplicity.
   echo.
   pause
   goto :EOF       )

:: Building the hard drive selection menu:
echo.
echo ###################################################
echo Enter a number to select hard drive:

:: If desired, you can make the script stop querying hard drives beyond a certain number.
:: For example, to query only hard drive numbers 0, 1, and 2, use "(0,1,2)" below, rather
:: than the default of "(0,1,9)". There is no harm in querying non-existent hard drive
:: numbers, but for any non-existent drive number that is queried, an error will display.
for /l %%a in (0,1,9) do call :CheckDrives %%a

:: The line below is not executed until after all hard drives have been queried:
goto :PostCheckDrives

:: The hard drive query loop begins with the line below ("call :CheckDrives"):
:CheckDrives

set #=%1
if "%#%"=="" goto :EOF

echo ---------------------------------------------------
echo Hard Drive [%#%]:
echo.

embr_nt.exe %#% lis

:: The "goto :EOF" below just stops the loop, not the whole script:
goto :EOF

:PostCheckDrives

echo ###################################################
echo.

:: Make the hard drive selection:
choice /c:0123456789 /n Please select a hard drive number [0-9]:
set HDNumber=%ErrorLevel%
:: The CHOICE command sets ErrorLevel to the 1-based offset of the key pressed (based on the
:: characters "0123456789" shown). Since hard drives are 0-based, we need to subtract 1 to adjust:
set /a HDNumber-=1

:: Verify the hard drive selection:
echo.
echo You selected hard drive number %HDNumber%.
echo.
choice /c:YN /n Is that hard drive number correct [YN]?
if ErrorLevel 2 (
   cls
   echo.
   echo No action has been taken.
   echo.
   pause
   goto :EOF    )

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Put your own code below here, doing whatever desired with the selected hard drive
:: number, which will be in the variable HDNumber. The ECHO line below is just an example.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

cls & echo. & echo You selected hard drive number %HDNumber%. & echo.

:: End of script


How helpful was this article to you?


powered by Lore