Search code examples
batch-filecmdwmic

Batch file acting on each item in a FOR loop


I'm trying to loop through all the dimms in my system and write them to a text file. I know I can use WMIC's /Append or CMD's redirect (>>), but append outputs Unicode and other sections of the batch file redirect output that is ANSI. Plus, both standard methods output lots of blank lines.

echo off
setlocal EnableDelayedExpansion
set _invpath="c:\temp\%COMPUTERNAME%-Inventory.txt"
for /f "tokens=2 delims==" %%f in ('wmic memorychip get devicelocator /value') do (
  if [!f] NEQ [] (
    set "MemoryDeviceLocator=%%f"
    set _prefix=%MemoryDeviceLocator:~0,4%
    if [%_prefix%]==[DIMM] (
       for /f "usebackq tokens=2 delims==" %%g in (`wmic memorychip where 'devicelocator^="%MemoryDeviceLocator%"' get capacity /value ^| find "="`) do set "Capacity=%%g"
       for /f "usebackq tokens=2 delims==" %%h in (`wmic memorychip where 'devicelocator^="%MemoryDeviceLocator%"' get serialnumber /value ^| find "="`) do set "SerialNumber=%%h"
       for /f "usebackq tokens=2 delims==" %%i in (`wmic memorychip where 'devicelocator^="%MemoryDeviceLocator%"' get speed /value ^| find "="`) do set "Speed=%%i"
       echo %MemoryDeviceLocator%=%MemoryDeviceLocator% >> %_invpath%
       echo %MemoryDeviceLocator%Capacity=%Capacity% >> %_invpath%
       echo %MemoryDeviceLocator%SerialNumber=%SerialNumber% >> %_invpath%
       echo %MemoryDeviceLocator%Speed=%Speed% >> %_invpath%
    )
  )
)

This formats the output the way I want, but only displays the last DIMM, not each DIMM. Eg:

DIMM2=DIMM2 
DIMM2Capacity=4294967296 
DIMM2SerialNumber=9169FG56 
DIMM2Speed=1333
DIMM2=DIMM2 
DIMM2Capacity=4294967296 
DIMM2SerialNumber=9169FG56
DIMM2Speed=1333

How do I modify the batch to output the information for DIMM1?

Edited to show my enabledelayedexpansion line


Solution

  • I managed to figure this out. And I apologize. I had enabled delayed expansion I should have included that portion of the batch file.

    Here's how I solved it:

    echo off
    setlocal EnableDelayedExpansion
    set _invpath="c:\temp\%COMPUTERNAME%-Inventory.txt"
    for /f "tokens=2 delims==" %%f in ('wmic memorychip get devicelocator /value') do (
      call set "MemoryDeviceLocator=%%f"
      call set _prefix=!MemoryDeviceLocator:~0,4!
      if [!_prefix!]==[DIMM] (
        echo !MemoryDeviceLocator!=!MemoryDeviceLocator! >> %_invpath%
        for /f "usebackq tokens=2 delims==" %%g in (`wmic memorychip where 'devicelocator^="!MemoryDeviceLocator!"' get capacity /value ^| find "="`) do call set "Capacity=%%g"
        echo !MemoryDeviceLocator!Capacity=!Capacity! >> %_invpath%
        for /f "usebackq tokens=2 delims==" %%g in (`wmic memorychip where 'devicelocator^="!MemoryDeviceLocator!"' get serialnumber /value ^| find "="`) do call set "SerialNumber=%%g"
        echo !MemoryDeviceLocator!SerialNumber=!SerialNumber! >> %_invpath%
        for /f "usebackq tokens=2 delims==" %%g in (`wmic memorychip where 'devicelocator^="!MemoryDeviceLocator!"' get speed /value ^| find "="`) do call set "Speed=%%g"
        echo !MemoryDeviceLocator!Speed=!Speed! >> %_invpath%
      )
    )
    

    I'm not 100% sure what the exact difference between %variable% and !variable! is, but I do know the exclamation point forces expansion of the variable at the time it is read.