Copyright © 2003-2011 by Prof. Timo Salmi  
Last modified Sun 27-Feb-2011 18:17:02

Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.

68} How can I test if a program already has been loaded?

In XP one can use Task Manager for the purpose e.g. as follows
  @echo off & setlocal enableextensions
  tasklist|find "firefox.exe ">nul
  if %errorlevel% EQU 0 (
    echo Firefox has been loaded
    ) else (
    echo Firefox has not been loaded)
  endlocal & goto :EOF

The output would be e.g.
  Firefox has been loaded

Another example, this time not relying on find. Sometimes e.g. Firefox keeps running in the background even if this does not show on the taskbar (a bug or a feature is beside the point here). How to detect and delete the task?
  @echo off & setlocal enableextensions
  set target_=firefox.exe
  set running_=false
  for /f "skip=3 tokens=1" %%p in (
    'tasklist /fi "USERNAME ne NT AUTHORITY\SYSTEM" ^
      /fi "STATUS eq running"') do (
        if "%%p"=="%target_%" set running_=true)
  if "%running_%"=="true" (
    taskkill /im %target_%
    ) else (
    echo %target_% is not running)
  endlocal & goto :EOF
If you want to force it apply   taskkill /f /im %target_%

References/Comments: (If a Google message link fails try the links within the brackets.)
  Google Groups Oct 27 2004, 12:08 am [M]
References/Comments (for NT/2000):
  PsTools Microsoft TechNet

If you are using a script in a command window to call an MS-DOS program (or a script), and you wish to prevent calling that same program simultaneously from another window/or via shelling (to avoid datafile or whatever conflicts), you can proceed as shown below. The script creates a lockfile for the duration of running the script.
  @echo off & setlocal enableextensions
  if exist "%temp%\vpp3d.mrk" (
    echo Exiting: vpp3d already is running
    echo If this is in error delete %temp%\vpp3d.mrk
    goto :EOF)
  echo Mark vpp3d>"%temp%\vpp3d.mrk"
  :: Call whatever program it happens to be

  del "%temp%\vpp3d.mrk"
  endlocal & goto :EOF

It is quite possible that there are several cmd.exe windows open and a script or program running in one of them. Since all are labeled cmd.exe the question arises how to test if a particular script is being run. This is how to solve the task:
  @echo off & setlocal enableextensions
  set myscriptIsRrunning_=
  tasklist /v|findstr /b /i /r /c:"cmd.exe.* - myscript">nul
  if %errorlevel% EQU 0 set myscriptIsRunning=true
  if defined myscriptIsRunning (
    echo myscript is currently running
    ) else (
    echo myscript is not currently running)
  endlocal & goto :EOF

References/Comments: (If a Google message link fails try the links within the brackets.)
  Google Groups Oct 13 2006, 12:42 am [M]
  Google Groups May 26 2008, 5:55 am [M]