WMIC输出到文件而没有其他行问题 [英] wmic output to file without addtional line issue

查看:45
本文介绍了WMIC输出到文件而没有其他行问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了所有这些主题和帖子:

I've looked at all these threads and posts:

线程: https://superuser.com/questions/1082530/如何删除我的Wmic命令的最后空白行

线程:如何从wmic输出中删除空行?

线程:解析WMIC输出

帖子: https://stackoverflow.com/a/37725658/8262102

到目前为止,没有任何帮助,其中大多数使用 findstr/r/v"^ $" 来完成.

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]

但是我得到了:

[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

任何帮助将不胜感激.

推荐答案

最简单的解决方案是更改:

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]

上面的答案对于所问的问题是有效的,该问题从一个特定值返回字符串结果.根据您的其他信息,它不再有效,但这不是代码的错误,而是由于您无法提出正确的问题.

对于您要查找的特定值,它们是 Manufacturer Name Description CurrentClockSpeed NumberOfCores NumberOfLogicalProcessors ,我会建议使用一种完全不同的方法,即使用:

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"

为了通过标签,这是完整的

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