0

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
Willy
  • 1
  • `properly` is a subjective term. What you mean is "it does not do what I expect". We would need to know what you entered in response to any prompts generated, what it did that it should not have done, what it did not do that it should have done, and any error messages it produced. Please edit any such information into your question. – Magoo Apr 03 '22 at 11:43
  • I'm not sure why you'd ask an end user to simply choose a drive letter, then let them know if it exists or not. Why not just allow them to choose from those which are already existing? or if the idea is for them to only pick an unallocated letter, offer only those to select from1 – Compo Apr 03 '22 at 12:07

3 Answers3

1

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.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and 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 with using 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:

Mofi
  • 46,139
  • 17
  • 80
  • 143
1

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
Compo
  • 36,585
  • 5
  • 27
  • 39
0

Maybe this would work. It checks if an USB, CD or DVD is exist.

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\nul (
      ECHO drive is inserted
   ) ELSE (
      ECHO no drive inserted
   )
)

All drive letters must contain a colon on the end.

arexst
  • 16
  • 1
  • 2