问题
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.
回答1:
The simplest solution is to change the output format of wmic:
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 powershell:
$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 batch-file tag, here's a complete batch-file which leverages powershell:
@"%__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\""
回答2:
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
回答3:
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
来源:https://stackoverflow.com/questions/63153754/wmic-output-to-file-without-addtional-line-issue