wmic 输出到文件没有附加行问题
wmic output to file without addtional line issue
我查看了所有这些话题和帖子:
话题:https://superuser.com/questions/1082530/how-can-i-remove-the-last-blank-line-of-a-wmic-command
主题:How can I remove empty lines from wmic output?
话题:Parsing WMIC output
Post:
到目前为止,none 提供了帮助,其中大多数人使用 findstr /r /v "^$"
来完成此操作。
它们的问题是,在我的例子中,输出在写入文件时包含一个额外的行。
我想要的文件输出应该是:
[Microsoft Windows 10 Pro]
但我得到:
[Microsoft Windows 10 Pro
]
这是我的代码,它将在您的桌面上创建输出文件:
@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
如有任何帮助,我们将不胜感激。
最简单的解决方案是更改wmic的输出格式:
For /F Tokens^=6Delims^=^" %%A In (
'"%__AppDir__%wbem\WMIC.exe" OS Get Caption /Format:MOF'
) Do Echo [%%A]
上面的答案对所问的问题有效,该问题从一个特定值返回字符串结果。根据您的附加信息,它不再有效,但这不是代码的错误,而是您无法提出正确的问题。
对于您要查找的特定值,它们是 Manufacturer
、Name
、Description
、CurrentClockSpeed
、NumberOfCores
和 NumberOfLogicalProcessors
,我会建议一个完全不同的方法,使用 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"
为了使 batch-file 标签保持主题,这里有一个完整的 batch-file,它利用了 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\""
WMIC 的输出是 unicode !
可以通过另一个 FOR /F 循环传递值来删除尾随 <CR>
。
这也删除了幻影“空白”行(实际上是 <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
我最终使用了@Hackoos 的解决方案,因为它允许获取诸如 wmic CPU get NumberOfCores
之类的值,但在 Compos 的出色回答中失败了,因为该值没有相同的分隔符,因为它是一个值所以我已经修改了 Hackoos 代码以与这样的可重用函数一起使用:
@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:
: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://superuser.com/questions/1082530/how-can-i-remove-the-last-blank-line-of-a-wmic-command
主题:How can I remove empty lines from wmic output?
话题:Parsing WMIC output
Post:
到目前为止,none 提供了帮助,其中大多数人使用 findstr /r /v "^$"
来完成此操作。
它们的问题是,在我的例子中,输出在写入文件时包含一个额外的行。
我想要的文件输出应该是:
[Microsoft Windows 10 Pro]
但我得到:
[Microsoft Windows 10 Pro
]
这是我的代码,它将在您的桌面上创建输出文件:
@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
如有任何帮助,我们将不胜感激。
最简单的解决方案是更改wmic的输出格式:
For /F Tokens^=6Delims^=^" %%A In (
'"%__AppDir__%wbem\WMIC.exe" OS Get Caption /Format:MOF'
) Do Echo [%%A]
上面的答案对所问的问题有效,该问题从一个特定值返回字符串结果。根据您的附加信息,它不再有效,但这不是代码的错误,而是您无法提出正确的问题。
对于您要查找的特定值,它们是 Manufacturer
、Name
、Description
、CurrentClockSpeed
、NumberOfCores
和 NumberOfLogicalProcessors
,我会建议一个完全不同的方法,使用 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"
为了使 batch-file 标签保持主题,这里有一个完整的 batch-file,它利用了 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\""
WMIC 的输出是 unicode !
可以通过另一个 FOR /F 循环传递值来删除尾随 <CR>
。
这也删除了幻影“空白”行(实际上是 <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
我最终使用了@Hackoos 的解决方案,因为它允许获取诸如 wmic CPU get NumberOfCores
之类的值,但在 Compos 的出色回答中失败了,因为该值没有相同的分隔符,因为它是一个值所以我已经修改了 Hackoos 代码以与这样的可重用函数一起使用:
@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:
: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