开发者

Batch File/Script to delete Several non sequential lines from text file(s)

I have read the suggestion listed here.

The batch file solution seems to work untill I get to about 5 ignore conditional strings and then the output starts to ignore very large sections of the text files(s) even things it shouldn't.

Basically, I have a bunch of Windows systeminfo (run > cmd > systeminfo) parses. If you run the system info util you will see several lines there (200+). I want a way to run through them (directory at a time hopefully) and only keep or parse out about the 10 lines that matter (CPU speed, RAM amt, etc.).

Like I said, I tried the solution above and it looked great until I got past a few ignore strings and all of a sudden it just started ignoring almost everything.

Does anyone have a suggestion? Or even an idea as to what I was doing wrong?

This is what I got up to before I realized that lines that should not have been deleted were not being printed,

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


This is the script I ended up with. I thank ghost greatly because he pointed me in a great direction. I'm someone that does not do scripting at all but I was able to figure it out.

The code post function is mangling this mangled mess even more but it is working.

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


please get a proper tool for parsing text. if you can afford to use tools like gawk (for windows), here's how you can do it , EASILY.

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

or if you can't afford to download stuff, M$ provided us with vbscript, which is way better than batch for doing things like this. Here's an example and not exhaustive solution

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

Others you can use , for example to get Bios information is Win32_Bios, Win32_PhysicalMemory, Win32_PhysicalMemoryArray for memory info, etc . Please research MSDN vbscript or simple google the various WMI you can use to get computer hardware information.


The problem you have is that findstr handles spaces in the search string a little different:

Use spaces to separate multiple search strings unless the argument is prefixed with /C. For example, 'FINDSTR "hello there" x.y' searches for "hello" or "there" in file x.y. 'FINDSTR /C:"hello there" x.y' searches for "hello there" in file x.y.

So your long chain of findstrs would ignore every line that contain even a single work from any one search string. You can prefix the search strings with /C to avoid that:

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

Also a problem is that the search strings may appear anywhere in the line with above syntax. You can anchor them to the start of the line by using regular expressions:

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

The ^ acts as anchor and tells findstr to match this only at the start of a line. But when using regex you have to escape some characters, such as a dot.


Odds are your problem is simply that your command line is too long. Try breaking the filtering up in several parts, with intermediate files in between.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