• Detect Drive Letter Of External Device in BAT File?

    Home » Forums » AskWoody support » Windows » Windows 10 » Windows 10 version 21H2 – November 2021 Update » Detect Drive Letter Of External Device in BAT File?

    • This topic has 6 replies, 6 voices, and was last updated 12 months ago.
    Author
    Topic
    #2451582

    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.

    Viewing 4 reply threads
    Author
    Replies
    • #2451597

      Has the external drive been plugged into E, F or G?

      I always set a drive letter to external drives.
      Unless you attach foreign drives setting a drive letter is the best solution.

    • #2451620

      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" ~

    • #2451634

      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.

      E-Test

      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.)

      • #2451726

        (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.

    • #2451715

      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.

    • #2451761
      1 user thanked author for this post.
    Viewing 4 reply threads
    Reply To: Detect Drive Letter Of External Device in BAT File?

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: