batch-fileformatoutputwmiccustom-formatting

Custom Format for wmic output in BATCH (CMD)


How to put a custom format for wmic output in batch ?

before everything to start, i wan't to say that i'm not english and that my text will be bad lmao

Hello, i'm a batch beginner and i would like to make a custom output format for wmic

Exemple :

1# Memory Name : Corsair

2# Memory Name : Kingston

3# Memory Name : Corsair

4# Memory Name : Kingston

the problem is that my code will show only the latest memory name like i have 4 chip of memory but it will only show me one of them

Exemple :

1# Memory Name : Corsair

but i don't really know how would i be able to do what i want with a custom output format

if you need the actual code :

wmic memorychip get manufacturer /value | findstr /r /v "^$" > manufacturer & wmic memorychip get capacity /value | findstr /r /v "^$" > capacity & set /p manufacturer=<manufacturer&set /p capacity=<capacity
set memory%manufacturer%&set memory%capacity%&del manufacturer&del capacity

echo Memory : %memorymanufacturer% %memorycapacity%

Solution

  • Unless you do not set variables, you're probably not going to get much more compact than this:

    @echo off & set cnt=0
    setlocal enabledelayedexpansion
    for /f "skip=1 delims=" %%i in ('wmic memorychip get capacity^,manufacturer') do for /f "tokens=1,*" %%a in ("%%i") do (
        set /a cnt+=1
        set "cap!cnt!=%%a"
        set "man!cnt!=%%b"
    )
    for /L %%r in (1,1,%cnt%) do echo(# %%r Manufacturer: !man%%r:  =!   Capacity: !cap%%r: =!
    

    If however you do not set the variables and simply use the metavariables, it can be much more compact:

    @echo off
    for /f "skip=1 delims=" %%i in ('wmic memorychip get capacity^,manufacturer') do for /f "tokens=1,*" %%a in ("%%i") do echo Manufacturer: %%b Capacity %%a