[Solved] How to get the drive letter of a drive with a specific drive name on Windows? [closed]


A batch file code for copying the folder IMPDoc from drive on which the batch file is stored to a drive with volume name Files is:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "skip=1" %%I in ('%SystemRoot%\System32\wbem\wmic.exe LOGICALDISK where VolumeName^="Files" GET DeviceID 2^>nul') do (
    %SystemRoot%\System32\robocopy.exe "%~d0\IMPDoc" "%%I\IMPDoc" /R:1 /W:1 /NDL /NFL /NJH /NJS
    goto EndBatch
)
echo ERROR: Found no drive with volume name "Files".
echo/
pause
:EndBatch
endlocal

I suggest to run first in a command prompt window the command line:

%SystemRoot%\System32\wbem\wmic.exe LOGICALDISK where VolumeName="Files" GET DeviceID

It should be displayed something like:

DeviceID  
D:        

What can’t be seen is that the output of Windows Management Instrumentation Command is Unicode encoded using encoding UTF-16 Little Endian with byte order mark (BOM) which cause often troubles as Windows command processor is designed for processing text being character encoded with one byte per character, i.e. ASCII/ANSI/OEM encoded text. There are also trailing spaces on both lines which cannot be seen in command prompt window, too.

The FOR command line results in starting in background one more command process with %ComSpec% /c and the command line between ' appended as additional arguments. For that reason it is necessary to escape the equal sign with ^ to be interpreted as literal character and notĀ as separator between argument strings as usual with replacing = by a space character. So executed is in background with Windows installed into directory C:\Windows:

C:\Windows\System32\cmd.exe /c C:\Windows\System32\wbem\wmic.exe LOGICALDISK where VolumeName="Files" GET DeviceID 2>nul

An error output by WMIC on no drive found with case-insensitive interpreted string Files as volume name is redirected with 2>nul from handle STDERR of background command process to device NUL to suppress it.

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.

The output written by WMIC to handle STDOUT of background command process is captured by FOR and processed line by line after started cmd.exe terminated itself.

The first line is skipped because of usage of option skip=1 in addition to empty lines which are always skipped by FOR on processing captured output.

Therefore the first line processed by FOR is the second line. FOR splits up by default a line into substrings using normal space and horizontal tab characters as string delimiters and assigns just first space/tab delimited string to specified loop variable I. This default line splitting behavior is exactly what is needed here to get just drive letter and colon without the trailing spaces assigned to the loop variable I.

FOR would also ignore lines by default on which first space/tab delimited string starts with a semicolon. But there is only a single line to process which starts with a drive letter and so the defaultĀ end of line option eol=; does not need to be changed in this case.

FOR executes the two commands in command block after assigning drive letter and colon to loop variable I. So ROBOCOPY copies all files in directory IMPDoc on drive with the just executed batch file to a directory IMPDoc in root of drive with volume name Files whereby the target directory is automatically created by ROBOCOPY if not already existing. Then the loop is exited with the command GOTO to continue batch file processing on the command line below the label EndBatch.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • pause /?
  • robocopy /?
  • setlocal /?
  • wmic /?
  • wmic logicaldisk /?
  • wmic logicaldisk get /?

See also:

solved How to get the drive letter of a drive with a specific drive name on Windows? [closed]