2

I am trying to write a small batch script to install an app from a thumb drive. The problem is the drive letter changes when plugged into different machines depending on the available drive letters. I have the script to run the installation but would like to add a script to the beginning that would detect the assigned drive letter of my inserted thumb drive and store it in a variable that I could then substitute in the rest of the script for the drive letter to complete the installation.

I got the command to identify the assigned drive letter of the thumb drive which works on its own.

wmic logicaldisk where volumename="StacelandFlash" get name

Result: D: (correct)

But I can't seem to assign it to a variable.

set X=wmic logicaldisk where volumename="StacelandFlash" get name
echo X

Result: X

set X=wmic logicaldisk where volumename="StacelandFlash" get name
echo %X%

Result: wmic logicaldisk where volumename="StacelandFlash" get name

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
2Cents
  • 23
  • 1
  • 3
  • 1
    Please [edit] your question title to something relevant that isn't just repeating the tags you've added. Your title should describe the question you're asking or problem you're trying to solve, and should be clear enough to be of use to future readers here who see it in a list of search results. You've simply added the tag information redundantly as the topic, which is meaningless and repetitive. – Ken White Mar 25 '17 at 00:57
  • 1
    Obilgatory "DOS and batch are different animals and you almost certainly aren't using DOS" comment. – SomethingDark Mar 25 '17 at 01:22

3 Answers3

2

Firstly, to capture the output of a command to a variable use for /F. See for /? in a cmd console for full details. Example:

for /f "tokens=2 delims=:" %%I in ('find /c /v "" "notes.txt"') do (
    set /a "linecount=%%I"
)
rem // %linecount% now contains the number of lines in notes.txt

Now, there are a couple of complications unique to capturing WMI query results. Firstly, your wmic command includes an equals sign, which will break a for /f. That part's easy enough to fix: either escape the = with a caret (e.g. ^=), or just surround the equation in quotation marks.

The next hurdle is a bit trickier. WMI results are encoded in a non-ANSI encoding (UCS-2 LE). Capturing the output of wmic also captures the output's encoding, resulting in the last character being moved to the beginning of the line or other unexpected behavior. The workaround for that is to use a second nested for /f to sanitize the value.

With all that in mind, I think this is what you're looking for:

@echo off
setlocal

for /f "tokens=2 delims==" %%I in (
    'wmic logicaldisk where "volumename='StacelandFlash'" get name /value'
) do for /f "delims=" %%# in ("%%I") do set "driveletter=%%~#"

echo %driveletter%

Note: credit @Dave Benham for discovering this workaround.

rojo
  • 24,000
  • 5
  • 55
  • 101
1

You need to run the command within a For Loop.

@Echo Off
For /F "Skip=1 Delims=" %%A In ('
    "WMIC LogicalDisk Where (VolumeName='StacelandFlash') Get Name"
') Do For %%B In (%%A) Do Set "USB=%%B"
Echo(%USB%
Timeout -1
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thank you VERY MUCH Compo Initially I used a prompt to ask for the drive letter as below. This worked fine but you gave me exactly what I was hoping for. Thanks again. @echo off echo Enter the Source Drive Letter set /p Drive=driveletter: and then replacing all references to the drive letter within the script with %drive% – 2Cents Mar 26 '17 at 02:18
0

I suppose the batch file executed is stored on the thumb drive. And this batch file is executed with a double click. Therefore all you need is:

set "DriveLetter=%~d0"

%~d0 references the drive of argument 0 which is the batch file name. Run in a command prompt window call /? for details on how to reference arguments of a batch file. %~d0 expands to D:, E:, ...

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Thank you Mofi. This is a very simple solution that gets the job done. I don't understand it but it is exactly what I was trying to do. – 2Cents Mar 26 '17 at 02:50