Batch file to automatically launch and close programs?

Synomenon

Lifer
Dec 25, 2004
10,547
6
81
Can anyone please show me how to write a batch file that will launch a program and then close / exit another one at the same time, and then when the program that was launched is closed, re-launch / re-open the one that was closed in the beginning? This is for World of Warcraft and Logitech Setpoint. When World of Warcraft is launched, I want Logitech Setpoint to close and when I close World of Warcraft, I want Logitech Setpoint to re-launch.


Thanks for any help.
 

skace

Lifer
Jan 23, 2001
14,488
7
81
This example shows me using wordpad and notepad since I don't have your exact programs. 2 things need to be figured out. First, put the executables into the script, logitech setpoint replacing wordpad, wow replacing notepad. Second, the dos window will remain open while playing wow, hopefully this won't have any impact, when wow closes the dos window should kick off setpoint again. Let me know if it works.

 

rh71

No Lifer
Aug 28, 2001
52,844
1,049
126
no spaces in batch files, correct ? Should be something like C:\Progra~1
Does the spacing actually work for you ?
 

RT

Member
Sep 20, 2005
38
0
0
I discovered a little app the other day, that may be useful in this situation. It's called runhide. It's a command line app that can launch other command line apps and batch files silently and without a DOSbox appearing. I've been using it to hide an annoying dosbox popup I had whenever my PVR changed channels on my cable box and it's worked like a charm.

I'm not sure whether it could be integrated into Skace's batch while retaining all the functionality of the batch, but if it could be applied it would run the batch without a DOSbox appearing at all, if that's a consideration.

You can find a little more info about it at http://www.novell.com/coolsolutions/tools/14871.html

Edit: Spaces in long file names/directory paths within batch files should be ok, as long as the statement is enclosed by " "'s, as in Skace's example. At least that's true for XP, as I put together a small batch using long file names only this morning, coincidentally, and it worked ok :)
 

skace

Lifer
Jan 23, 2001
14,488
7
81
Originally posted by: rh71
no spaces in batch files, correct ? Should be something like C:\Progra~1
Does the spacing actually work for you ?

You can put plenty of spaces in a batch file. And yea that command works fine. Try it out, your machine should have wordpad and notepad. Just open wordpad type some crap in it and then run the batch file.

Edit: Oh RT said it all. Thanks RT, btw cool app.
 

Synomenon

Lifer
Dec 25, 2004
10,547
6
81
Thanks for all the advise guys! I won't be able to try any of it later, but how would I use RT's tool with the batch file. It doesn't matter to me whether a DOS window is open, but if it does interfere with the game, then I'd like to try the batch file with RT's tool.

Thanks again!
 

skace

Lifer
Jan 23, 2001
14,488
7
81
Try the batch file first, if it does exactly what you want it to do, then worry about hiding it with RT's tool. BTW, you will be using the batch file as your WoW shortcut. So create the batch file somewhere, make a shortcut to the desktop and then associate that shortcut with the WoW Icon and run that to start WoW from now on.

If the shortcut to the batch works to your liking, download RunHide and throw it in your system32 folder. Then change your shortcut to call runhide with the path to the batch file in quotes: runhide "c:\bleh\mybatchfile.bat"
 

Synomenon

Lifer
Dec 25, 2004
10,547
6
81
Cool skace's method works. I'm at work so I tried this on my work PC:


This is in the .bat file:

@echo off
taskkill /im firefox.exe
call "C:\Program Files\Windows Media Player\wmplayer.exe"
call "C:\Program Files\Mozilla Firefox\firefox.exe"
goto :eof


Will RT's RunHide app. hide the DOS box so it doesn't stay visible and take up any taskbar space? If so, how do I use it?



Edit:
Hmmm... I didn't notice it earlier, but after it re-launches Firefox at the end, the DOS window stays open until I close Firefox. What do I add to make the DOS window close after it re-launches Firefox?
 

skace

Lifer
Jan 23, 2001
14,488
7
81
Oops, you are right, make the second "Call" a "Start"

call "C:\Program Files\Windows Media Player\wmplayer.exe"
start "C:\Program Files\Mozilla Firefox\firefox.exe"

Also, my other post has info on how to utilize runhide.
 

Synomenon

Lifer
Dec 25, 2004
10,547
6
81
I changed the second "Call" to "Start" and now after I close Windows Media Player, Firefox doesn't re-launch and after the first DOS window closes (closes after closing Windows Media Player), a second DOS Window opens with the normal things in it (like when you first open the DOS window).

===============

@echo off
taskkill /im firefox.exe
call "C:\Program Files\Windows Media Player\wmplayer.exe"
call "C:\Program Files\Mozilla Firefox\firefox.exe"
goto :eof

===============

@echo off
taskkill /im firefox.exe
call "C:\Program Files\Windows Media Player\wmplayer.exe"
start "C:\Program Files\Mozilla Firefox\firefox.exe"
goto :eof
 

