So I want to close my live wallpaper when epic games launcher starts and then start it when i close the launcher and with other programs it worked fine but now it gives me this error message:ERROR: Invalid argument/option - 'Live'.
I think this is because the live wallpaper has spaces in its name, but I tried quotation marks and it still doesn't work. Do you know what I should do?
#echo off
:TEST
tasklist /nh /fi "imagename eq EpicGamesLauncher.exe" | find /i "EpicGamesLauncher.exe" >nul
if "%ERRORLEVEL%"=="1" goto TIMER
taskkill /im "DesktopHut Live v5.0.0.exe" /t /f
:TIMER
timeout /T 10
goto TEST2
:TEST2
tasklist /FI "IMAGENAME eq EpicGamesLauncher.exe" 2>NUL | find /I /N "EpicGamesLauncher.exe">NUL
if "%ERRORLEVEL%"=="0" goto TEST
tasklist /nh /fi "imagename eq "DesktopHut Live v5.0.0.exe" "| find /i /n "DesktopHut Live v5.0.0.exe" >nul
if "%ERRORLEVEL%"=="0" goto TEST
start " " "C:\Users\David\Desktop\DesktopHut Live v5.0.0.exe"
goto TEST
pause>nul
The reason for the error message is that you've used a wrong syntax for the filter expression of tasklist: tasklist /nh /fi "imagename eq "DesktopHut Live v5.0.0.exe" "
The filter expressions can have spaces in them and tasklist does not have any problems with them, The real problem is the limitation in the output of tasklist which is apparently fixed at 25 characters for the Image Name column, when using the default output formatting.
So assuming the process DesktopHut Live v5.0.0.exe is already running, The output of
tasklist /fi "imagename eq DesktopHut Live v5.0.0.exe"
would be:
Image Name
=========================
DesktopHut Live v5.0.0.ex
To overcome that limitation you can specify csv as the output format by the /fo csv switch
tasklist /fi "imagename eq DesktopHut Live v5.0.0.exe" /fo csv | find /i "DesktopHut Live v5.0.0.exe" >nul
Since you are using find to set the errorlevel, there is no need to use a filter expression for tasklist so the entire command can be simplified to:
tasklist /fo csv | find /i "DesktopHut Live v5.0.0.exe" >nul
Related
I am fairly new to coding and batch files, so bare with me.
The program I want to start that way is opera. But the batch file doesnt seem to find it. This is how far I got:
tasklist /FI "opera.exe" 2>NUL | find /I /N "opera.exe">NUL
if NOT "%ERRORLEVEL%" == "0" start "" "C:\Users\leonv\AppData\Local\Programs\Opera.exe"
PAUSE
I would take the verification check, just a little bit further. I would look to see if an ImageName of Opera.exe, with a Status of Running, and for the current UserName is returned:
#%SystemRoot%\System32\tasklist.exe /Fi "Status Eq Running" /Fi "ImageName Eq Opera.exe" /Fi "UserName Eq %UserDomain%\%UserName%" | %SystemRoot%\System32\find.exe "="
#If ErrorLevel 1 Start "" /Min "%LocalAppData%\Programs\Opera.exe"
You need to specify IMAGENAME eq processname like:
tasklist /fi "IMAGENAME eq opera.exe"
Additionally, no need to run if statements, you can use conditional operators && and ||
(tasklist /fi "IMAGENAME eq opera.exe"| findstr /I "opera.exe")>nul && echo It's running || start "" "%LocalAppData%\Programs\Opera.exe"
and to run it in a loop, checking every N of seconds:
#echo of
:loop
(tasklist /fi "IMAGENAME eq opera.exe"| findstr /I "opera.exe")>nul && echo It's running || start "" "%LocalAppData%\Programs\Opera.exe"
timeout /t 20
goto :loop
I am very new to batch files and am trying to create a script that checks if a process is running and alert if the process is/is not running. This is what I have gotten from google, but isn't working the way I am wanting.
tasklist /FI "IMAGENAME eq example.exe" 2>NUL | find /I /N "example.exe">NUL if "%ERRORLEVEL%"=="0" echo Program is running
If this is batch (as the attempt from the question indicates), according to [MS.Docs]: Using multiple commands and conditional processing symbols:
&& [...]
command1 && command2
Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully.
Example:
C:\>(tasklist /FI "IMAGENAME eq svchost.exe" 2>NUL | findstr /I /N "svchost.exe" >NUL) && (echo Program running)
Program running
C:\>(tasklist /FI "IMAGENAME eq svchost1.exe" 2>NUL | findstr /I /N "svchost1.exe" >NUL) && (echo Program running)
You are trying to over complicate it. Copy the below into a batch file. Also use findstr instead.
tasklist /fi "imagename eq example.exe" | findstr /i "example.exe" >nul
If %errorlevel%==0 echo example.exe running.
If %errorlevel%==1 echo example.exe Not running.
This is even less complicated, there's no need to filter by ImageName when you're using Find to filter too:
TaskList|Find /I "example.exe">Nul&&(Echo Running)||Echo Not running
I'm trying to log the memory usage of browsers for exemple for Chrome / Firefox etc.
For Firefox I can simply use this little command line:
tasklist /fo csv /fi "imagename eq firefox.exe" > DumpResults.csv
And this will nicely result with one proces and its usage. But when applying this train of thoughts to Chrome you'll get around 4 processes even when you did a clean launch of Chrome. Is there any way to sum the results?
Sorry for the stupid question but it's my first attempt to create a bat file.
An alternative…
#Echo Off
Set "sum=0"
For /F "Tokens=6-7 Delims=., " %%a In (
'TaskList /NH /FI "ImageName Eq chrome.exe"') Do Set/A sum+=%%a%%b
Set sum
Pause
…prevents using two loops
#echo off
set sum=0
for /f "tokens=5 delims=," %%x in ('tasklist /fo csv /nh /fi "imagename eq chrome.exe"') do (
for /f "tokens=1-4 delims=.K " %%a in (%%x) do set /a sum+=%%a%%b%%c%%d
)
echo Sum Chrome = %sum%
Maybe you have to adapt the delimiters to your local settings. Output on my computer is like:
C:\Users> tasklist /fo csv /nh /fi "imagename eq chrome.exe"
"chrome.exe","7744","Console","1","86.388 K"
"chrome.exe","7784","Console","1","1.312 K"
"chrome.exe","7920","Console","1","50.188 K"
...
I am in need of help to adding a not responding check to my .bat and restarting a server application when it is not responding. This is what I have so far.
#echo on
tasklist /nh /fi "imagename eq javaw.exe" | find /i "javaw.exe" >nul && (
echo Server is running.
//
I need a not responding check right afterward here. If the program is not responding,
taskkill /f /IM server.exe
taskkill /f /IM javaw.exe
cd C:\Users\Administrator\Desktop
serverstart.jar
else
exit
//
exit
) || (
echo Server process not found.
taskkill /f /IM server.exe
taskkill /f /IM javaw.exe
cd C:\Users\Administrator\Desktop
serverstart.jar
)
pause>nul
It is necessary for me to kill both of these process because it doesn't end eachother when it crashes apparently.
You already managed to filter by imagename.
You can also filter by Status (Running / Not Responding / Unknown):
tasklist /nh /fi "imagename eq java.exe" /fi "status eq not responding"
will give you any java.exe, which does not respond.
EDIT
well - when I think about it: I would try:
tasklist /nh /fi "imagename eq javaw.exe" /fi "status eq running` |find /i "javaw.exe" >nul && (
echo Server is running
rem nothing to do
) || (
echo Server is not running or not responding
rem restart service
)
I get the number of a specific process with the help of this thread:
How to count amount of processes with identical name currently running, using a batchfile
I hope to assign the result of this command to a variable, then compare the variable with a number. My code is listed as below:
#echo off
setlocal enabledelayedexpansion
set procName=chrome.exe
set a=tasklist /FI "IMAGENAME eq %procName%" 2>NUL | find /I /C "%procName%"
if !a! equ 1 (
echo !a!
echo %procName% starts to run...
) else (
echo !a!
echo %procName% has not run!
)
Here I got '0' for
'set a=tasklist /FI "IMAGENAME eq %procName%" 2>NUL | find /I /C "%procName%"' command.
It also gives me "Echo closed" hint for 'echo !a!'.
FYI, when running the following command in cmd
tasklist /FI "IMAGENAME eq chrome.exe" 2>NUL | find
/c /i "chrome.exe"
set a=tasklist /FI "IMAGENAME eq chrome.exe" 2>NUL
| find /c /i "chrome.exe"
the output is 16 and 0 respectively.
What's the reason? How could I assign the result of a command to a variable? How to compare the variable to a number?
Thank you so much!
Well, set a=tasklist /FI "IMAGENAME eq chrome.exe" 2>nul | find /c "chrome.exe" does not work for me either. Which is good because I don't know how that was supposed to work.
I believe that this will be faster, because it doesn't have the overhead of FIND.EXE and writing, reading and deleting proc_temp.
set a=0
for /f "skip=3" %%x in ('tasklist /FI "IMAGENAME eq chrome.exe"') do set /a a=a+1
echo Total chrome.exe tasks running: %a%
EDIT: I just discovered that set /a does not require expanded variables and so removed the setlocal and endlocal commands and altered the set /a syntax.
after this line in environment a has the pid of the process sought
for /F "tokens=1,2,*" %%a in ('tasklist /fi "imagename eq %procName%"') do if !%%a!==!%procName%! set a=%b
I think I found a solution:
tasklist /fi "imagename eq %procName%" 2>nul | findstr /i %procName% | find /c /v "">proc_temp
set /p current_num= < proc_temp
echo !current_num!
Also I think the code can be simplified. Hope some of you can give brief version :)