1

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.

Ste
  • 1,729
  • 1
  • 17
  • 27

3 Answers3

2

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\""
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thanks very much. This is exactly what I was after. Is there any reason for `%__AppDir__%wbem\WMIC.exe` when `wmic` does the same? – Ste Jul 29 '20 at 23:43
  • I came to a bit of a roadblock when I was after `wmic CPU get NumberOfCores` as the value is not delimited with `"` so I had to revert to @Hackoos' answer. I'll definitely keep this answer in mind. Thanks again for taking your time to help. – Ste Jul 30 '20 at 11:34
  • 1
    @Ste, in answer to your comment, `WMIC` only works like that if the content of `%PATH%` and `%PATHEXT%`, have not been mistakenly modified, and is not a risk anyone should be taking in their production scripts. I have updated my answer, but please understand that my answer was absolutely correct for your asked question, accepting it, then changing your mind when it became invalid for another question you hadn't asked was not really fair. My updated answer should provide everything you wanted as a single command, whereas your current method, uses six `WMIC` commands to get your required output. – Compo Jul 30 '20 at 16:15
  • 1
    I'll mark this as the anwser again as it'll cover all the bases. I didn't mean to upset you but as I didn't know that there was different delimiters in the output when I asked the question or used your answer. What you have given is here more than enough, so thank you. – Ste Jul 30 '20 at 17:02
1

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
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • Not sure what's wrong but the variable `%os%` is reserved and results in `Windows_NT`. If I change that variable name to something else it doesn't work. – Ste Jul 29 '20 at 23:45
  • Thanks, that works but first I need to `Setlocal EnableDelayedExpansion` and then `echo [!MyVAR!]` as opposed to `echo [%MyVAR%]`. That is only when I've wrapped the code as per my question like so: `( your code with those changes above )>output.txt` – Ste Jul 30 '20 at 10:20
1

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
Ste
  • 1,729
  • 1
  • 17
  • 27
  • Ste, I'll say this specifically, here too, because you've highlighted my solution as failing in the answer text above. **My solution did not fail your asked question, you changed your question**. The question you asked was retrieving a string, specifically the value of `Caption` under `Win32_OperatingSystem`, you even provided the exact output you required. Your answer here is retrieving both strings and integers and from a completely different class, `Win32_Processor`. Please do not ask questions, then change them and call out responders who did not guess that your question content was fake! – Compo Jul 30 '20 at 16:28
  • No answer here is wrong. I think you're overthinking this. I didn't foresee an issue when using your answer OR when I asked the question. So far the solution that Hackoo has provided works in all situations. – Ste Jul 30 '20 at 16:55