batch-filesubroutine

How to get file names included in a list file depending on strings read from one more file?


I have text file testsuite.txt which contains records, i.e. test suites value:

ProductABC_TestSuite.txt
ProductDEF_TestSuite.txt
ProductHIG_TestSuite.txt
ProductIJK_TestSuite.txt

I have another file product_to_test.txt which contains:

ABC, IJK, HIG

I need to split and read the value from product_to_test.txt and get the corresponding right test suite from testsuite.txt.

For example:

  1. If product is ABC then it should fetch the test suite ProductABC_TestSuite.txt.
  2. If product is ABC, IJK then it should fetch ProductABC_TestSuite.txt (space) ProductIJK_TestSuite.txt

I have tried the following code. But it is NOT working:

for /F "tokens=*, delimiter=," %%A in (product_to_test.txt) do (
    call :sub %%A
)

:sub
    SETLOCAL ENABLEEXTENSIONS
    set product=%1
    set "test_suite="
    SETLOCAL EnableDelayedExpansion
    for /F "tokens=*" %%A in (testsuites.txt) do (
        set str=%%A
        Echo.!str! | findstr /C:"!product!">nul
        if !errorlevel!==0 set test_suite=!test_suite! !str!
    )
    echo %test_suite%

Solution

  • This works based on the "examples" you provided.

    @echo off
    setlocal enabledelayedexpansion
    set /p strings=<product_to_test.txt
    set strings=%strings:,=%
    
    for /F "delims=" %%A in ('findstr "%strings%" testsuite.txt') do (
        set "test_suite=!test_suite!%%A "
    )
    echo %test_suite%
    pause