0

I'm dumping info about all the processes running on my pc into a .txt file. To do this I execute handle.exe from my java application. The file contains all the running processes in this format:

RuntimeBroker.exe pid: 4756 
4: Key           HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image 
8: Event  
   10: WaitCompletionPacket 
   1C: IRTimer       
   20: WaitCompletionPacket 
   24: IRTimer       
   28: File          \Device\CNG

--

SearchIndexer.exe pid: 5616 
4: Event         
8: WaitCompletionPacket 
C: IoCompletion  
1C: IRTimer
20: File          \Device\0000007s
22: Directory   

I need to get the name of the process that is using a given device i.e. if I'm looping through the file searching for the string "\Device\0000007s", I need to get the name of the process and the process id which is a few lines above. Does anybody know how could I do this? The processes are delimited by a line of dashes -- in the file. Bear in mind that the file is massive, this is just an example.

5
  • Does it have to be Java? awk is made for this sort of thing. Commented Oct 17, 2016 at 19:48
  • Possible duplicate of How to read a specific line using the specific line number from a file in Java? Commented Oct 17, 2016 at 19:48
  • Yeah @cxw it needs to be Java, it's a Java application Commented Oct 17, 2016 at 19:49
  • You could simply replace out all the spaces, break on the delimiter and loop through to grep/regex the string you are looking for. Once you have that just use String methods to pull out "pid:[0-9][0-9]+" im just spitballing here but experiment with that. Commented Oct 17, 2016 at 19:51
  • The thing is I don't know yet the string I'm looking for, the file could be in use by any process with any process name. As if I need to go backwards into the file until the line of dashes and then one line ahead to get the process name, I'm struggling so bad Commented Oct 17, 2016 at 19:55

1 Answer 1

1

I would read each line of a process (using a Scanner) into a List<String>. Then search through the List<String> for your desired String and if it is there, do your processing. Here is some psuedo-code:

Scanner scanner = new Scanner("path/to/file.txt");
List<String> stringList;
while(scanner.hasNextLine()) {
  String nextLine = scanner.nextLine();

  if(nextLine.equals("--") {
    for(String line : stringList) {
      if(line.contains("\Device\0000007s") {
        // Do your processsing here
      }
    }

    stringList.clear();
  }

  else {
    stringList.add(nextLine);
  }

}

This is just psuedo-code, doesn't handle the edge case of the last process and probably won't compile. I will leave the nitty-gritty syntax up to you.

There is probably a more optimal way of doing this, with less looping. But for simple things like this I much prefer a clear approach to an optimized one.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice one Adam Rosini, I'll give this a try and see if it doesn't take ages looping and finding the string as I need this to a be quite quick process. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.