{"id":13523,"date":"2021-12-13T11:03:32","date_gmt":"2021-12-13T11:03:32","guid":{"rendered":"https:\/\/lgildv5i97.onrocket.site\/answers\/?post_type=question&#038;p=13523"},"modified":"2021-12-13T11:04:19","modified_gmt":"2021-12-13T11:04:19","slug":"solved-batch-script-typeperf-exe-monitor-cpu-process","status":"publish","type":"question","link":"https:\/\/computing.net\/answers\/programming\/batch-script-typeperfexe-monitor-cpu-process\/29792.html","title":{"rendered":"Solved Batch Script Typeperf.exe MONITOR CPU PROCESS"},"content":{"rendered":"<p>MONITOR CPU PROCESS, Have been using some third party software to monitor the total overall process on my CPU this is handy but unfortunately other Anti virus software keeps flagging it as malicious software, I really want to use a script that can do this for me that will avoid these problems, I found this pice of code online from <a href=\"https:\/\/web.archive.org\/web\/20161223121129\/http:\/\/stackoverflow.com\/questions\/25215083\/batch-script-to-monitor-and-react-to-cpu-usage-for-a-certain-program\" target=\"_blank\" rel=\"nofollow noopener\">http:\/\/stackoverflow.com\/questions\/&#8230;<\/a> according to the user who requested help it works for him but I am getting an error message can anyone here tell me what&#8217;s wrong ?<\/p>\n<p>Here is my modified code below, all I have changed is adding the SET Variable to simplify setting an PROGRAMNAME.EXE to monitor.<\/p>\n<p>@echo on<br \/>\nsetlocal enabledelayedexpansion<br \/>\n:: &#8212;&#8212;&#8212;&#8212;&#8212; debug &#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n:selftst<br \/>\necho ERROR TEST LOG FLAG<br \/>\nIF NOT %1!==\/go! %0 \/go 1&gt;%~n0.log 2&gt;&amp;1<br \/>\nSHIFT<\/p>\n<p>ECHO %1 %2 %3 %4 %5<br \/>\n:: &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nset xyz=PROGRAMNAME.exe<\/p>\n<p>:check<\/p>\n<p>for \/f &#8220;skip=2 tokens=2 delims=,&#8221; %%c in (&#8216;typeperf &#8220;\\Process(%xyz%)\\%% Processor Time&#8221; -sc 1&#8217;) do (<br \/>\nset cpu_usage=%%~c<br \/>\ngoto :break<br \/>\n)<\/p>\n<p>:break<\/p>\n<p>echo %cpu_usage%<br \/>\nset cpu_usage=%cpu_usage:.=%<\/p>\n<p>:: 1 is set in the front to avoid octal comparison<br \/>\nif 1%cpu_usage% LSS 11000000 (<br \/>\ngoto :check_process<br \/>\n) else (<br \/>\ngoto :check<br \/>\n)<br \/>\n:: sleep for 1 second<br \/>\npathping 127.0.0.1 -n -q 1 -p 1000 &gt;nul 2&gt;&amp;1<br \/>\n:check_process<\/p>\n<p>QPROCESS * | find \/i &#8220;%xyz%&#8221; &gt;nul 2&gt;&amp;1 &amp;&amp; (<br \/>\necho process %xyz% is running<br \/>\n) || (<br \/>\necho process %xyz% is not running<br \/>\n)<\/p>\n<p>endlocal<\/p>\n<p>Below is the DUMP of the entire script.<\/p>\n<p>OUTPUT<\/p>\n<p>D:\\MERGE&gt;setlocal enabledelayedexpansion<\/p>\n<p>D:\\MERGE&gt;echo ERROR TEST LOG FLAG<br \/>\nERROR TEST LOG FLAG<\/p>\n<p>D:\\MERGE&gt;IF NOT \/go! == \/go! &#8220;D:\\MERGE\\cpumon1.bat&#8221; \/go 1&gt;cpumon1.log 2&gt;&amp;1<\/p>\n<p>D:\\MERGE&gt;SHIFT<\/p>\n<p>D:\\MERGE&gt;ECHO<br \/>\nECHO is on.<\/p>\n<p>D:\\MERGE&gt;set xyz=PROGRAMNAME.exe<\/p>\n<p>D:\\MERGE&gt;for \/F &#8220;skip=2 tokens=2 delims=,&#8221; %c in (&#8216;typeperf &#8220;\\Process(PROGRAMNAME.exe)\\% Processor Time&#8221; -sc 1&#8217;) do (<br \/>\nset cpu_usage=%~c<br \/>\ngoto :break<br \/>\n)<\/p>\n<p>D:\\MERGE&gt;echo<br \/>\nECHO is on.<\/p>\n<p>D:\\MERGE&gt;set cpu_usage=.=<br \/>\n= was unexpected at this time.<br \/>\nD:\\MERGE&gt;if 1.= LSS 11000000 (<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: gray; font-size: small;\">If Dreams Come True Oleg Would be Famous so far he&#8217;s very shy, so much for Being Famous \ud83d\ude09 <a href=\"https:\/\/web.archive.org\/web\/20161223121129\/http:\/\/www.book-stores.com\/angels-\" target=\"_blank\" rel=\"nofollow noopener\">http:\/\/www.book-stores.com\/angels-<\/a><br \/>\n<\/span><\/p>\n<p align=\"right\"><span style=\"font-size: xx-small;\"><i>message edited by Ortorea<\/i><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"iawp_total_views":2},"question-category":[55],"question_tags":[],"class_list":["post-13523","question","type-question","status-publish","hentry","question-category-programming"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question\/13523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question"}],"about":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/types\/question"}],"author":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/comments?post=13523"}],"wp:attachment":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/media?parent=13523"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question-category?post=13523"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question_tags?post=13523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}