如何根据从另一个文件读取的字符串获取列表文件中包含的文件名?
How to get file names included in a list file depending on strings read from one more file?
我有包含记录的文本文件 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
中获取对应的正确测试套件。
例如:
- 如果产品是
ABC
那么它应该获取测试套件 ProductABC_TestSuite.txt
。
- 如果产品是
ABC, IJK
那么它应该获取 ProductABC_TestSuite.txt
(space) 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%
这基于您提供的“示例”。
@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
这是另外一个不使用 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
要了解使用的命令及其工作原理,请打开命令提示符 window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
echo /?
endlocal /?
exit /?
for /?
goto /?
if /?
rem /?
set /?
setlocal /?
我有包含记录的文本文件 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
中获取对应的正确测试套件。
例如:
- 如果产品是
ABC
那么它应该获取测试套件ProductABC_TestSuite.txt
。 - 如果产品是
ABC, IJK
那么它应该获取ProductABC_TestSuite.txt
(space)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%
这基于您提供的“示例”。
@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
这是另外一个不使用 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
要了解使用的命令及其工作原理,请打开命令提示符 window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
echo /?
endlocal /?
exit /?
for /?
goto /?
if /?
rem /?
set /?
setlocal /?