batch-filesearchcmdfindsubstring

Variables are not set properly inside nested loops


I have a similar question like I did ask here: Nested loop

However, I still did not solve the "problem" which seems to be little. I already implemented the solution mentioned using [FindStr][2], however the runtime is much much longer than without FindStr. So I would like to keep the method using

IF not "!stringToTest:!searchValue!=!"=="!stringToTest!" echo Something

Below I post some runable code. In the beginning I just set my array with the values I would like to search for and then some exmaple "texts" in which I like to search my beforehand set searchvalues. My strategy:

  1. Loop through my file with the text lines. For each line test each SearchValue for presence.
  2. After all Searchvalues tested, go to ne next line and check also for the presence of each searchvalue.

So I have two nested Loops. At every step inside the loop I output the current vaule of the variables and those seems to be correct. I really do not know how my fault in this line is as the searchfunction is not working correctly:

IF not "!stringToTest:!searchValue!=!"=="!stringToTest!" echo Searchword is in Textline

There may be just a little mistake in here? I am very thankful for every tipp in this issue.

@echo off
setlocal enabledelayedexpansion
set /a counter=0
set "searchValues=abc,xyz"
FOR %%G in (%searchValues%) do (
    set arraySearchVal[!counter!]=%%G
    set /a counter+=1
)
REM set arraySearchVal
set /a counter-=1
set "TextWhichNeedToBeSearched=tzu,abc,qsd"
FOR %%G in (%TextWhichNeedToBeSearched%) do (
    set "stringToTest=%%G"
    echo Textline:         !stringToTest!   
    FOR /l %%I in (0,1,%counter%) do ( 
        set "searchValue=!arraySearchVal[%%I]!"
        echo Searchword:       !searchValue!
        REM echo !stringToTest! found in Textline | findstr "!searchValue!"
        IF not "!stringToTest:!searchValue!=!"=="!stringToTest!" echo Searchword is in Textline
    )
    echo/     
)
endlocal
pause

Solution

  • This falls under the K.I.S.S. methodology. By making your search arguments array variables you have over complicated your code. You can simplify it like so.

    @echo off
    setlocal enabledelayedexpansion
    
    set "searchValues=abc,xyz"
    set "TextWhichNeedToBeSearched=tzu,abc,qsd"
    
    FOR %%G in (%TextWhichNeedToBeSearched%) do (
        set "stringToTest=%%G"
        echo Textline:         !stringToTest!   
        FOR %%I in (%searchValues%) do ( 
            echo Searchword:       %%I
            IF not "!stringToTest:%%I=!"=="!stringToTest!" echo Searchword is in Textline
        )
        echo/     
    )
    endlocal
    pause
    

    If you really want to use your original code you can get the double variable expansion using a trick with the CALL command to set another variable. Then use that variable with the IF command.

    @echo off
    setlocal enabledelayedexpansion
    set /a counter=0
    set "searchValues=abc,xyz"
    FOR %%G in (%searchValues%) do (
        set arraySearchVal[!counter!]=%%G
        set /a counter+=1
    )
    REM set arraySearchVal
    set /a counter-=1
    set "TextWhichNeedToBeSearched=tzu,abc,qsd"
    FOR %%G in (%TextWhichNeedToBeSearched%) do (
        set "stringToTest=%%G"
        echo Textline:         !stringToTest!   
        FOR /l %%I in (0,1,%counter%) do ( 
            set "searchValue=!arraySearchVal[%%I]!"
            echo Searchword:       !searchValue!
            REM echo !stringToTest! found in Textline | findstr "!searchValue!"
            call set "temptest=%%stringToTest:!searchValue!=%%"
            IF not "!temptest!"=="!stringToTest!" echo Searchword is in Textline
        )
        echo/     
    )
    endlocal
    pause