skace

Lifer
Jan 23, 2001
14,488
7
81
Heh, I'm not sure if the functionality changed, but I took another look and this should work for you. Did some testing of my own.


@echo off
taskkill /f /im firefox.exe
call "C:\Program Files\Windows Media Player\wmplayer.exe"
start /d"C:\Program Files\Mozilla Firefox" firefox.exe
goto :eof
 

skace

Lifer
Jan 23, 2001
14,488
7
81
This may be a better solution for the start command:
start "bleh" "C:\Program Files\Mozilla Firefox\firefox.exe"

You don't actually see this second cmd window, but for some reason it wants a title so I titled it bleh. Try what I posted above and then try this and use whichever one works better. The problem with testing with wordpad is that Windows knows where it is and is always able to open it... I should have picked something more unique.

Edit: Ok glad that worked, I'll leave this info here incase it comes in handy. Gluck with runhide.
 

Synomenon

Lifer
Dec 25, 2004
10,547
6
81
Ok. I tried what you said to do for RunHide. It works and the DOS Window is hidden (both Window and taskbar), but I get this window:

Error: Runhide
Error: C:\WINDOWS\system32\RunHide.exe

3052: Uninitiated variable or undefined function

STOP

On line:
WinTitlex=WinItemProcID(FoundPID,0,1)

WinBatch 32 2004F
WIL Version: 5.4fed

Additional Error Information:

Uninitialized variable or undefined function:
foundpid
 

skace

Lifer
Jan 23, 2001
14,488
7
81
Looks like runhide really hates getting any input from the batchfile :)

Try changing "taskkill /im firefox.exe" line to "taskkill /im firefox.exe >nul"
 

RT

Member
Sep 20, 2005
38
0
0
Hmm, seems runhide has some limitations

I've tried adding the runhide parameters into the batch file itself, with mixed results.

runhide.exe taskkill /f /im firefox.exe <- Runhide deals with this fine and closes firefox :)
runhide.exe call "C:\Program Files\Windows Media Player\wmplayer.exe" <- Unfortunately, it's not so hot here and it just skips straight past it :(
runhide.exe start /d"C:\Program Files\Mozilla Firefox" firefox.exe <- Ditto for this line. It skips straight through it :(

For the last two lines, I even tried launching a program from a folder with a short name, in case runhide had some aversion to LFN's, but it made no difference :(

For some reason, it seems that runhide doesn't like start or call commands, but is fine with taskkill. Meh, sorry for getting your hopes up :/


 

skace

Lifer
Jan 23, 2001
14,488
7
81
Sorry I'm at home now and don't have Windows XP here. I believe you can choose some options on your shortcut in XP to utilize it to run hidden without the need for runhide. In Windows 2000, the best I can do is tell it to 'run minimized' in the properties for the shortcut.

I'm not sure what you can do about the error in runhide, don't have any experience with it.
 

RT

Member
Sep 20, 2005
38
0
0
Ok, felt guilty for turning this thread horribly complicated with my earlier mention of hidden windows and then the app I suggested failing to deliver :p, so I did a bit of scouting and may have found a sort of solution, ditching runhide and instead using nircmd.exe from http://www.nirsoft.net/utils/nircmd.html

It's not perfect, but you only see a popup dosbox for a split second before Skace's batch executes and does exactly as he intended it to do.

First create the batch file Skace put together >

@echo off
taskkill /f /im firefox.exe
call "C:\Program Files\Windows Media Player\wmplayer.exe"
start /d"C:\Program Files\Mozilla Firefox" firefox.exe
goto :eof

I stuck with the earlier suggested name of bleh.bat, cos I wasn't feeling particualrly creative, so plagiarised the earlier suggestion :p

Then I created another batch file in the same folder, containing the following


nircmd.exe exec hide bleh.bat


I called this hopeful.bat cos fingerscrossedthis works.bat was a tad unweildy ;)

Now if you click on hopeful.bat you'll see a dos window popup for a fraction of a second, then firefox closes, windows media player opens, and when you close windows media player, firefox re-opens as programmed by Skace :)

Like I say, it's not perfect, but the dosbox that does popup is literally gone in a blink, so it may be workable for your needs :)

Edit: By the way, if you have the session saver plus extension installed in firefox, it gives you the option upon firefox restarting of restoring the active session at the time the batch closed it. So if you had 5 tabs and a half written reply penned on anandtech, it all pops up exactly as it was when firefox closed, which could be handy. Saved me a ton of retyping while testing :)


 

Synomenon

Lifer
Dec 25, 2004
10,547
6
81
Ok. I saved Skace's batch file in my Windows Media Player directory (named the batch file "WinLauncher.bat"), then made another batch file called "Launch.bat": with this in it:

nircmd.exe exec hide WinLauncher.bat



I also saved nircmd in the same directory as the the two batch files above. I see the DOS window appear very quickly and Windows Media Player launches, but FireFox doesn't close like it's supposed to.