Tasklist doesn't have access to a certain process - windows

I made a batch file with the following script:
#echo off
goto start
:start
"C:\Games\SV_Debug\Data\Win32\InvisibleM2.vbs"
goto check
:check
tasklist /FI "IMAGENAME eq Launcher2.exe" >swap | find /I /N "Launcher2.exe" >swap
if "%ERRORLEVEL%" == "0" (
goto waitrecheck
) else "%ERRORLEVEL%" (
goto start
)
:waitrecheck
ping 127.0.0.1 -n 3
goto check
This batch file is executed by a vbscript that make's this batch invisible.
The batch file runs a vbscript that runs Launcher2.exe normal and not invisible.
But this batch above here can't check is Launcher2.exe is running.
When it tries I get this error:
The process does not have access to the file because it's being used by another program.
How do I fix this?

tasklist /FI "IMAGENAME eq Launcher2.exe" >swap | find /I /N "Launcher2.exe" >swap
You can not send the output of two programs to the same file at the same time
tasklist /FI "IMAGENAME eq Launcher2.exe" | find /I /N "Launcher2.exe" >nul

Related

Start a program from Batch while preventing duplicates

I am starting a program from Batch, but if the program is already running, I want to avoid starting a second instance.
So far, I have crafted the following code, which seems to work:
TASKLIST /FI "IMAGENAME eq speedfan.exe" | FIND "speedfan.exe" >NUL
IF %ERRORLEVEL%==0 (
TASKKILL /F /IM speedfan.exe
)
START "" "C:\Program Files (x86)\SpeedFan\speedfan.exe"
Alternatively, if I want to keep the current instance instead of starting a new one:
TASKLIST /FI "IMAGENAME eq speedfan.exe" | FIND "speedfan.exe" >NUL
IF NOT %ERRORLEVEL%==0 (
START "" "C:\Program Files (x86)\SpeedFan\speedfan.exe"
)
Can this be improved/simplified?
Update: Thank you all for your comments! So far, my revised code is becoming:
TASKLIST /FI "IMAGENAME eq speedfan.exe" | FINDSTR /BLI "speedfan.exe " >NUL || (
START "" "C:\Program Files (x86)\SpeedFan\speedfan.exe"
)
(and maybe I'll drop the /FI "IMAGENAME eq speedfan.exe" part for the sake of simplicity)
Here's a quick example intended to request input confirmation to close it, if it is running, or to just open it if isn't running. I hope it is robust enough for your purposes.
#"%__AppDir__%tasklist.exe" /Fi "ImageName Eq speedfan.exe" /Fo CSV /NH^
| "%__AppDir__%find.exe" ":" > NUL && (
Start "" "C:\Program Files (x86)\SpeedFan\speedfan.exe") || (
"%__AppDir__%choice.exe" /M "Would you like to close SpeedFan"
If Not ErrorLevel 2 "%__AppDir__%taskkill.exe" /Im speedfan.exe /T > NUL)
I have split the long first line over two for tidiness, and because you don't particularly like long lines.

Batch File which checks for another Batch File process

so google didn't help me at all i need to ask here again.
I use this kind of method to check if my servers are running in 2 batch files.
tasklist /FI "IMAGENAME eq server_64.exe" 2> nul | find "server_64.exe" > nul
IF ERRORLEVEL == 1 (
echo Server is not running
echo.
) else (
echo Stopping Server ...
echo.
taskkill /F /IM server_64.exe > nul 2>&1
)
One to start and one to stop the servers.
Well this works great but when it comes to batch files it wont work for me...
I have one server which runs on phyton so start it via batch file.
My question is, is there a way to get somehow the batch file process status and stop it like it works for exe?
I hope i explained it good enough.
Thx in advance! :)
You can try it with a batch file like this :
#echo off
set "Process=server_64.exe"
Title Checking for status of this process ===^> "%Process%"
tasklist /nh /fi "imagename eq %Process%" 2>nul |find /i "%Process%" >nul
IF '%ERRORLEVEL%' EQU '1' (
Color 0B
echo.
echo "%Process%" is not running
) else (
Color 0C
echo.
echo Stopping "%Process%" ...
taskkill /F /IM "%Process%" > nul 2>&1
)
pause
Omg i found the solution, this was a beast...
tasklist /fi "imagename eq cmd.exe" /v /fo table /nh | find /i "Broker" 2>nul
but what is starmnge is that i cant get the output to be silnce...
when i try to mute it it gives me always error level 1.
tasklist /fi "imagename eq cmd.exe" /v /fo table /nh 2>nul | find /i "Broker" 2>nul
so whats wrong with this? ^

Batch program to to check if process exists

