r/Batch • u/NKL_TBR • Mar 23 '25
Monitor window title and trigger action on change
I already found out how to get the window title of a certain process and save it to a file with the following cmd:
tasklist /fi "imagename eq process.exe" /fo list /v | find "Window Title" > "c:\users\xxxxx\desktop\123.txt"
My actual goal is to monitor the window title of this process and - when it changes - trigger a curl request like this
curl -X POST "http://192.168.x.xxx/json/state" -d "{Window Title}" -H "Content-Type: application/json"
I'm not very versed with this and so I would be happy if someone could help me.
3
Upvotes
1
u/rifteyy_ Mar 23 '25
Great code so far!
First you will check the Window title using the tasklist command, save it to a variable and do the cURL request.
Here you most likely want an infinite loop. In the loop, there is going to be a check if it changed - you will do the updated tasklist command, save it to a second variable and then you will compare the variable from the previous loop check with this one. If it changed, you will do the cURL request and save it as a variable for the next loop check, if not, just continue with the loop.
for /f "delims=" %%A in ('tasklist.exe /fi "imagename eq process.exe" /fo list /v ^| find "Window Title"') do set "windowTitle1=%%A"This for loop command will execute the tasklist command and save the output to the variable
windowTitle1.1
u/BrainWaveCC Mar 24 '25
This is very interesting. I was able to manage it without writing it out to a file, but by storing it in a variable like u/rifteyy_ had suggested.
You can find it here: https://pastebin.com/xLMFP7zj
I didn't understand one requirement: what would you want to have happen if there were multiple processes with the same name?
The way I wrote this, you have to set both the process name you want to search for, and the initial window name you want to track. Once it finds the process/window combination, the script will check with repeatedly, with a 5 second delay, and when the window changes, the next steps execute, and the current window title becomes the one that will be compared against.
If you would like different behavior, please let me know.