Search code examples
windowsbatch-filefor-loopfindstr

Findstr Results Manipulation & Loop Batch File


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:

  1. Find VALUE1 in the file if the line contains the key word “word1” and display VALUE1.
  2. Find VALUE2 in the file if the line contains the key word “word1”.
  3. 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).
  4. Add VALUE2 and VALUE3 and display this value as TOTAL.
  5. Loop through file again and with steps 1-4 and display another TOTAL if there is another set of VALUE1, VALUE2, and VALUE3.

Example file (test.txt):

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

Key word: “apples”, “peaches”

Desired output:

  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

Current 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(&pause&goto:eof

Current output:

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

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.

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:

Hello today is 05022017 and you have 4 apples 
Also it is good to know that you bought 5 peaches

Or

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

I would greatly appreciate any help I can get or some guidance in how I can approach this differently.

Thank you!

UPDATE Working 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

Solution

  • @ECHO OFF
    SETLOCAL ENABLEDELAYEDEXPANSION
    SET "sourcedir=U:\sourcedir"
    SET "filename1=%sourcedir%\q44875889.txt"
    SET "word1=%~1"
    SET "word2=%~2"
    
    FOR /f "tokens=4,8,9,10,11delims= " %%a IN ('findstr /Li /c:"%word1%" /c:"%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
    

    You would need to change the setting of sourcedir to suit your circumstances.

    I used a file named q44875889.txt containing your data for my testing.

    Much better to provide us with realistic data.

    The processing should be obvious - read each line of output of findstr which is looking for either of the words and selecting appropriate tokens.

    If the first word appears in the required position, process it and save the required value2. Ditto second word, calculating total and using delayed expansion to show the run-time value of total

    It doesn't make apparent sense to attempt to process lines that are not of the required format.


    Important (but originally omitted) running instructions:

    run as

    thisbatchname apples peaches

    The two words supplied are then substituted for word1/2 in my original posting.

    (and since no parameters were being supplied, word1/2 were empty, hence the error message) enter image description here