0

This use case is the definition of unnecessary but I've taken an interest in scripting and I'm trying to learn. I find my motivation holds best when trying to practice it on things that make my life easier but aren't super important!

I've created a batch file that does the following:

  • Launches a Steam game
  • Switches my display to my TV (I have my TV on the wall next to my PC)
  • Switches my audio source to my headphones

Works wonderfully, but I want to improve it as currently, when I finish playing the game, the display is still on my TV. What I'm looking for is a way to make a batch file run ONLY AFTER the (game) is no longer running to move the image back to my monitor.

In essence, it would go:

  1. Run batch file.
  2. Game opens
  3. Display moves to TV (I use a little app called MultimonitorTool to do this with a set configuration called tv.cfg)
  4. Audio source changes to headphones (I use SoundSwitch and a profile for the game to set source)
  5. Finish playing game and exit.
  6. Batch file to move display back to monitor (already configured this as pc.cfg) runs but only because the game.exe is no longer open.

I was playing around with "/wait" but I couldn't get it to work. In the script below, the first 2 commands work to open Notepad.exe (changed for testing) and then the TV.cfg is what moves the image to the TV. It's the section about waiting for Notepad.exe to run PC.cfg to move the image back to my monitor that doesn't work.

I changed the application to Notepad just for testing, this is what I was trying:

@echo off

cd C:\Windows\system32
start Notepad.exe

cd C:\Users\Rob\Downloads\MultiMonitorTool
start MultiMonitorTool.exe /LoadConfig "C:\Users\Rob\Downloads\MultiMonitorTool\TV.cfg" 

cd C:\Users\Rob\Downloads\MultiMonitorTool
start MultiMonitorTool.exe /LoadConfig "C:\Users\Rob\Downloads\MultiMonitorTool\PC.cfg" /wait "C:\Windows\system32\Notepad.exe"

exit

Is there an easy way to achieve what I'm trying to do? (I'm a complete noob with no schooling on any of this stuff guys! it's purely a hobby)

Hackoo
  • 18,337
  • 3
  • 40
  • 70
Rob Pi
  • 1
  • Have you checked out Windows Task Scheduler? – aschipfl May 28 '21 at 13:17
  • Plain and simple don't use the `START` command on your last command of the batch file. It will wait for the program that is last run to finish and then it will execute any commands after that after you close the program. – Squashman May 28 '21 at 14:13
  • 1
    a) switch to TV, b) `start /wait notepad.exe`, c) switch back to monitor. NOTE: it *does* work with `notepad`, and it *might* work with your game (or not - depends on how exactly the game is written). If not, checking whether the game still runs (with `tasklist` in a loop) is the way to go. – Stephan May 28 '21 at 14:48
  • Thanks Stephan, tried your suggestion and (at least for notepad) it’s working. I’ll play around with it more later when I get home from work lol – Rob Pi Jun 02 '21 at 01:28

1 Answers1

0

In order to know if an application is running, you might use tasklist, more information can be found, using following command:

tasklist /?

You write a while loop which checks if your application is running (don't forget to put a timeout 1, for making your loop wait a second, or you might be "overtasklisting" your PC :-) ).

An example can be found here.

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • I had a go at this but can't get my head around it lol, is there any chance you could be bothered to re-jig the script I started with to include the tasklist/while you mentioned? – Rob Pi May 31 '21 at 01:03