How to be sure selected drive is removable or not?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: How to be sure selected drive is removable or not?

#16 Post by thefeduke » 21 Sep 2016 23:21

I used some of the above and posted the following elsewhere at http://www.dostips.com/forum/viewtopic.php?p=49195#p49195. It kind of leads to finger problem correction:

As it happens I was working on something similar with many good ideas from Post subject: "Re: check if a removable drive exists before running". I came up with this:

Code: Select all

@echo off
SetLocal
set folder="x:\"
::  first argument to override previous set command
If /i "%~1" NEQ "" set "Folder=%~1"
::  Do whatever validitycheck that fails
IF not EXIST "%folder%" Call :RemDir folder

Echo.
If /i "%folder%" NEQ "" (Echo.Now working with removable drive %folder%
) Else Echo.No Removable drives available.

set folder="%folder%\"
::  Insert rest of dangerous code here.

Exit /B

:RemDir     DirVariableName[In]
@echo Off & SetLocal EnableDelayedExpansion
    Set "Choices=0"
::  It should work, if you escape the comma (so it is not handled as a special character (delimiter); FOR loop analougous):
For /F "usebackq tokens=1* Delims= " %%d In (`
    WMIC logicaldisk get DeviceId^,drivetype ^| FINDSTR /B "[A-Z]:.*[2]"
`) DO (Set "Drive=%%d"&Set "Choices=!Choices!!Drive:~,1!")

If "%Choices%" EQU "0" Exit /B 1
If "%Choices%" EQU "0%Drive:~,1%" (Set "Chose=1"&GoTo :OnlyOne)
Echo.
Echo. Select '0' to terminate without making a choice.
Echo. Choose from the following drive letters:
Echo.
Choice /C %Choices%
Set "Chose=%errorlevel%"
If "%Chose%" EQU "1" (Echo.Leaving, as requested.&Exit /B 1)
Set /A "Chose-=1"
:OnlyOne
Set "MyDrive=!Choices:~%Chose%,1!:"
(EndLocal & Rem.Save "%~1" value
    If "%~1" NEQ "" (SET "%~1=%MyDrive%") ELSE Echo."No variable to save Drive=%MyDrive%")
Exit /B

John A.

Post Reply