2009-12-03 50 views
0

我已閱讀here列出的建議。批文件/腳本刪除文本文件中的幾個非連續行

批處理文件解決方案似乎工作,直到我得到約5忽略條件字符串,然後輸出開始忽略文本文件的非常大的部分甚至它不應該。

基本上,我有一堆Windows系統信息(run> cmd> systeminfo)解析。如果你運行系統信息util,你會看到幾行(200+)。我希望有一種方法可以通過它們(有希望的時候是目錄),並且只保留或解析有關10條線路的信息(CPU速度,RAM內存等)。

就像我說過的,我嘗試了上面的解決方案,它看起來很棒,直到我忽略了一些忽略的字符串,並且突然間它忽略了幾乎所有的東西。

有沒有人有建議?甚至是一個想法,我做錯了什麼?

這是我站起身來之前,我意識到,沒有被打印的不應該被刪除的行,

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 

回答

-1

賠率是你的問題很簡單,你的命令行太長。試着打破幾個部分的過濾,中間文件介於兩者之間。

+0

沒有可能導致此問題的變量擴展,cmd中的最大命令行長度限制爲8190個字符。 – Joey 2009-12-06 14:18:27

0

請獲取解析文本的適當工具。如果你有能力使用像gawk這樣的工具(對於windows),那麼這就是你如何做到的,EASILY。

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或者簡單的谷歌可以使用的各種WMI來獲取計算機硬件信息。

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 
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僅在一行的開始匹配這一點。但是在使用正則表達式時,你必須避開一些字符,比如點。

相關問題