I want a batch program, which will check if the process notepad.exe exists.
if notepad.exe exists, it will end the process,
else the batch program will close itself.
Here is what I've done:
#echo off
tasklist /fi "imagename eq notepad.exe" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"
exit
But it doesn't work. What is the wrong in my code?
TASKLIST does not set errorlevel.
echo off
tasklist /fi "imagename eq notepad.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"
exit
should do the job, since ":" should appear in TASKLIST output only if the task is NOT found, hence FIND will set the errorlevel to 0 for not found and 1 for found
Nevertheless,
taskkill /f /im "notepad.exe"
will kill a notepad task if it exists - it can do nothing if no notepad task exists, so you don't really need to test - unless there's something else you want to do...like perhaps
echo off
tasklist /fi "imagename eq notepad.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"&exit
which would appear to do as you ask - kill the notepad process if it exists, then exit - otherwise continue with the batch
This is a one line solution.
It will run taskkill only if the process is really running otherwise it will just info that it is not running.
tasklist | find /i "notepad.exe" && taskkill /im notepad.exe /F || echo process "notepad.exe" not running.
This is the output in case the process was running:
notepad.exe 1960 Console 0 112,260 K
SUCCESS: The process "notepad.exe" with PID 1960 has been terminated.
This is the output in case not running:
process "notepad.exe" not running.
TASKLIST doesn't set an exit code that you could check in a batch file. One workaround to checking the exit code could be parsing its standard output (which you are presently redirecting to NUL). Apparently, if the process is found, TASKLIST will display its details, which include the image name too. Therefore, you could just use FIND or FINDSTR to check if the TASKLIST's output contains the name you have specified in the request. Both FIND and FINDSTR set a non-null exit code if the search was unsuccessful. So, this would work:
#echo off
tasklist /fi "imagename eq notepad.exe" | find /i "notepad.exe" > nul
if not errorlevel 1 (taskkill /f /im "notepad.exe") else (
specific commands to perform if the process was not found
)
exit
There's also an alternative that doesn't involve TASKLIST at all. Unlike TASKLIST, TASKKILL does set an exit code. In particular, if it couldn't terminate a process because it simply didn't exist, it would set the exit code of 128. You could check for that code to perform your specific actions that you might need to perform in case the specified process didn't exist:
#echo off
taskkill /f /im "notepad.exe" > nul
if errorlevel 128 (
specific commands to perform if the process
was not terminated because it was not found
)
exit
That's why it's not working because you code something that is not right, that's why it always exit and the script executer will read it as not operable batch file that prevent it to exit and stop
so it must be
tasklist /fi "IMAGENAME eq Notepad.exe" 2>NUL | find /I /N "Notepad.exe">NUL
if "%ERRORLEVEL%"=="0" (
msg * Program is running
goto Exit
)
else if "%ERRORLEVEL%"=="1" (
msg * Program is not running
goto Exit
)
rather than
#echo off
tasklist /fi "imagename eq notepad.exe" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"
exit
Try this:
#echo off
set run=
tasklist /fi "imagename eq notepad.exe" | find ":" > nul
if errorlevel 1 set run=yes
if "%run%"=="yes" echo notepad is running
if "%run%"=="" echo notepad is not running
pause

How to check if a program is running as the current user via bat or cmd

How would i go about finding if a version of Outlook is running as a specific user?
I need to check and then if it's not open it under that logged in account.
I've tried a few suggestions from around the site but none are care about the logged in user
Some examples of what i have tried
-------------------------------
tasklist /FI "IMAGENAME eq outlook.exe" 2>NUL | find /I /N "outlook.exe">NUL
if "%ERRORLEVEL%"=="0" echo Programm is running
----------------------
tasklist /FI "IMAGENAME eq notepad.exe" /FO CSV > search.log
FOR /F %%A IN (search.log) DO IF %%~zA EQU 0 GOTO end
start notepad.exe
:end
del search.log
-------------------------------------
I can't install anything on the server and so would need an existing windows solution.
Can't you just add another clause to your tasklist query like so?
tasklist /FI "IMAGENAME eq outlook.exe" /FI "USERNAME eq %username%"
EDIT
Full script that should work.
tasklist /FI "IMAGENAME eq outlook.exe" /FI "USERNAME eq %username%" 2>NUL | find /I /N "outlook.exe">NUL
if "%ERRORLEVEL%" == "1" start outlook.exe

How to count amount of processes with identical name currently running, using a batchfile

I would like to use a batch file to compare the number of processes named "standard.exe", that are running on my Windows 7 machine, with the number of processes named "basic.exe". If the amount of processes called "standard.exe" equals the amount of processes called "basic.exe" nothing should happen, if the numbers are unequal, basic.exe should be restarted.
Any ideas? Already found the following code to check whether a process is running, but now I would like to count the number of processes carrying the same name.
tasklist /FI "IMAGENAME eq myapp.exe" 2>NUL | find /I /N "myapp.exe">NUL
if "%ERRORLEVEL%"=="0" echo Programm is running
Thanks in advance!
Using your example simply replace the /N in find with /C to return the count of processes.
tasklist /FI "IMAGENAME eq myapp.exe" 2>NUL | find /I /C "myapp.exe"
Then you can just reduce it down to :
tasklist | find /I /C "myapp.exe"
Although as Andriy M points out it will match both myapp.exe and notmyapp.exe.
As for the second part of your question, simply do this:
set a=tasklist /FI "IMAGENAME eq myapp.exe" 2>NUL | find /I /C "myapp.exe"
set b=tasklist /FI "IMAGENAME eq myapp2.exe" 2>NUL | find /I /C "myapp2.exe"
if not a==b do (
stuff
)
If you don't want to write a file, replace the tasklist and set var1 commands with
for /f "tokens=1,*" %%a in ('tasklist ^| find /I /C "standard.exe"') do set var1=%%a
same for the second ones.
for /f "tokens=1,*" %%a in ('tasklist ^| find /I /C "basic.exe"') do set var2=%%a
There is probably a neater way to do it, but the following code seems to do the trick:
:begin
tasklist | find /I /C "standard.exe">D:\tmpfile1.txt
tasklist | find /I /C "basic.exe">D:\tmpfile2.txt
set /p var1= <D:\tmpfile1.txt
set /p var2= <D:\tmpfile2.txt
if %var1% LSS %var2% goto restart
if %var1% EQU %var2% goto wait
:wait
echo waiting..
ping -n 300 127.0.0.1 > nul
goto begin
:restart
echo error has occured, all processes will be restarted
taskkill /f /im standard.exe
taskkill /f /im basic.exe
ping -n 30 127.0.0.1 > nul
goto begin
Cheers!

Resources