Im wanting to be able to print this line to a file:
tasklist /FI "IMAGENAME eq %ZOEXT%" 2>NUL | find /I /N "%ZOEXT%">NUL
But its proving to be difficult, to try and make it work properly i tried splitting it up considerably. I eventually got down to this
set TASKER=task
echo %TASKER%
pause
set PROPTASKLIST=%TASKER%list /FI "IMAGENAME eq
echo %PROPTASKLIST%
pause
set POINT=">
set POINT=%POINT:"=%
echo %POINT%
pause
set NULLER=NUL
echo %NULLER%
pause
set TONULL=%POINT%%NULLER%
echo %TONULL%
pause
set F=f
echo %F%
echo set F=f >> boot.bat
set FIND=%| %F% ind %/I %/N
echo %FIND%
echo %PROPTASKLIST% %%ZOEXT%%" 2%TONULL% %FIND% "%%ZOEXT%%"%TONULL% >> boot.bat
The "| FIND" doesn't seem to work, and all my attempts to set ">NUL" to anything has proved fruitless.
Current attempt was to set it to "">" which doesn't kill cmd, and then strip the character out later. But that made it crash anyway (or maybe im doing character stripping badly)
It looks like you just don't know how to escape things. Try this:
echo tasklist /FI "IMAGENAME eq %%ZOEXT%%" 2^>NUL ^| find /I /N "%%ZOEXT%%"^>NUL>>boot.bat
Related
bat file that should echo lines to another .bat file (). I'm sure it was working fine, but for some reason is not now...
echo #echo off > %USERPROFILE%\Documents\Richmond\check_node.bat
echo pushd >> %USERPROFILE%\Documents\Richmond\check_node.bat
echo tasklist /nh /fi "imagename eq node.exe" | find /i "node.exe" > nul ||(start %USERPROFILE%\Documents\Richmond\server.bat) >> %USERPROFILE%\Documents\Richmond\check_node.bat
Outputs only...
#echo off
pushd
It's completely ignoring the last line.
Any help would be appreciated.
You need to escape characters when you want to create a batch file like that :
Give a try for this modification :
#echo off
Set "check_node=%USERPROFILE%\Documents\Richmond\check_node.bat"
Set "server=%USERPROFILE%\Documents\Richmond\server.bat"
(
echo #echo off
echo pushd
echo tasklist /nh /fi "imagename eq node.exe" ^| find /i "node.exe" ^>nul ^|^|(start "" "%server%"^)
)> "%check_node%
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.
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 :)
Write variables to file as %VAR% not as the value of the variable, as well ask making tasklist function properly when redirecting output.
Code im using to write to file:
echo tasklist /FI "IMAGENAME eq %ZOEXT%" 2>NUL | find /I /N "%ZOEXT%">NUL >> boot.bat
echo if "%ERRORLEVEL%"=="1" start /d "%ZODIR%" %ZOEXT% >> boot.bat
Result:
tasklist /FI "IMAGENAME eq Zoiper.exe"
if "0"=="1" start /d "C:\Documents and Settings\mgladman\Desktop\Zoip\Zoiper Communicator\" Zoiper.exe
What I want added to boot.bat:
tasklist /FI "IMAGENAME eq %ZOEXT%" 2>NUL | find /I /N "%ZOEXT%">NUL
if "%ERRORLEVEL%"=="1" start /d "%ZODIR%" %ZOEXT%
If you want i can publish whole code not just segment, it is a cool script :P Just has this small issue.
Only "workaround" i can think is to make the first script, to echo echo in the first file and write the correct system data (this has to be a portable script, so that would work, but would be messy as)
You can escape the % using ^ if you are in command line.
For example:
set var=test
echo %var%
echoes test
set var=test
echo ^%var^%
echoes %var%
In batch file you have to use double % -
set var=1
echo %%var%%
echoes %var%
The % can be escaped inside of an batch with a percent.
echo %%var%%
This doesn't work on the command line, as the parser works there a bit different.
There didn't exists an escape character for the percent, but percents are preseverd if the variable doesn't exist.
set "var="
set "var2=content"
echo %var% %%var2%%
results in
%var% %content%
Just double the percentage signs to escape them, and use carets to escape most other special characters. Here is how I would do it:
(echo tasklist /FI "IMAGENAME eq %%ZOEXT%%" 2^>NUL ^| find /I /N "%%ZOEXT%%"^>NUL)>>boot.bat
(echo if "%%ERRORLEVEL%%"=="1" start /d "%%ZODIR%%" %%ZOEXT%%)>>boot.bat
I am executing following command in a label inside a batch file:
tasklist.exe /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "IMAGENAME eq %1" /FI "PID eq %2" 2>nul && echo errorl:%errorlevel%
%1 is process running and %2 is its PID.
Even if process and its PID matches or doesnt matches, I m getting "errorl:1" in o/p.
I am not sure whats wrong here. Any idea?
You could pipe tasklist through the find command and get an errorlevel off of it.
Example:
tasklist | find "firefox.exe"
echo Error level = %ERRORLEVEL%
REM If firefox is running, the errorlevel is set to 0
REM If firefox is not running, errorlevel is set to 1
In my opinion, you can't use errorlevel at all,
because tasklist always returns a 0 even if the pid isn't found.
I suppose, you have to parse the output of tasklist.
To fetch the output of a command, FOR /F is a good choice.
To avoid problems wth the quoting in the FOR /F, I build first a cmd variable which is expanded with delayed expansion to avoid any side effects of special characters.
#echo off
setlocal enableDelayedExpansion
set "cmd=tasklist.exe /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "IMAGENAME eq %1" /FI "PID eq %2""
for /F "delims=*" %%p in ('!cmd! ^| findstr "%2" ') do (
echo found %%p
)
%variables% are expanded before executing the line, so %errorlevel% will expand to some old value. (The fact that the code after && executes at all is your clue that the command returned 0)
You options are:
Use %errorlevel% or the more correct IF errorlevel 1 ... on the next line
Call setlocal ENABLEDELAYEDEXPANSION first and then use !errorlevel!
Edit:
I guess tasklist is buggy and/or stupid when it comes to exit codes, I wrote some code that does not use the exit code at all:
#echo off
if "%~1"=="SOTEST" (
start calc
ping -n 2 localhost >nul
for /F "tokens=1,2 skip=3" %%A in ('tasklist /FI "IMAGENAME eq calc.exe"') do (
call "%~0" %%A %%B
)
call "%~0" dummy.exe 666
goto :EOF
)
goto main
:IsTaskRunning
setlocal ENABLEEXTENSIONS&set _r=0
>nul 2>&1 (for /F "tokens=1,2" %%A in ('tasklist /FO LIST %*') do (
if /I "%%~A"=="PID:" set _r=1
))
endlocal&set IsTaskRunning=%_r%&goto :EOF
:main
call :IsTaskRunning /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "IMAGENAME eq %1" /FI "PID eq %2"
if %IsTaskRunning% gtr 0 (echo.%1:%2 is running) else (echo.%1:%2 is NOT running)
Run it as test.cmd SOTEST and it prints:
calc.exe:4852 is running
dummy.exe:666 is NOT running
Easy solution to this, given that
1) you can't get an errorlevel from tasklist, and
2) you can't directly pipe it to a FIND
Just write it to a file using output redirection and use FIND to check the file. Each time this is run, it will overwrite the previous iteration, so no need to even do any file cleanup. Amazing how many bat/cmd file limitations can be overcome with a simple scratchpad file!!
:TOP
rem swap rems from good to bad to test
set findvar=goodfile.exe
rem set findvar=badfile.exe
set scratchfile=scratch.txt
tasklist /fi "status eq running" /fi "imagename eq %findvar%">%scratchfile%
type %scratchfile%
pause
echo Looking for %findvar%
find "%findvar%" %scratchfile%
echo Error level = %errorlevel%
pause
IF errorlevel 1 GOTO BAD
IF errorlevel 0 GOTO GOOD
GOTO OTHER
:BAD
echo Errrlevel 1 - task not found
PAUSE
GOTO TOP
:GOOD
echo Errrlevel 0 - task is running
pause
GOTO TOP
:OTHER
echo something else ????? you blew it somewhere!
tasklist returns 0 when executes successfully:
If you're looking for existence of some process or some attribute of a process, one method is to supply the attributes to tasklist and check if it returned any process names. If no matching processes are found, it'll return "INFO: No tasks are running which match the specified criteria."
The result of tasklist may be checked either via for command embedding (and parse command output) or filter via find or findstr, which accepts regular expressions & wildcards.
eg. To check if the any process is running with following criteria:
tasklist.exe /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "IMAGENAME eq %1" /FI "PID eq %2" | find /v "No task" >nul && (echo process exists) || (echo na man).
Above method can also find if any document (window) is open, in addition to the underlying process, like "firefox.exe".
eg. close high speed vpn ad window if/when it pops up without notice:
tasklist /fi "windowtitle eq High-Speed*" | find /v "No task" >nul && (taskkill /fi "windowtitle eq High-Speed*")
Tested on Win 10 CMD