Search code examples
javawindowswindows-7groovywmic

How can I use a delimiter in wmic output, separating columns?


I want to fetch Windows Hotfix listing with some format, whose output can be separated with some delimiter. so far I found a wmic command which gives me a desired output but the problem is the \s delimiter is not going to work here. Is there a way I can place some , or anyother character, which I can later use in java program to get individual columns?

Command

wmic qfe get caption,csname,description,hotfixid,installedby,installedon

Output

Caption                                        CSName    Description      HotFixID   InstalledBy          InstalledOn
http://go.microsoft.com/fwlink/?LinkId=161784  Abhishek  Update           KB971033   NT AUTHORITY\SYSTEM  3/15/2012
http://support.microsoft.com/?kbid=2032276     Abhishek  Security Update  KB2032276  NT AUTHORITY\SYSTEM  3/15/2012
..
.

Update

I am trying

for /f "tokens=1,2,3,4,5,6,7,8,9,10,11" %g in ('wmic qfe get caption,csname,description,fixcomments,hotfixid,installdate,installedby,installedon,name,servicepackineffect,status') do @echo %g,%h,%i,%j,%k,%l,%m,%n,%o,%p

but it gives me invalid GET Expression

C:\Users\Abhishek\Desktop>for /f "tokens=1,2,3,4,5,6,7,8,9,10,11" %g in ('wmic qfe get caption,csname,description,fixcomments,hotfixid,installdate,installedby,installedon,name,servicepackineffect,status') do @echo %g,%h,%i,%j,%k,%l,%m,%n,%o,%p
Invalid GET Expression.

What is the problem here? This might solve the problem for me .

More Update

I even tried the below command but this too does not solve space problem

Command

for /f "tokens=1,2,3,4,5,6,7,8,9,10,11" %g in ('wmic qfe list') do @echo %g,%h,%i,%j,%k,%l,%m,%n,%o,%p

Output

Caption,CSName,Description,FixComments,HotFixID,InstallDate,InstalledBy,InstalledOn,Name,ServicePackInEffect
http://go.microsoft.com/fwlink/?LinkId=161784,Abhishek,Update,KB971033,NT,AUTHOR,,Y\SYSTEM,3/15/2012,
http://support.microsoft.com/?kbid=2281679,Abhishek,Security,Update,KB2281679,NT,AUTHORITY\SYSTEM,3/15/2012,
http://support.microsoft.com/?kbid=2284742,Abhishek,Update,KB2284742,NT,AUTHORIT,,SYSTEM,3/15/2012,
http://support.microsoft.com/?kbid=2286198,Abhishek,Security,Update,KB2286198,NT,AUTHORITY\SYSTEM,3/15/2012,

Solution

  • As I can see in the result of wmic, columns are separated at least by 2 spaces.

    Caption                                        CSName    Description      HotFixID   InstalledBy          InstalledOn
    http://go.microsoft.com/fwlink/?LinkId=161784  Abhishek  Update           KB971033   NT AUTHORITY\SYSTEM  3/15/2012
    http://support.microsoft.com/?kbid=2032276     Abhishek  Security Update  KB2032276  NT AUTHORITY\SYSTEM  3/15/2012
    

    So this can be easily parsed in Java by splitting with the \s{2,} regular expression :

    String result = "...";
    for (String line : result.split("\n")) {
         System.out.println("-> " + line);
         for (String column : line.split("\\s{2,}")) {
             System.out.println("  => [" + column.trim() + "]");
         }
     }
    

    Outputs:

    -> Caption                                        CSName    Description      HotFixID   InstalledBy          InstalledOn
      => [Caption]
      => [CSName]
      => [Description]
      => [HotFixID]
      => [InstalledBy]
      => [InstalledOn]
    -> http://go.microsoft.com/fwlink/?LinkId=161784  Abhishek  Update           KB971033   NT AUTHORITY\SYSTEM  3/15/2012
      => [http://go.microsoft.com/fwlink/?LinkId=161784]
      => [Abhishek]
      => [Update]
      => [KB971033]
      => [NT AUTHORITY\SYSTEM]
      => [3/15/2012]
    -> http://support.microsoft.com/?kbid=2032276     Abhishek  Security Update  KB2032276  NT AUTHORITY\SYSTEM  3/15/2012
      => [http://support.microsoft.com/?kbid=2032276]
      => [Abhishek]
      => [Security Update]
      => [KB2032276]
      => [NT AUTHORITY\SYSTEM]
      => [3/15/2012]
    

    For the batch side, I cannot help you as I know really nothing about it :-)

    Edit: Apparently there is a switch /format:csv for wmic that should generate a CSV that you could also easily parse in Java. I cannot get it to work on my machine, but it's worth to be noticed.