Ste
Ste

Reputation: 2293

wmic output to file without addtional line issue

I've looked at all these threads and posts:

Thread: https://superuser.com/questions/1082530/how-can-i-remove-the-last-blank-line-of-a-wmic-command

Thread: How can I remove empty lines from wmic output?

Thread: Parsing WMIC output

Post: https://stackoverflow.com/a/37725658/8262102

So far and none help and most of them use findstr /r /v "^$" to do this.

The issue with them all is that in my case the output contains an additional line when written to a file.

My desired output to file should be:

[Microsoft Windows 10 Pro]

but I'm getting:

[Microsoft Windows 10 Pro
]

Here's my code which will create the output file on your desktop:

@echo off
Setlocal EnableDelayedExpansion
cd /d "C:\Users\%username%\Desktop"
(
  for /f "skip=1 delims=" %%A in (
    'wmic OS get Caption ^| findstr /r /v "^$"'
    ) do echo [%%A]
  )>output.txt && exit

Any help would be greatly appreciated.

Upvotes: 1

Views: 864

Answers (3)

Compo
Compo

Reputation: 38589

The simplest solution is to change the output format of :

    For /F Tokens^=6Delims^=^" %%A In (
        '"%__AppDir__%wbem\WMIC.exe" OS Get Caption /Format:MOF'
    ) Do Echo [%%A]

The answer above was valid for the question asked, which was returning a string result from one specific value. Based upon your additional information it is no longer valid, but that is not the fault of the code, but in your inability to ask the correct question.

For the specific values you were looking for, which were Manufacturer, Name, Description, CurrentClockSpeed, NumberOfCores, and NumberOfLogicalProcessors, I would have suggested a completely different approach, using :

$c = GCIM "Win32_Processor"
$o = ForEach ($i in $c) {
    "Manufacturer:                 " + $i.Manufacturer
    "Name:                         " + $i.Name
    "Description:                  " + $i.Description
    "Current Clock Speed:          " + $([Math]::Round($i.CurrentClockSpeed/1000,2)) + " GHz"
    "Number Of Cores:              " + $i.NumberOfCores
    "Number Of Logical Processors: " + $i.NumberOfLogicalProcessors
}
$o | Out-File ".\CPUInfo.txt"

In order to keep that on topic with the tag, here's a complete which leverages :

@"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoP ^
  "$c = GCIM 'Win32_Processor';" ^
  "$o = ForEach ($i in $c) {" ^
    "\"Manufacturer:                 \" + $i.Manufacturer;" ^
    "\"Name:                         \" + $i.Name;" ^
    "\"Description:                  \" + $i.Description;" ^
    "\"Current Clock Speed:          \" + $([Math]::Round($i.CurrentClockSpeed/1000,2)) + \" GHz\";" ^
    "\"Number Of Cores:              \" + $i.NumberOfCores;" ^
    "\"Number Of Logical Processors: \" + $i.NumberOfLogicalProcessors};" ^
  "$o | Out-File \".\CPUInfo.txt\""

Upvotes: 2

Ste
Ste

Reputation: 2293

I've ended up using @Hackoos' solution as it allows to get values such as wmic CPU get NumberOfCores which fails with Compos' great answer because that value doesn't have the same delimiters as it's a value so I've modified Hackoos code for use with a reusable function like so:

@echo off
cd /d "C:\Users\%username%\Desktop"
set WMICGET_VALUE=
(

  call :WMICGET_ECHO CPU Manufacturer              "  Manufacturer:                  "
  call :WMICGET_ECHO CPU Name                      "  Name:                          "
  call :WMICGET_ECHO CPU Description               "  Description:                   "
  call :WMICGET_ECHO CPU CurrentClockSpeed         "">nul
  Setlocal EnableDelayedExpansion
  echo   Current Clock Speed:            !WMICGET_VALUE! Mhz / !WMICGET_VALUE:~0,1!.!WMICGET_VALUE:~1,2! GHz
  endlocal
  call :WMICGET_ECHO CPU NumberOfCores             "  Number Of Cores:               "
  call :WMICGET_ECHO CPU NumberOfLogicalProcessors "  Number Of Logical Processors:  "

  exit

rem Based on this answer: https://stackoverflow.com/a/63153964/8262102
:WMICGET_ECHO
for /f "skip=1 delims=" %%a in ('"wmic %1 get %2"') do (for /f "delims=" %%b in ("%%a") do echo %~3 %%~nb & set "WMICGET_VALUE=%%~nb")
goto :EOF
)>output.txt

Upvotes: 1

Hackoo
Hackoo

Reputation: 18827

The output of WMIC is unicode !

The trailing <CR> can be removed by passing the value through another FOR /F loop.

This also removes the phantom "blank" line (actually a <CR>)

@echo off
for /f "skip=1 delims=" %%a in ('"wmic OS get Caption"') do (
    for /f "delims=" %%b in ("%%a") do set "MyVAR=%%~nb"
)
echo [%MyVAR%]
pause

Upvotes: 1

Related Questions