I am writing a simple batch file to start programs - batch-file

I am writing a simple batch file to start programs.
What command(s) do I use to have the batch file FIRST check (automatically) to see if a program is running, and run the program if it is NOT already running?
[[ Thanks in advance, I've looked and I just can't seem to find the answer. ]]

Try this:
TASKLIST /FI "IMAGENAME eq processName.exe" /FI "status eq running" | find /I /N "processName.exe">NUL
IF ("%ERRORLEVEL%"=="0" echo Process is running) ELSE (start "processName.exe")

Related

How to handle batch file if need to kill the CMD's

I have to write a batch file which is suppose to kill all the currently running CMD processes, except one which is in Listening mode before regression runs. If there is any opened CMD in the same path as Regression, it ruines the regression. There would be probably 2 ways to achieve this:
1. Close all command prompts and start the listener (which can be done by calling a soft link).
2. Close all command prompts, except the one which would run the Regression.
I tried to kill the command prompts, however it ruined the regression as it killed the cmd which is expected to run the regression.
TASKKILL /F /IM cmd.exe /T
cd %appdata%\Microsoft\Windows\Start Menu\Programs\Startup
"Regression Run.lnk"
If I go ahead with approach 1, tried to kill all cmd instances and then run the link, as soon as TASKKILL is called the cmd running itself is also killed. The other 2 lines were never ever called.
For the other approach, I am not able to get any method how to add any exception while calling TASKKILL.
If any one has better idea around it or suggest a better way to achieve it.
Any guidance would be greatly appreciated.
This can be done by using TASKKILL /FI to filter out the PID of the current cmd.exe shell you are running. I would like to know an easier way of getting the current cmd.exe PID.
#ECHO OFF
SET "TEMPFILE=%TEMP%\regtest.tmp"
powershell -NoProfile -Command ^
"(Get-WmiObject -Class Win32_Process -Filter "processid=`'$pid`'").ParentProcessId" >"%TEMPFILE%"
SET /P "MYPID=" <"%TEMPFILE%"
ECHO MYPID is set to %MYPID%
IF EXIST "%TEMPFILE%" (DEL "%TEMPFILE%")
TASKLIST /FI "IMAGENAME eq cmd.exe"
TASKKILL /F /FI "PID ne %MYPID%" /IM cmd.exe
TASKLIST /FI "IMAGENAME eq cmd.exe"

Cannot run batch file as a windows service through nssm

I'm trying to run batch file as a windows service through nssm which will run an exe file if it is not runned.
My code to create service from batch file
CALL "%~dp0nssm64.exe" install MyService "%~dp0test.bat"
CALL "%~dp0nssm64.exe" start MyService
(%~dp0 is the path of the batch file I am running)
I have no problems to creating service, but with starting it. I get the following message while trying to start it manually
In the windows event viewer there is a warning with the message "Service MyService ran for less than 1500 milliseconds. Restart will be delayed by 256000 milliseconds."
So how should I solve the problem? any idea?
Thank you
EditHere is the batch file code:
tasklist /FI "IMAGENAME eq SomeEXE.exe" 2>NUL | find /I /N "SomeEXE.exe">NUL
if "%ERRORLEVEL%"=="0" echo Program is running
if "%ERRORLEVEL%"=="1" start "" "%~dp0SomeEXE.exe"
I just encountered pretty much the same error. I have a batch file that starts a Java application.
Execution line in the batch file was:
start javaw -jar "%APP_HOME%\lib\app.jar" %*
I just fixed it by removing the "start". No more insight into why, but that worked for me.
Here's how I would have written the batch file:
#TaskList /NH /FI "ImageName eq SomeEXE.exe"|Find /I "SomeEXE.exe">Nul&&(
#Echo Program is running)||#Start "" "%~dp0SomeEXE.exe"
…and the NSS commands from my comments:
"%~dp0nssm64.exe" install MyService "%~dp0test.bat">Nul 2>&1&&"%~dp0nssm64.exe" Start MyService

Start batch > start exe + stop exe > batch activation

Batch:
#echo off
tasklist /FI "IMAGENAME eq custom.bin" 2>NUL | find /I /N "custom.bin">NUL
if "%ERRORLEVEL%"=="0" goto mycode
goto quite
:mycode
#RD /S /Q "customfolder"
:quite
exit
now I need to make it work as in the header, step by step:
Run batch
Run custom.bin
Close custom.bin
And now our batch understand that we ve closed custom.bin and
execute custom folder and custom files deletion. Can someone give a
hand? Is it even possible that kind of stuff?
I think you're trying to do something like this:
#echo off
start /wait custom.bin
echo custom.bin has finished running
pause
This starts custom.bin, and waits until it closes, and after that continues to run the rest of the batch-file

Scripting process names with spaces when using TASKLIST command in a bat file

I'm trying to run a bat file that has a TASKLIST command for a process with a space in the name. I'm using the following code:
set "exe=The Process.exe"
tasklist /FI "PROCESSES eq %exe%"
...
but it returns
C:\folder>set "exe=The Process.exe"
C:\folder>tasklist /FI "PROCESSES eq The Process.exe"
ERROR: The search filter cannot be recognized.
So how do I go about running a tasklist command in a bat file for a process with a space in the name?
I believe you have used an invalid keyword in your tasklist filter - PROCESSES should be IMAGENAME.
set "exe=The Process.exe"
tasklist /FI "IMAGENAME eq %exe%"
...
For more filters, take a look at the help for the tasklist command - tasklist /?

Create batch file to block specific programs

How to create an autorun batch file to block/close portable programs?
For example:
if (xxx.exe is running)
then (close it immediately OR block access to internet)
Thank you.
I have tested this batch command:
#echo off
:TOP
tasklist /FI "IMAGENAME eq taskmgr.exe" /FO | grep taskmgr.exe
if ERRORLEVEL == 0 taskkill /f /im taskmgr.exe
GOTO TOP
but it's failed to run, what is the problem?
I'm pretty sure this isn't possible, in batch anyways.
You could edit registry keys for certain files to stop them from being run but this would be system wide, not just for portable programs.
There are too many variables to consider, and this really isn't something you can do in batch.

Resources