title: Findstr Results Manipulation & Loop Batch File
body: <p>I am working on a script that gathers values from a file that are in a line of txt and I find these values by using findstr with referencing a key word. The process I want to accomplish is this:</p>
  
  <ol>
  <li>Find VALUE1 in the file if the line contains the key word “word1” and display VALUE1.</li>
  <li>Find VALUE2 in the file if the line contains the key word “word1”.</li>
  <li>Find VALUE3 in the file if the line contains the key word “word2” (I also know that this value is going to be on a separate line).</li>
  <li>Add VALUE2 and VALUE3 and display this value as TOTAL.</li>
  <li>Loop through file again and with steps 1-4 and display another TOTAL if there is another set of VALUE1, VALUE2, and VALUE3.</li>
  </ol>
  
  <p>Example file (test.txt):</p>
  
  <pre><code>Hello today is 05022017 and you have 4 apples 
  Also it is good to know that you bought 5 peaches 
  Hello today is 05032017 and you have 5 apples 
  Also it is good to know that you bought 6 peaches
  </code></pre>
  
  <p>Key word: “apples”, “peaches”</p>
  
  <p>Desired output:</p>
  
  <pre><code>  First value is: 05022017 
    Second value is: 4 
    Third value is: 5 
    TOTAL: 9
    First value is: 05032017 
    Second value is: 5 
    Third value is: 6 
    TOTAL: 11
  </code></pre>
  
  <p>Current code:</p>
  
  <pre><code>@echo off
  
  FOR /f "tokens=4,8" %%a in ('type test.txt ^|findstr /i "apples"') do (
  
     echo(First value is: %%a
  
     echo(Second value is: %%b
  
  )
  
  FOR /f "tokens=10" %%c in ('type test.txt ^|findstr /i "peaches"') do (
  
     echo(Value on another line is: %%c
  
  )
  
  echo(All done!
  
  echo(&amp;pause&amp;goto:eof
  </code></pre>
  
  <p>Current output:</p>
  
  <pre><code>First value is: 05022017
  Second value is: 4
  First value is: 05032017
  Second value is: 5
  Value on another line is: 5
  Value on another line is: 6
  </code></pre>
  
  <p>I understand why my code is working the way it does. I know I have two separate loops where I find the first and second values first and then I find the third values which are on separate lines. I have tried to use vectors, nested loops, and assigning values to variables but I can’t seem to get what I want working. </p>
  
  <p>I want to be able to run this script on a file with N possibilities which means it should work with both these file situations:</p>
  
  <pre><code>Hello today is 05022017 and you have 4 apples 
  Also it is good to know that you bought 5 peaches
  </code></pre>
  
  <p>Or </p>
  
  <pre><code>Hello today is 05022017 and you have 4 apples 
  Also it is good to know that you bought 5 peaches 
  Hello today is 05022017 and you have 4 apples 
  Also it is good to know that you bought 5 peaches 
  Hello today is 05022017 and you have 4 apples 
  Also it is good to know that you bought 5 peaches 
  Hello today is 05022017 and you have 4 apples 
  Also it is good to know that you bought 5 peaches 
  Hello today is 05022017 and you have 4 apples 
  Also it is good to know that you bought 5 peaches
  </code></pre>
  
  <p>I would greatly appreciate any help I can get or some guidance in how I can approach this differently.</p>
  
  <p>Thank you!</p>
  
  <p>UPDATE Working Code:</p>
  
  <pre><code>SETLOCAL ENABLEDELAYEDEXPANSION
  SET "sourcedir=I:\Tests"
  SET "filename1=%sourcedir%\test.txt"
  SET "word1=apples"
  SET "word2=peaches"
  
  FOR /f "tokens=4,8,9,10,11 delims= " %%a IN ('findstr "%word1% %word2%" "%filename1%"') DO (
   IF /i "%%~c"=="%word1%" (
   REM apple line
    ECHO First value is: %%a
    ECHO Second value is: %%b
    SET /a value2=%%b
   )
   IF /i "%%~e"=="%word2%" (
   REM peaches line
    ECHO Third value is: %%d
    SET /a total=value2 + %%d
    ECHO Total: !total!
   )
  )
  
  GOTO :EOF
  </code></pre>