Missing operator error using batch file - batch-file

I've been trying to create a little batchscript to get the usages of your browser. So far so good everything works, it does what it should. Then I moved the file to another pc and now I'm getting "Missing Operator" errors eventho the program runs like it should. Any idea's?
#echo off
set date = %date
set time = %time
set sum=0
for /f "tokens=5 delims=," %%x in ('tasklist /fo csv /fi "imagename eq firefox.exe"') do (
for /f "tokens=1-5 delims=.K " %%a in ("%%~x") do set /a sum+=%%a%%b%%c%%d
)
echo %date%, %time%, firefox.exe, %sum%K > FirefoxDumpResult.csv
pause
:start
set date = %date
set time = %time
set sum=0
for /f "tokens=5 delims=," %%x in ('tasklist /fo csv /fi "imagename eq firefox.exe"') do (
for /f "tokens=1-5 delims=.K " %%a in ("%%~x") do set /a sum+=%%a%%b%%c%%d
)
echo %date%, %time%, firefox.exe, %sum%K >> FirefoxDumpResult.csv
set choice=
set /p choice="Do you want to log another one? Press 'y' and enter for Yes: "
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='y' goto start

You have not placed the closing percent characters on your %DATE% and %TIME% variables.
Additionally you shouldn't be creating variables which already exist and which don't need setting anyhow.
You have also pointlessly repeated a section of your code.
Finally you have not used the simpler code I provided for you in an earlier reply to another similar question using chrome.exe.
Try this:
#Echo Off
If /I Not "%CD%\" Equ "%~dp0" CD /D %~dp0
>FirefoxDumpResult.csv Type Nul
:Start
Set "_sum=0"
For /F "Tokens=6-7 Delims=., " %%a In (
'TaskList /NH /FI "ImageName Eq firefox.exe"') Do Set/A _sum+=%%a%%b
Echo=%DATE%, %TIME%, firefox.exe, %_sum%K>>FirefoxDumpResult.csv
Echo=
Echo=Firefox process information logged
Echo=
Choice /M "Do you want to log another one?"
If ErrorLevel 2 Exit/B
GoTo :Start
These are the outputs from both your and my versions:
::-------------------------------- Akorna Output -------------------------------
Type Nul 1>FirefoxDumpResult.csv
Set _sum=0
For /F "Tokens=6-7 Delims=., " %a In ('TaskList /NH /FI "ImageName Eq firefox.exe"') Do Set/A "_sum+=%a%b"
Set/A "_sum+=414 032K" Missing operator.
Echo=wo 07/09/2016, 16:39:43,48, firefox.exe, 414K 1>>FirefoxDumpResult.csv
::------------------------------------------------------------------------------
::-------------------------------- Compo Output --------------------------------
Type Nul 1>FirefoxDumpResult.csv
Set "_sum=0"
For /F "Tokens=6-7 Delims=., " %a In ('TaskList /NH /FI "ImageName Eq firefox.exe"') Do Set/A _sum+=%a%b
Set/A _sum+=333232
Echo=09/09/2016, 10:23:22.56, firefox.exe, 333232K 1>>FirefoxDumpResult.csv
::------------------------------------------------------------------------------
You have clearly altered the script; please make sure, at least, that line five in the script you're using matches the one I posted.

Related

Is there a way to do this less time consuling within one check?

