Home > Software engineering >  Batch file Check if entered disk exists
Batch file Check if entered disk exists

Time:04-04

I have a question about check entered letter od disk which exist. I want to display list of avaible disks, then ask user for enter letter of disk, check if this name of disk exist and do some command like echo,etc... I have this code, but it is not working properly..

:Start
echo List of avaible disks:
wmic logicaldisk get deviceid
set /p image=Enter disk:

for %%D in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
 if exist "%%D:\" ( if %image% EQU %%D (
     echo This disk exists: %%D:\... 
goto Next) 
)  else (echo Entered disk does not exist, please enter again
 goto Zacatek)
     
       
)
:Next

CodePudding user response:

Here's a similar idea, also using choice.exe, but instead using mountvol.exe for the drive letter enumeration, (instead of the slow, and now deprecated WMIC.exe).

@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set "DrvList=" & For /F "Delims=: " %%G In ('%SystemRoot%\System32\mountvol.exe
 ^| %SystemRoot%\System32\findstr.exe /RC:"^  *[A-Z]:\\$"'
) Do Call Set "DrvList=%%DrvList%%%%G"
If Not Defined DrvList (Exit /B 1) Else Set "Selection="
If "%DrvList:~,1%" == "%DrvList%" Set "Selection=%DrvList%:"
If Defined Selection GoTo Selected
Echo Enter your drive letter here [%DrvList%]:
For /F "Tokens=2 Delims=?" %%G In ('
 %SystemRoot%\System32\choice.exe /C %DrvList%') Do Set "Selection=%%G:"
:Selected
@Rem Place your commands below here, [%Selection% will be the drive letter].
Echo Your selected drive letter is %Selection%
Pause

The above code should auto select the drive letter, should only one be enumerated.


As a sidenote, based upon the seemingly random drive order output from mountvol.exe, if you'd prefer the prompted order to be guaranteed alphabetical, you could use sort.exe to order it.

@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set "DrvList=" & For /F "Delims=: " %%G In ('%SystemRoot%\System32\mountvol.exe
 ^| %SystemRoot%\System32\findstr.exe /RC:"^  *[A-Z]:\\$"
 ^| %SystemRoot%\System32\sort.exe') Do Call Set "DrvList=%%DrvList%%%%G"
If Not Defined DrvList (Exit /B 1) Else Set "Selection="
If "%DrvList:~,1%" == "%DrvList%" Set "Selection=%DrvList%:"
If Defined Selection GoTo Selected
Echo Enter your drive letter here [%DrvList%]:
For /F "Tokens=2 Delims=?" %%G In ('
 %SystemRoot%\System32\choice.exe /C %DrvList%') Do Set "Selection=%%G:"
:Selected
@Rem Place your commands below here, [%Selection% will be the drive letter].
Echo Your selected drive letter is %Selection%
Pause

CodePudding user response:

I suggest using this code giving the user no chance to select a drive not existing or entering a string which is not valid for selecting a drive at all.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
setlocal EnableDelayedExpansion
:ListDisks
cls
echo List of avaible disks:
echo/
set "DriveLetters="
for /F "skip=1 tokens=1* delims==: " %%I in ('%SystemRoot%\System32\wbem\wmic.exe LOGICALDISK GET DeviceID^,VolumeName 2^>nul') do if not "%%J" == "" (
    echo     Drive %%I: ... %%J
    set "DriveLetters=!DriveLetters!%%I"
)
echo/
%SystemRoot%\System32\choice.exe /C %DriveLetters% /N /M "Please select a drive:"
if not errorlevel 1 goto ListDisks
set /A SelectedDrive=%ERRORLEVEL%-1
set "SelectedDrive=!DriveLetters:~%SelectedDrive%,1!"
endlocal & set "SelectedDrive=%SelectedDrive%"

rem Enter here the code using the variable SelectedDrive.
echo Drive selected: %SelectedDrive%:
endlocal

The batch file outputs the existing logical drives with their volume names and creates at the same time the list of keys the user can press on the following choice prompt according to the drive letters of the existing drives. A where clause could be used to exclude certain drives like CD/DVD drives, too.

The command CHOICE is used to prompt the user for selecting one of the offered drives and the user has just to press the key according to the drive letter. Every other key is ignored and results in an error beep, except Ctrl C for which the IF condition exists below the choice command line.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • cls /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
  • wmic /?
  • wmic logicaldisk /?
  • wmic logicaldisk get /?

Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded wmic command line in a separate command process started in background. The comma must be also escaped with ^ as it would be otherwise replaced by a space character making the wmic command line invalid.

See also:

  • Related