Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening/Closing application via .bat file [Windows]

Good Day, I have a .bat file that run a specific application then after 5 seconds it will close/kill it.

I having right now due to it successfully open the application thought when the app opens it will not execute the remaining commands unless I manually close the app.

Here is my code:

cd "C:\Program Files (x86)\Aspera\Point-to-Point\bin\"
asperascp.exe
sleep 5 
taskkill /IM asperascp.exe /f

I also try removing the sleep command.

cd "C:\Program Files (x86)\Aspera\Point-to-Point\bin\"
asperascp.exe
taskkill /IM asperascp.exe /f

But it will have same output it will not execute the remaining commands when the asperascp.exe starts.

Any tips?

Thanks.

like image 692
Edmhar Avatar asked Mar 16 '16 05:03

Edmhar


People also ask

How do I close a program with a batch file?

Type taskkill /IM your-program-name. your-program-extension /T /F and then hit ↵ Enter . Repeat this command for as many programs as you want! When finished, type exit on the last line and hit ↵ Enter .

How do you make a batch file that keeps a window open?

Edit your bat file by right clicking on it and select “Edit” from the list. Your file will open in notepad. Now add “PAUSE” word at the end of your bat file. This will keep the Command Prompt window open until you do not press any key.

How do you make a batch file that opens a shortcut?

To do this, go to File > Save As, navigate to the desktop, and type the name of your shortcut followed by . bat (for example, Chrome and Steam. bat) in the File name field. In the Save as type field, click the dropdown menu and select All files.


2 Answers

You can use Start /b command.

@echo off
cd "C:\Program Files (x86)\Aspera\Point-to-Point\bin\"
Start "" /b asperascp.exe 
timeout /T 5 /nobreak >nul
taskkill /IM asperascp.exe /F
like image 75
Hackoo Avatar answered Sep 22 '22 01:09

Hackoo


TASKKILL /IM asperascp.exe /F
will kill all images with the same image name. So if you run the same program twice, for example, and the second one starts before the first one exits, the second one will be killed too when the first one runs taskkill. If you do tasklist you will see some images with the same image name, but with differing PIDs. You will have to get the PID of the process started by your batch file (I can't think of how to do this with CMD.) Then you can use: TASKKILL /PID 999 /F

like image 35
Marichyasana Avatar answered Sep 22 '22 01:09

Marichyasana