批处理文件/脚本从文本文件中删除几个非连续的行

时间:2009-12-03 21:02:11

标签: string parsing batch-file

我已阅读here列出的建议。

批处理文件解决方案似乎工作,直到我得到大约5忽略条件字符串然后输出开始忽略文本文件的非常大的部分甚至它不应该的事情。

基本上,我有一堆Windows systeminfo(运行> cmd> systeminfo)解析。如果你运行系统信息工具,你会看到几行(200+)。我想要一种方法来运行它们(希望一次目录)并且只保留或解析有关的10行(CPU速度,RAM amt等)。

就像我说的那样,我尝试了上面的解决方案,它看起来很棒,直到我通过一些忽略的字符串,突然间它开始忽略了几乎所有的东西。

有人有建议吗?或者甚至想知道我做错了什么?

在我意识到不应该删除不应删除的行之前,我就是这样做的,

type *.txt | findstr /v "OS Manufacturer:" | findstr /v "OS Configuration:" | findstr /v "OS Build Type:" | findstr /v "Product ID:" | findstr /v "Original Install Date:" | findstr /v "System Up Time:" | findstr /v "System type:" | findstr /v "BIOS Version:" | findstr /v "Windows Directory:" | findstr /v "System Directory:" | findstr /v "Boot Device:" | findstr /v "System Locale:" | findstr /v "Input Locale:" | findstr /v "Time Zone:" | findstr /v "Available Physical Memory:" | findstr /v "Virtual Memory: Max Size:" | findstr /v "Virtual Memory: Available:" | findstr /v "Virtual Memory: In Use:" | findstr /v "Page File Location(s):" | findstr /v "Domain:" | findstr /v "Logon Server:" | findstr /v "Hotfix(s):" | findstr /v "NetWork Card(s):" | findstr /v "Registered Owner:" | findstr /v "Registered Organization:"  > c:\zzz\final.txt

4 个答案:

答案 0 :(得分:1)

这是我最终的剧本。我非常感谢鬼,因为他向我指出了一个伟大的方向。我是一个根本不做脚本的人,但我能够弄明白。

代码发布功能正在破坏这个混乱的混乱,但它正在发挥作用。

On Error Resume Next
Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")    
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")    
Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
Set colComputerSystems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
Const ForAppending = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
        For Each objComputerSystem in colComputerSystems
            dim txtfilename
            txtfilenamepc = objComputerSystem.Name
        dim longuser
        longuser = objComputerSystem.UserName
        dim myArray
        MyArray = Split(longuser,"\")
        dim shortuser
                if MyArray(2) = "" then shortuser = MyArray(1) else shortuser = MyArray(2)
    next
Set objTextFile = objFSO.OpenTextFile _
    ("\\hqfs01\TSUInstall\tools\damir\pcinfologs\" & shortuser & "-" & txtfilenamepc & ".txt", ForAppending, True)
For Each objComputerSystem in colComputerSystems

    objTextFile.WriteLine "User Name: " & shortuser
    objTextFile.WriteLine "Computer Name: " & objComputerSystem.Name
    objTextFile.WriteLine "Model: " & objComputerSystem.Model

    dim ramraw, ramdivider
    ramdivider = 1048576
    ramraw = objComputerSystem.TotalPhysicalMemory
    objTextFile.WriteLine "RAM: " & ramraw\ramdivider & " MB"
    Next
For Each objOperatingSystem in colOperatingSystems
    objTextFile.WriteLine "Operating System: " & objOperatingSystem.Caption
    objTextFile.WriteLine "Service Pack: " & objOperatingSystem.ServicePackMajorVersion
    Next
objTextFile.Close

答案 1 :(得分:0)

请获取解析文本的正确工具。如果你有能力使用像gawk这样的工具(对于windows),这就是你如何做到的,很容易。

C:\test>systeminfo | gawk -F":" "/BIOS|Processor|<things i want>/{print $2}"

或者如果你买不起东西,M $为我们提供了vbscript,这比批处理更好。这是一个例子,而不是详尽的解决方案

Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")    
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")    
Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    Wscript.Echo "Boot Device: " & objOperatingSystem.BootDevice
    Wscript.Echo "Build Number: " & objOperatingSystem.BuildNumber
    Wscript.Echo "Build Type: " & objOperatingSystem.BuildType
    Wscript.Echo "Caption: " & objOperatingSystem.Caption
    Wscript.Echo "Code Set: " & objOperatingSystem.CodeSet
    Wscript.Echo "Country Code: " & objOperatingSystem.CountryCode
    Wscript.Echo "Debug: " & objOperatingSystem.Debug
    Wscript.Echo "Install Date: " & dtmInstallDate 
    Wscript.Echo "Licensed Users: " & objOperatingSystem.NumberOfLicensedUsers
    Wscript.Echo "Organization: " & objOperatingSystem.Organization
    Wscript.Echo "OS Language: " & objOperatingSystem.OSLanguage
    Wscript.Echo "OS Product Suite: " & objOperatingSystem.OSProductSuite
    Wscript.Echo "OS Type: " & objOperatingSystem.OSType
    Wscript.Echo "Primary: " & objOperatingSystem.Primary
    Wscript.Echo "Registered User: " & objOperatingSystem.RegisteredUser
    Wscript.Echo "Serial Number: " & objOperatingSystem.SerialNumber
    Wscript.Echo "Version: " & objOperatingSystem.Version
Next

其他可以使用的,例如获取Bios信息的是Win32_Bios,Win32_PhysicalMemory,Win32_PhysicalMemoryArray以获取内存信息等。请研究MSDN vbscript或简单的google各种可用于获取计算机硬件信息的WMI。

答案 2 :(得分:0)

您遇到的问题是findstr处理搜索字符串中的空格略有不同:

  

使用空格分隔多个搜索   字符串,除非参数是   以/C为前缀。例如,   'FINDSTR "hello there" x.y'次搜索   对于文件"hello"中的"there"x.y。   'FINDSTR /C:"hello there" x.y'   在文件中搜索"hello there"   x.y

因此,您的findstr长链会忽略包含任何一个搜索字符串中的单个作品的每一行。您可以在搜索字符串前加/C作为前缀,以避免:

type *.txt | findstr /v /c:"OS Manufacturer:" | findstr /v /c:"OS Configuration:" | ...

另外一个问题是搜索字符串可能出现在具有上述语法的行中的任何位置。您可以使用正则表达式将它们锚定到行的开头:

type *.txt | findstr /r /v /c:"^OS Manufacturer:" | findstr /r /v /c:"^OS Configuration:" | ...

^充当并告诉findstr仅在行的开头匹配此内容。但是当使用正则表达式时,你必须转义一些字符,例如点。

答案 3 :(得分:-1)

可能的问题很简单,你的命令行太长了。尝试在几个部分中打破过滤,中间使用中间文件。