简体   繁体   中英

USB drive Letter finder

I have this script that checks for a certain USB and then opens a file on it. However, now I need it to check only for the drive letter of the USB inserted so. However I don't really know how to do that and searches online returned nothing that could help me. This is the script I have right now:

@echo off
:loop
if exist D:\ (goto Load) else (goto loop)
:Load

D:
start Loader1.exe
goto Finish

:Finish 
exit

Is something like this that you are looking?


This will take over the task only on the Removable Disk and will be given the associated logical name of each for check if your file Loader1.exe is there for take next action.

  • Value Meaning for drivetype in WMIC logicaldisk

    0 Unknown

    1 No root directory

    2 Removable disk

    3 Local disk

    4 Network drive

    5 Compact disk

    6 RAM disk


  • wmic logicaldisk get caption,drivetype|find "2"

![在此处输入图片说明


This Result Removable disk : 
D:       2
E:       2
F:       2
G:       2
  • Caption = D:
  • DriveType = Removable disk == 2

After, and make sure the Loader1.exe file exist to start/run your command:

Driver:\\Loader1.exe

The last part, time out (1 minute) to perform the next loop:

timeout 60 >nul && goto :loop


@echo off

:loop
for /f %%i in ('"wmic logicaldisk get caption,drivetype|find "2""')do if exist "%%~i\Loader1.exe" (
    cd /d %%~i\ & start .\Loader1.exe && goto :Finish )

timeout 60 >nul & goto :loop)

:Finish 
  • :: Or ::

@echo off

set "_cd=%cd%" && title <nul

:loop

for /f %%i in ('"wmic logicaldisk where drivetype=2 get name|find /v "Name""
')do if exist "%%~i\Loader1.exe" cd /d %%~i\ & start .\Loader1.exe & cd /d "%_cd%" & exit /b

timeout 60 >nul & goto :loop

If You Have label Name Of Flash, Try This :

@echo off
setlocal
set /p vName=Enter volume to search for:
set n=0
set ltrs=ABCDEFGHIJKLMNOPQRSTUVWXYZ
:Loop
call set ltr=%%ltrs:~%n%,1%%
set /a n+=1
vol %ltr%: 2>nul|find /i " %vname%">nul||if %n% lss 26 goto :loop
if %n% equ 26 (set "ltr="
echo No matching volume label found.
 ) else (echo Volume %1 is drive %ltr%:)
endlocal & set ltr=%ltr%
pause

Another Way :

for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (

if %%l equ 2 (
echo %%i is a USB drive.
            )
            )
Pause

Or =

setlocal enabledelayedexpansion
Set "USB="
REM get removable loaded drives:
for /f "tokens=1-5" %%a in (
 'wmic logicaldisk list brief'
) do if %%b Equ 2 if %%d gtr 0 Set USB=!USB! %%a
Echo:Usb removable drives:%USB%
EndLocal&Set USB=%USB:~1%

Here's how to get the USB drive letter from any drive:

@echo off
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do ( 
  if %%l equ 2 ( echo %%i is a USB drive. ) 
)

Answer is not a duplicate of @scientist_7 I got it from Find USB Drive letter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM