如何格式化文本文件上的批处理命令结果

时间:2018-06-22 02:19:43

标签: batch-file

我正在使用下面的批处理脚本检索计算机信息,

(
    systeminfo | findstr /c:"Host Name" /c:"Domain" /c:"OS Name" /c:"OS Version" /c:"System Manufacturer" /c:"System Model" /c:"System type" /c:"Total Physical Memory" /c:"Available Physical Memory" 

    wmic bios get serialnumber

) > "getPCinfo.txt"

然后在文本文件上显示的结果是

Host Name:                 Host
OS Name:                   Microsoft Windows 10 Pro
OS Version:                10.0.16299 N/A Build 16299
System Manufacturer:       Dell Inc.
System Model:              Latitude 7480
BIOS Version:              Dell Inc. 1.7.3, 10/11/2017
Total Physical Memory:     8,077 MB
Available Physical Memory: 2,450 MB
Domain:                    Domain.Domain.Domain.net
S e r i a l N u m b e r     

 3 R K 5 M H 2               

我可以知道如何将序列号格式化为与以前的信息相同的格式:-

S e r i a l N u m b e r :   3 R K 5 M H 2  

4 个答案:

答案 0 :(得分:2)

我不喜欢脚本中的行太长-概述太松散了。
该批次使用findstr的默认RegEx模式,行的锚点以if (!BigDecimal.ZERO.equals(parentInstruction.getQuantity()) || !canSkipTransTypeCds(parentInstruction)) return true;
开头 并用点代替搜索字符串中的空格。

/B

示例输出:

:: Q:\Test\2018\06\22\Get-PCInfo.cmd
@Echo off
FOR /F "tokens=1,2 delims==" %%A IN ('
  wmic bios get serialnumber /value ^|find /I "serialnumber"
') DO For %%C in (%%B) Do Set "BIOS=%%A:              %%C"

Set "Find=Host.Name Domain OS.Name OS.Version System.Manufacturer"
Set "Find=%Find% System.Model System.type Total.Physical.Memory"
Set "Find=%Find% BIOS.Version Available.Physical.Memory"

( systeminfo | findstr /I /B "%Find%"
  Echo=%BIOS%
) >getPCinfo.txt

答案 1 :(得分:1)

这应该对您有用。

type Demo = Demo of Map<ContentAndYear, DemoMap>

...

let x = demoMap |> Demo

答案 2 :(得分:1)

这是一个字符编码问题:systeminfo返回ASCII / ANSI文本,而wmic返回Unicode文本。

Squashman已经展示了一种将wmic的输出转换为his answer中的ASCII / ANSI文本的方法,尽管我会写一些不同的字样以避免转换伪造的东西-返回(CR)字符:

systeminfo | findstr /c:"Host Name" /c:"Domain" /c:"OS Name" /c:"OS Version" /c:"System Manufacturer" /c:"System Model" /c:"System type" /c:"Total Physical Memory" /c:"Available Physical Memory" > "getPCInfo.txt"
for /F "delims=" %%H in ('wmic BIOS get SerialNumber /VALUE') do for /F "tokens=1* delims==" %%I in ("%%H") do >> "getPCInfo.txt" echo %%I:              %%J

无论如何,这是使用临时文件的另一种方法:

systeminfo | findstr /c:"Host Name" /c:"Domain" /c:"OS Name" /c:"OS Version" /c:"System Manufacturer" /c:"System Model" /c:"System type" /c:"Total Physical Memory" /c:"Available Physical Memory" > "getPCInfo.txt"
wmic bios get serialnumber /VALUE > "getPCInfo.tmp"
type "getPCInfo.tmp" >> "getPCInfo.txt"
del "getPCInfo.tmp"

仅当托管cmd实例不是不是以Unicode模式运行(请参见其/U选项),而默认值为ASCII / ANSI({{1 }})。

答案 3 :(得分:1)

这是未经测试的PowerShell版本,仅是因为systeminfo需要花费多长时间

PCInfo.ps1

$Properties = @{  
  Host_Name = (GWMI Win32_OperatingSystem).CSName
  OS_Name = (GWMI Win32_OperatingSystem).Caption
  OS_Version = GWMI Win32_OperatingSystem | % {$_.Version+' build '+$_.BuildNumber}
  System_Manufacturer = (GWMI Win32_ComputerSystem).Manufacturer
  System_Model = (GWMI Win32_Computersystem).Model
  BIOS_Version = (GWMI Win32_BIOS).SMBIOSBIOSVersion
  Total_Physical_Memory = GWMI Win32_PhysicalMemory | Measure Capacity -Sum | % {[String]$([Math]::Round(($_.Sum / 1MB),0))+' MB'}
  Available_Physical_Memory = GWMI Win32_OperatingSystem | % {[String]$([Math]::Round(($_.FreePhysicalMemory * 1KB / 1MB),0))+' MB'}
  Domain = GWMI Win32_Computersystem | % {$_.DNSHostName +'.'+$_.Domain}
  Serial_Number = (GWMI Win32_BIOS).SerialNumber}
$ColumnWidth = ($Properties.Keys | Measure -Max Length).Maximum
$Properties.GetEnumerator() | Sort Name| % {"{0,-$ColumnWidth}: {1}" -F $_.Key, $_.Value}

要运行它,请根据需要更改路径,然后在命令提示符下输入以下内容:

PowerShell -NoP -ExecutionPolicy RemoteSigned -File "C:\Users\YHTAN\Desktop\PCInfo.ps1">"C:\Users\YHTAN\Desktop\PCInfo.txt"