yanyusanqian
yanyusanqian

Reputation: 120

How can I get drive letter by searching USB name using Windows command or Node.js/Electron?

I want to download the content to USB disk named distinguishingly in the background. So I need to find its position. Is there have any function to realize it by using a Windows command?

Or is there any function to find a USB drive by using Node.js/Electron?

And if it possible, how can I distinguish between two USB drives that have the same name?

Upvotes: 1

Views: 1407

Answers (1)

Mofi
Mofi

Reputation: 49136

It is possible to use %~d0 to reference the drive of the batch file if the batch file is stored on the storage media connected via USB to the computer.

But if the batch file is stored on a different storage media like the local hard disk inside the PC, the following small batch files demonstrates how to get the drive letter of the USB storage media via its volume name.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "DriveUSB="
set "VolumeName=NameOfVolume"
:GetDriveLetter
for /F "tokens=2 delims==" %%I in ('%SystemRoot%\System32\wbem\wmic.exe LOGICALDISK where VolumeName^="%VolumeName%" GET DeviceID /VALUE 2^>nul') do set "DriveUSB=%%I"
if not defined DriveUSB (
    echo Device with name %VolumeName% not found.
    echo(
    %SystemRoot%\System32\choice.exe /C YN /N /M "Retry (Y/N):"
    if errorlevel 2 goto :EOF
    goto GetDriveLetter
)
echo Drive letter of %VolumeName% is: %DriveUSB%
rem More commands using environment variable DriveUSB.
echo(
pause
endlocal

NameOfVolume must be adapted to real volume name. This string is interpreted case-insensitive by WMIC.

Please read the Microsoft documentation for Win32_LogicalDisk class and its properties for more information about the used class.

The device identification could be done also by using VolumeSerialNumber instead of VolumeName which is more difficult to change than the volume name without formatting the volume. Run in a command prompt window the following command to get displayed the volume serial number:

wmic LOGICALDISK where VolumeName="NameOfVolume" GET VolumeSerialNumber

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.

  • choice /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • rem /?
  • 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 with %ComSpec% /c and the command line within ' appended as additional arguments. This is also the reason why = must be escaped with ^ to be interpreted as literal character and not as argument separator which would be replaced otherwise by a space character by command processor instance processing the batch file making the where clause invalid for wmic executed by the background command process.

It is recommended to read also DosTips forum topic: ECHO. FAILS to give text or blank line - Instead use ECHO/

Upvotes: 2

Related Questions