{"id":9544,"date":"2021-12-01T17:26:42","date_gmt":"2021-12-01T17:26:42","guid":{"rendered":"https:\/\/lgildv5i97.onrocket.site\/answers\/?post_type=question&#038;p=9544"},"modified":"2021-12-01T17:27:44","modified_gmt":"2021-12-01T17:27:44","slug":"batch-file-for-loop-increment","status":"publish","type":"question","link":"https:\/\/computing.net\/answers\/programming\/batch-file-for-loop-increment\/24168.html","title":{"rendered":"Batch File For Loop Increment"},"content":{"rendered":"<p>I&#8217;ve got a two part batch file that runs on a system schedule on win2k3 server.<\/p>\n<p>Part 1 uses WMI to get the current load on the system and write to a CSV file every five minutes. &#8211; works.<\/p>\n<p>Part 2 takes that CSV file and calculates average load. &#8211; fail<\/p>\n<p>My problem is down to incrementing the counter for the lines and adding to the running total for calculating the average. Here&#8217;s what I have, but when I run it, TOTALLOAD never has any value other than that last value from the CSV file, and counter is always 0.<\/p>\n<p>@echo off &amp; setlocal enabledelayedexpansion<\/p>\n<p>REM check cmd line args for no or 1 arg<br \/>\nif &#8220;%1&#8243;==&#8221;&#8221; goto msg<br \/>\nif &#8220;%2&#8243;==&#8221;&#8221; goto setArg<\/p>\n<p>:setArg<br \/>\nset node=%1<br \/>\ngoto loopFile<\/p>\n<p>:loopFile<\/p>\n<p>more %node%\\cpuload.txt &gt; temp.txt<\/p>\n<p>REM set globals<br \/>\nset \/a counter=0<br \/>\nset \/a TOTALLOAD=0<\/p>\n<p>for \/F &#8220;tokens=1,2,3 delims=,&#8221; %%m in (&#8216;findstr \/v &#8220;Node,LoadPercentage,MaxClockSpeed&#8221; temp.txt&#8217;) do (<br \/>\nset localMachine=%%m<br \/>\nset CPUload=%%n<br \/>\nset CPUspeed=%%o<br \/>\nset \/a counter+=1<br \/>\nset \/a TOTALLOAD=%TOTALLOAD%+%CPUload%<br \/>\necho Count is: %counter%<br \/>\n)<\/p>\n<p>REM del temp.txt<br \/>\necho totalLoad is: %totalLoad%<br \/>\nset \/a averageLoad=%totalLoad% \/ %counter%<br \/>\necho &#8220;%averageLoad%&#8221; &gt;&gt; %node%\\dailyAverage.txt<br \/>\nREM del %node%\\cpuload.txt<br \/>\ngoto end<\/p>\n<p>:msg<br \/>\necho You may specify at one or two computer name(s). Using localhost<br \/>\nset node=%computername%<br \/>\necho %node%<br \/>\ngoto loopFile<\/p>\n<p>:end<\/p>\n<p>Hopefully enough people still use batch files out there that can help me. Thanks<\/p>\n","protected":false},"author":5,"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-9544","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\/9544","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/comments?post=9544"}],"wp:attachment":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/media?parent=9544"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question-category?post=9544"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question_tags?post=9544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}