I have this batch code and its working perfectly well, BUT:
every line take ~60 seconds to execute and I'm wondering, if I could do this faster...
Also the if Statements.. I want to check, if every Window is closed, and if so, I want to execute something. But if at least one window is still open, then it should check it again.
:loop
for /f %%i in ('tasklist /v /fi "WINDOWTITLE eq %server1%" /FO Table') do set #1=%%i
for /f %%i in ('tasklist /v /fi "WINDOWTITLE eq %server2%" /FO Table') do set #2=%%i
for /f %%i in ('tasklist /v /fi "WINDOWTITLE eq %server3%" /FO Table') do set #3=%%i
for /f %%i in ('tasklist /v /fi "WINDOWTITLE eq %server4%" /FO Table') do set #4=%%i
for /f %%i in ('tasklist /v /fi "WINDOWTITLE eq %server5%" /FO Table') do set #5=%%i
for /f %%i in ('tasklist /v /fi "WINDOWTITLE eq %server6%" /FO Table') do set #6=%%i
if not %#1%==cmd.exe (
if not %#2%==cmd.exe (
if not %#3%==cmd.exe (
if not %#4%==cmd.exe (
if not %#5%==cmd.exe (
if not %#6%==cmd.exe (
goto backup
)
)
)
)
)
) else (
echo back to loop
goto openWindow
)
Let me suggest a slightly different approach. Instead of all those if statements, you can just loop whenever one of the tasks exist:
:wait
timeout 1 >nul
for %%a in (%server1% %server2% %server3% %server4% %server5% %server6%) do (
tasklist /nh /fi "windowtitle eq %%a" |find " " >nul && goto :wait
)
echo all closed.
or
setlocal enabledelayedexpansion
:wait
timeout 1 >nul
for /l %%a in (1,1,6) do (
tasklist /nh /fi "windowtitle eq !server%%a!" |find " " >nul && goto :wait
)
echo all closed.
Note: find " " looks for two consecutive spaces, not a TAB)
If you choose your window titles wisely, you don't even need a for loop:
:wait
timeout 1 >nul
tasklist /nh /fi "windowtitle eq MySubWindow*" |find " " >nul && goto :wait
echo all closed.
where the window titles all start with a fixed string (MySubWindow here), like MySubWindow-1, MySubWindow-2 etc. (yes, tasklist is able to use a wildcard - but only at the end of the string). This is basically "if any window exists with a title that starts with MySubWindow then loop"
little optimization of Stephan answer.
This execute faster.
:wait
timeout 1 >nul
for %%a in (%server1% %server2% %server3% %server4% %server5% %server6%) do (
if not defined v%%a tasklist /nh /fi "windowtitle eq %%a" |find " " >nul && goto :wait
set v%%a=done
)
echo all closed.
You have not explained how your 6 Batch files are "open", but if they are open via START command, then there is a much simpler way to do the same:
(
start call batch1.bat
start call batch2.bat
start call batch3.bat
start call batch4.bat
start call batch5.bat
start call batch6.bat
) | pause
echo All 6 Batch files are closed
The previous code run the 6 Batch files in parallel and then the control flow is stopped at the pause command. When all the 6 Batch files terminates, this program continue.
Note that there is not any Batch code that check if the procesess ends; this is done automatically by the Operating System. In this way, the wait state of this program does not waste any CPU time. For a further explanation, see this answer
Without being able to test this, perhaps it would be quicker to run just one tasklist command per loop:
#Echo Off
SetLocal EnableExtensions
:Loop
For /F Tokens^=17^ Delims^=^" %%G In (
'%SystemRoot%\System32\tasklist.exe /Fi "ImageName Eq cmd.exe" /Fo CSV /NH /V 2^>NUL'
) Do Set /P "=%%G" 0<NUL | %SystemRoot%\System32\findstr.exe /I ^
"\<%server1%\> \<%server2%\> \<%server3%\> \<%server4%\> \<%server5%\> \<%server6%\>" 1>NUL && GoTo Loop
:Backup
The findstr.exe options may need to be adjusted depending upon the content of those variables.

How to check if multiple processes is running via a batch script

