Reputation: 496
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.
Upvotes: 0
Views: 86
Reputation: 2269
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.
Upvotes: 1