capture result of wmic command to variable [duplic

2019-07-27 04:07发布

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

3条回答
Luminary・发光体
2楼-- · 2019-07-27 04:15

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:, ...

查看更多
Juvenile、少年°
3楼-- · 2019-07-27 04:25

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
查看更多
Juvenile、少年°
4楼-- · 2019-07-27 04:29

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.

查看更多
登录 后发表回答