I got this script form stackoverflow and its working for me . and because i can't comment there im asking my question in new post.
This script check if exe is running in tasklist:
#echo off
SETLOCAL EnableExtensions
set EXE=notepad.exe
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto ProcessFound
goto ProcessNotFound
:ProcessFound
echo %EXE% is running
goto END
:ProcessNotFound
echo %EXE% is not running
goto END
:END
echo Finished!
The question is :
How can I check multiple process is running on tasklist?
for example exe1 and exe2
thanks in advance
From your comments; if all you want to do is run a third executable if neither of two other executables are running then here is a single line complete batch file example for you:
#TaskList/NH /FI "Status Eq Running"|FindStr/IC:"first.exe" /C:"second.exe">Nul||Start "" "X:\PathTo\third.exe"
Note:Do not change anything other than the names first, second and X:\PathTo\third; all double quotes, ", are necessary!
I've organised your code a bit differently so it's easier to follow and has more functionality. Note: This means it will be slower if you have lots of processes. If you wish to only see if it exists, I'd recommend using findstr.
I've added REM (batch-file equivalent for comments) explaining what each section does.
#echo off
REM Create variable's for exe's and their counter
set exe_1=notepad.exe
set exe_2=explorer.exe
set exe_3=chrome.exe
set "count_1=0"
set "count_2=0"
set "count_3=0"
REM Store all tasklist findings in a temp file
>tasklist.temp (
tasklist /NH /FI "IMAGENAME eq %exe_1%"
tasklist /NH /FI "IMAGENAME eq %exe_2%"
tasklist /NH /FI "IMAGENAME eq %exe_3%"
)
REM Go through all finds and count for each task instance
for /f %%x in (tasklist.temp) do (
if "%%x" EQU "%exe_1%" set /a count_1+=1
if "%%x" EQU "%exe_2%" set /a count_2+=1
if "%%x" EQU "%exe_3%" set /a count_3+=1
)
REM Use variables to see instance count
Echo %exe_1%: %count_1%
Echo %exe_2%: %count_2%
Echo %exe_3%: %count_3%
REM Use GTR 0 to see if process exists
if %count_1% GTR 0 if %count_2% GTR 0 Echo Both notepad and explorer are open
REM Delete temp file once finished. (NB: Will still exist if your code crashes)
del tasklist.temp
Conditional if-statements
As requested from your comment:
if %count_1% GTR 0 if %count_2% GTR 0 (
Echo Both notepad and explorer are open
goto :finish
)
if %count_1% GTR 0 (
Echo Only notepad is open
goto :finish
)
if %count_2% GTR 0 (
Echo Only explorer is open
goto :finish
)
REM Not Finished means none are open
Echo Neither notepad nore explorer are open
:finish
Here is my solution, keeping a similar structure to that of your sample script. Modify the EXE variable to reference the IMAGENAMEs you're interested in checking.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET EXE=(notepad.exe wordpad.exe winword.exe thunderbird.exe outlook.exe greenshot.exe)
FOR %%i IN %EXE% DO (
TASKLIST /NH /FI "IMAGENAME EQ %%i" 2>NUL | FIND /I /N "%%i">NUL
IF !ERRORLEVEL! EQU 0 ( CALL :ProcessFound %%i ) ELSE ( CALL :ProcessNotFound %%i )
)
ECHO Finished^^!
EXIT /B 0
:ProcessFound
ECHO %1 is running
EXIT /B 0
:ProcessNotFound
ECHO %1 is not running
EXIT /B 1
If you want to start the program when the process is not found, insert START %1 after ECHO %1 is not running.

Close All Program Except these with CMD

