System: Windows 10 21H2
In a BAT file, I want to detect the drive letter into which an external device has been plugged in. Has the external drive been plugged into E, F or G?
How can I do this in a BAT file?
Thank you.
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Windows » Windows 10 » Windows 10 version 21H2 – November 2021 Update » Detect Drive Letter Of External Device in BAT File?
In my past life had a very clunky but effective way to do this. Create a uniquely named txt file on the USB drive root, make sure it’s not on any other drive.
Then in the BAT script use ‘if exist d:\someweirdfilename.txt then xxx” checks for that file on likely drive letters to find it. I did this to manage a home brewed backup solution for spanning multiple external drives back in the day.
~ Group "Weekend" ~
Windows 7, non-empty USB drive plugged into E: .
(I don’t have an unpackaged empty USB drive at the moment.
But you are looking for a loaded device anyway, right?)
This is a demo directly in the Command Environment.
Ask if you want a full BAT file template.
Also see all the [ IF ] capabilities at [ if /? ]. Elevated prompt not needed.
(Correction on the ‘test for D:’ verbiage; the drive exists, but no disc is loaded.)
(Correction on the ‘test for D:’ verbiage; the drive exists, but no disc is loaded.)
The Optical drive (if you have one) is not always going to be drive D:
Unless you “manually” change it, the S/W will initially assign drive letters depending on how many storage devices it sees and which ports they’re plugged into.
For example, I have 3 internal drives (2 SSD & 1 HDD) and an Optical drive.
The 3 drives are plugged into SATA ports # 1, 2 & 4
The Optical drive is plugged into SATA port # 3
So my drive letters are C: (Win10 SSD), D: (Downloads SSD), E: (Optical drive) & F: (Videos HDD)
If your PC has a Card reader installed, windows will treat it exactly like an Optical drive (i.e. it’s a storage device so it gets a drive letter — regardless of whether there’s a card in it or not.)
So, while your example will determine whether a particular drive letter exists, it can’t really tell you if it’s an internal drive, external drive, optical drive, etc.
Here’s a batch file that will only display the drive letters of removable drives.
@echo off setlocal for /F "skip=1 tokens=1-10" %%A IN ('wmic logicaldisk get description^, deviceid')DO ( if "%%A %%B" == "Removable Disk" ( echo Found Removable Disk %%C ) ) @echo. pause
Simply copy the above code into notepad and save as a .bat file
Notes:
%%A %%B are the device description used to locate a “specific” device type.
Local Fixed Disk = internal storage (SSD/HDD/NVMe)
CD-ROM Disc = Optical drives
Removable Disk = external storage (Thumb drive/SSD/HDD)
Network Connection = network attached storage (mapped drives/shared files & folders)
%%C is the device ID (i.e. the drive letter assigned to that device “including the colon”)
If you have an “external” Card Reader attached to your PC, it’ll show up as one or more of your removable drives (depending on how many different types of cards it can read) regardless of whether there’s actually any cards in it or not.
My USB hub includes slots for reading SD & microSD cards that show up as 2 different “removable” drives so I manually assigned them as Y: & Z: to avoid confusing them with the external drives I regularly use.)
I also have a portable USB card reader for when I travel that can read 7 different types of cards (CF, SD, microSD, M2, MS/MS Pro, xD & SIM) that show up as 5 different “removable” drives when I plug it in.
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 |
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2023 by AskWoody Tech LLC. All Rights Reserved.