Computer Hope

Software => Computer programming => Topic started by: kohgan on October 20, 2017, 08:37:09 AM

Title: Need Batch file to execute and close same program
Post by: kohgan on October 20, 2017, 08:37:09 AM
Hello all.
I have rainmeter on my system, and game alot. I'm trying to figure out how to create a single button on my desktop to open and close the program so as not to clutter my desktop with multiple icons for the same program. I currently have two separate files one for turning it on and the other for turning it off.

To turn on rainmeter I have the following batch

@echo off
cd "C:\Program Files\Rainmeter"
start Rainmeter.exe
exit


To turn off Rainmeter I have this batch

taskkill /f /im rainmeter.exe


Is there a way to combine these into a single batch file so I have only one button that does both optionally?
Title: Re: Need Batch file to execute and close same program
Post by: BC_Programmer on October 20, 2017, 09:18:09 PM
This should "toggle" when you run it. If it's running it is terminated, if it's not running, it is launched.

Code: [Select]
@echo off
cd "C:\Program Files\Rainmeter"
taskkill /f /im rainmeter.exe && echo "terminated Rainmeter" || rainmeter.exe
exit
Title: Re: Need Batch file to execute and close same program
Post by: Squashman on October 24, 2017, 12:56:08 PM
Why not gracefully exit the program with the proper command line argument.

Code: [Select]
"C:\Program Files\Rainmeter\Rainmeter.exe" !Quit