i have this code :
This is a batch file code
#echo off
title Kill all running apps - Bharat Balegere - AgniPulse.com
cd c:\windows\System32
for /f "skip=3 tokens=1" %%i in ('TASKLIST /FI "USERNAME eq %userdomain%\%username%" /FI "STATUS eq running"') do (
if not "%%i"=="svchost.exe" (
if not "%%i"=="explorer.exe" (
if not "%%i"=="cmd.exe" (
if not "%%i"=="tasklist.exe" (
echo.
taskkill /f /im "%%i"
echo.
)
)
)
)
)
pause
but it will close all of the program
i try to edit it and add something like this if not "%%i"=="notepad.exe" (
but its not working anyway and when i click on batch file its nothing
so i don't know how to edit this
and put some except on it!
thanks in advance
You can give a try for this batch file with a whitelist for killing process
Of course you can modify your whitelist variable as you need :
#echo off
Title Kill all running apps - Bharat Balegere - AgniPulse.com
set "whitelist=avast mbam dllhost conhost"
Set "whitelist=%whitelist% taskeng skype"
Set "whitelist=%whitelist% firefox explorer cmd"
Set "whitelist=%whitelist% chrome tasklist taskmgr notepad"
#For /f "skip=3 tokens=1" %%i in (
'TASKLIST /FI "USERNAME eq %userdomain%\%username%" /FI "STATUS eq running" ^| findstr /VI "%whitelist%"'
) Do (
echo Taskkill /IM "%%i" /F
)
pause & exit
You can of course remove the echo command if that you want this list to be killed !
EDIT :
And here is another batch with whitelist using WMIC
#echo off
Title Process killer with a white list
Mode con cols=50 lines=40 & color 9B
setlocal
Set "White_Process_List=%~dpn0_White_Process_List.txt"
set "Ignoring_Process_List=%~dpn0_Ignoring_Process_List.txt"
set "Terimnated_Process_List=%~dpn0_Terimnated_List.txt"
If exist "%Ignoring_Process_List%" Del "%Ignoring_Process_List%"
If exist "%Terimnated_Process_List%" Del "%Terimnated_Process_List%"
:Whitelist
set "whitelist=Microsoft Mozilla Google Avast Panda ESET Kaspersky Avira AVG Bitdefender Malwarebytes Norton McAfee GAS IBM Skype"
If not exist "%White_Process_List%" echo %whitelist% > "%White_Process_List%"
Set /p whitelist=<"%White_Process_List%"
:Analyze
for /f "tokens=2 delims=," %%I in (
'wmic process get executablepath^,status /format:csv ^| find "\"'
) do (
set "proc=%%~I"
setlocal enabledelayedexpansion
wmic datafile where "name='!proc:\=\\!'" get manufacturer 2>nul | findstr /i "%whitelist%" >nul && (
( echo Ignoring : %%~nxI & echo "%%~I"
echo --------------------------------------- )>>!Ignoring_Process_List! 2>&1
) || (
( echo Terminating: %%~nxI )>con
( echo Terminating : %%~nxI & echo "%%~I"
Taskkill /im "%%~nxI" /f /t
echo --------------------------------------- )>>!Terimnated_Process_List! 2>&1
)
endlocal
)
Timeout /T 2 /nobreak>nul

Batch file check if a program is running only once and close duplicate instances or launch it if none is running

I'm trying to realize a batch file that checks if a program is running and if not launch it, and more if is it launched more than one time close all the duplicate instances.
this is the code I implemented using also some tips found here in stackoverflow:
:check
tasklist /fi "imagename eq notepad.exe" /nh |find /i /c "notepad.exe" > "%temp%\variable.txt" #here I count how many instances are running
set /p value=<"%temp%\variable.txt" #and save the value in the variable.txt file
##check and choose action
if %value% equ nul goto none
if %value% geq 2 goto more
if %value% equ 1 goto one
:more
for /f "tokens=2" %%x in ('tasklist ^| findstr notepad.exe') do set PIDTOKILL=%%x
taskkill /F /PID %PIDTOKILL%
goto check
:none
start notepad.exe
goto check
:one
timeout 10 > nul
goto check
But some strange behavior happens when I test it...
if only one instances is running all fine, but if I close notepad while the batch file is running the routine goes to the :more label apparently without any reason...what I'm doing wrong?
thanks for any help
stupid error ... this is the working code:
:check
timeout 10 > nul
tasklist /fi "imagename eq notepad.exe" /nh |find /i /c "notepad.exe" > "%temp%\variable.txt"
set /p value=<"%temp%\variable.txt"
if %value% equ 0 goto none
if %value% geq 2 goto more
if %value% equ 1 goto check
:more
for /f "tokens=2" %%x in ('tasklist ^| findstr notepad.exe') do set PIDTOKILL=%%x
taskkill /F /PID %PIDTOKILL%
goto check
:none
start notepad.exe
goto check

Compare number of a specific process to a number

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 :)

Resources