Search code examples
batch-filewmic

Batch file set variable from wmic


Currently I am trying to get the speed value of the memory into a var like so

for /f "tokens=2 delims==" %%f in ('wmic memorychip get speed /value ^| find "="') do (set msp=%%f)
echo %msp%

But it never transfer the value into the variable. If I do

for /f "tokens=2 delims==" %%f in ('wmic memorychip get speed /value ^| find "="') do (echo %%f)

it will echo out the correct value. Why is this not storeing the value into variable?


Solution

  • Based upon my recent comment, the following is intended to return the slowest memory stick speed in your system.

    @Set "MSp=9999"&For /F EOL^=S %%G In (
        '""%__AppDir__%wbem\wmic.exe" MemoryChip Get Speed 2>NUL"')Do @(
        SetLocal EnableDelayedExpansion
        If 1%%G Lss 1!MSp! (EndLocal&Set /A MSp=%%G 2>NUL)Else EndLocal)
    @Set MSp&Pause
    

    The last line is included for demonstration purposes. I used 9999 as the initial value as it is unlikely that we'll have memory sticks with those speeds for several years, (currently I think the fastest is 5000 and nobody can justify their cost!)