greyli
greyli

Reputation: 58

Re-opening files in Batch

For definitely not malicious reasons, I need to have a batch file always open. I have some base code:

:b

echo off

tasklist /fi "imagename eq cmd.exe" |find ":" > nul

if errorlevel 1 taskkill /f /im "game.bat"&start game.bat

goto b

And it works fine if want notepad.exe or blah.txt and etc. Except for batch files, as the program itself is a batch file, the system sees cmd.exe is already open.

Upvotes: 1

Views: 459

Answers (1)

DavidPostill
DavidPostill

Reputation: 7921

It works except for batch files, as the system sees cmd.exe is already open.

  1. Give your batch file a Title by adding the following command to game.bat:

    title %~nx0
    
  2. Check if game.bat is running by using tasklist with /v option:

    :b
    @echo off
    tasklist /v | find "game.bat" > nul
    rem errorlevel 1 means game.bat is not running, so start it
    if errorlevel 1 start game.bat
    rem timeout to avoid excessive processor load
    timeout 60
    goto b
    

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • tasklist - TaskList displays all running applications and services with their Process ID (PID) This can be run on either a local or a remote computer.
  • timeout - Delay execution for a few seconds or minutes, for use within a batch file.
  • title - Change the title displayed above the CMD window.

Upvotes: 3

Related Questions