Thursday, January 5, 2017

windows 10 - I need a batch file that opens cmd, runs two commands, and keeps cmd open


I am making a set of batch files made to force restart games when they freeze and won't start back up again.


So I need a batch file that opens cmd.exe, runs the two commands:


taskkill /F example.exe`

and


start example.exe

and lets cmd stay open. Every time I try these two commands cmd is just open for a second and closes saying that no command was made, or the commands aren't able to be used even though they work when I type them into cmd directly.


Answer



I need a batch file that opens cmd, runs two commands, and keeps cmd open


taskkill /F example.exe

This is an invalid command. You need to use:


taskkill /F /IM example.exe


  • /IM image name. The image name of the process to be terminated. Wildcard '*' can be used to specify all image names.


Use the following batch file (test.cmd):


@echo off
setlocal
taskkill /F /IM example.exe
start example.exe
endlocal

To run it and keep the cmd shell open use the following command:


cmd /k test


  • /K Run Command and then return to the CMD prompt. This is useful for testing, to examine variables




Further Reading



  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.

  • cmd - Start a new CMD shell and (optionally) run a command/executable program.

  • start - Start a program, command or batch script (opens in a new window).

  • taskkill - End one or more processes (by process id or image name).


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...