【问题标题】:How to get file names included in a list file depending on strings read from one more file?如何根据从另一个文件读取的字符串获取列表文件中包含的文件名?
【发布时间】:2015-11-24 17:25:20
【问题描述】:

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

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

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

ABC, IJK, HIG

我需要从product_to_test.txt 拆分并读取值,并从testsuite.txt 获取相应的正确测试套件。

例如:

  1. 如果产品是ABC,那么它应该获取测试套件ProductABC_TestSuite.txt
  2. 如果产品是ABC, IJK,那么它应该获取ProductABC_TestSuite.txt(空格)ProductIJK_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%

【问题讨论】:

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

标签: batch-file subroutine


【解决方案1】:

这基于您提供的“示例”。

@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 

【讨论】:

    【解决方案2】:

    这里还有一个注释批处理脚本,它不使用 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 /?
    • exit /?
    • for /?
    • goto /?
    • if /?
    • rem /?
    • set /?
    • setlocal /?

    【讨论】:

      猜你喜欢
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-27
      相关资源
      最近更新 更多