how to extract specific lines from msinfo32 file using java code

346 Views Asked by At

i am writing a code to extract system info details i.e ram, processor speed and put them on a text file.

public void getSpecs(){
    //run a cmd command to convert msinfo32 to .txt file
   String[] command = {
        "cmd",
    };
    Process p;
    try{
        p= Runtime.getRuntime().exec(command);
        new Thread(new Sec(p.getErrorStream(), System.err)).start();
        new Thread(new Sec(p.getInputStream(), System.out)).start();
        PrintWriter pw= new PrintWriter(p.getOutputStream());

        pw.println("msinfo32 /report .\\specs.txt");
        pw.close();
        p.waitFor();
    }catch(Exception e){
        e.printStackTrace();
    }   
  }
}

This process is taking long and its converting the whole file.

1

There are 1 best solutions below

0
Ioannis Barakos On

msinfo32 exports the computer info into a file. It is expected to take some time as it retrieves a huge export for each computer/windows component.

I have done something similar using powershell

    public static void main(String[] args) throws IOException {
        //Set the commands
        String cmd = "powershell.exe  get-WmiObject ";
        String[] win32CmdA = {"win32_processor", "win32_computerSystem", "win32_logicaldisk"};

        for (String win32Cmd : win32CmdA) {
            String info = runCmd(cmd + win32Cmd,
                    "MaxClockSpeed",
                    "TotalPhysicalMemory",
                    "DeviceID",
                    "FreeSpace");//Add as many atributes you want to return from powershell output
            System.out.println(info); // You can use a file writer here
        }

//        //You can handle ErrorStream here
//        String line;
//        BufferedReader stderr = new BufferedReader(new InputStreamReader(
//                powerShellProcess.getErrorStream()));
//        while ((line = stderr.readLine()) != null) {
//            System.out.println(line);
//        }
    }

    private static String runCmd(String cmd, String... attrs) throws IOException {
        Process powerShellProcess = Runtime.getRuntime().exec(cmd);
        powerShellProcess.getOutputStream().close();

        String line;
        String result="";
        BufferedReader stdout = new BufferedReader(new InputStreamReader(powerShellProcess.getInputStream()));
        while ((line = stdout.readLine()) != null) {
            if (line != null && line.contains(":")) {
                String nameValue[] = line.split(":");
                if (Arrays.asList(attrs).contains(nameValue[0].trim())) {
                    result+=nameValue[0] + " - " + nameValue[1] + "\n";
                }
            }
        }
        stdout.close();
        return result;
    }

The above code invokes the Windows Management Instrumentation (WMI) classes in powershell for specific components (processor, computerSystem and logicaldisk).

Then you define which values should be taken from the powershell output, like MaxClockSpeed, TotalPhysicalMemory, etc.

If you change the System.out.println(info); with a file writer you will have this info in a file.

Sample output (took ~3 seconds to run)

DeviceID           -  CPU0
MaxClockSpeed      -  3401

TotalPhysicalMemory  -  17053949952

DeviceID      -  C
FreeSpace     -  56341774336
DeviceID      -  D
FreeSpace     -  
DeviceID      -  F
FreeSpace     -  373687742464