Batch file 如何根据从一个或多个文件读取的字符串获取列表文件中包含的文件名?

Batch file 如何根据从一个或多个文件读取的字符串获取列表文件中包含的文件名?,batch-file,subroutine,Batch File,Subroutine,我有文本文件testsuite.txt,其中包含记录,即测试套件值: ProductABC_TestSuite.txt ProductDEF_TestSuite.txt ProductHIG_TestSuite.txt ProductIJK_TestSuite.txt 我有另一个文件product_to_test.txt,其中包含: ABC, IJK, HIG 我需要从product\u到_test.txt中拆分并读取值,然后从testsuite.txt中获取相应的正确测试套件 例如: 如果

我有文本文件
testsuite.txt
,其中包含记录,即测试套件值:

ProductABC_TestSuite.txt
ProductDEF_TestSuite.txt
ProductHIG_TestSuite.txt
ProductIJK_TestSuite.txt
我有另一个文件
product_to_test.txt
,其中包含:

ABC, IJK, HIG
我需要从
product\u到_test.txt
中拆分并读取值,然后从
testsuite.txt
中获取相应的正确测试套件

例如:

  • 如果产品是
    ABC
    ,那么它应该获取测试套件
    ProductABC\u TestSuite.txt
  • 如果产品是
    ABC,IJK
    ,那么它应该获取
    ProductABC\u TestSuite.txt
    (空格)
    ProductIJK\u TestSuite.txt
  • 我尝试了以下代码。但它不起作用:

    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%
    
    这是基于您提供的“示例””实现的

    @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 
    
    @echo关闭
    延迟扩展
    
    set/p strings=这里还有一个已注释的批处理脚本,它不使用findstr命令

    @echo off
    rem Are both files existing in current directory?
    rem Exit batch job if one of the two files is missing.
    if not exist "testsuite.txt" goto :EOF
    if not exist "product_to_test.txt" goto :EOF
    
    rem Setup a new environment for the remaining processing. This environment
    rem is discarded with command endlocal which restores previous environment.
    setlocal EnableExtensions EnableDelayedExpansion
    set "TestSuites="
    set "Products="
    
    rem Assign first line from products file to a variable.
    rem All other lines are ignored if there are more lines.
    for /F "usebackq eol=| delims=" %%P in ("product_to_test.txt") do (
        set "Products=%%~P"
        goto ProcessProducts
    )
    
    :ProcessProducts
    rem Was the file not empty?
    if "%Products%" NEQ "" (
        rem Split this line up and search for each product in test suites file.
        for %%P in (%Products%) do call :GetTestSuite "%%P"
    )
    
    rem Was any test suite file name found at all?
    if not "%TestSuites%" == "" echo The test suites are: %TestSuites%
    
    endlocal
    goto :EOF
    
    rem Search in test suites file for first file name containing the
    rem product name of current product to test and when found append
    rem it to the test suites variable. The first one is not appended
    rem to avoid a space at beginning.
    
    :GetTestSuite
    set "Product=%~1"
    for /F "usebackq eol=| delims=" %%T in ("testsuite.txt") do (
        set "Suite=%%T"
        if /I "!Suite:%Product%=!" NEQ "%%T" (
            if "%TestSuites%" == "" (
                set "TestSuites=%%T"
            ) else (
                set "TestSuites=%TestSuites% %%T"
            )
            exit /B
        )
    )
    exit /B
    
    要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

    • echo/?
    • endlocal/?
    • 退出/?
    • 获取/?
    • goto/?
    • 如果/?
    • rem/?
    • 设置/?
    • setlocal/?

    您是否尝试过删除ABC、IJK、HIG in product_to_test.txt之间的所有空格?