1

我需要创建一个搜索 2 个文本的批处理文件。捕获变量中的一行文本(至少包含 3 个字符串中的一个,但不包含第四个字符串)及其行号。
搜索第二个文本并在另一个变量中捕获存在于第一个变量的行号上的文本行。

之后我还需要使用两行文本(变量)。
我通过第一个文本阅读进行了管理,但不确定我在第二个文本中做错了什么:

@echo off
set "found="
for /f "tokens=1,* delims=[]" %%a in (' find /n /v "" ^< "%LocalDir%\list.txt" ') do (
    echo "%%b"|findstr /i /c:"one two small" /c:"one two birds" /c:"one two strings" >nul && set found=1
    if defined found echo "%%b"|findstr /v /c:"one two small red apples" >nul || set "found="
    if defined found (
        echo %%a found
        @echo off & setLocal EnableDelayedExpansion
        set var=%%b
        set Line_num=%%a
        endlocal
    ) else ( 
        echo %%a NOT FOUND
    )
    set "found="
)

REM part2--------------------

for /f "delims=" %%d in (list1.txt) do (
    set FullVersion=%%d
    @echo off & setLocal EnableDelayedExpansion
    for /f "tokens=1* delims=" %%e in ("%%d") do (
        if !Line_num!==%%e
        set var2=!FullVersion!
        echo !var2! 
    )
)

endlocal

echo %var%
echo %var2%

任何帮助将不胜感激。

这是我最终得到的解决方案:

    for /f "tokens=1,* delims=[]" %%a in (' find /n /v "" ^< "%LocalDir%\software_list.txt" ') do (

echo "%%b"|findstr /i /c:"Micro Focus Enterprise " /c:"Micro Focus Visual" /c:"Micro Focus COBOL" >nul && set found=1

if defined found echo "%%b"|findstr /v /c:"Micro Focus Enterprise Server for .NET" >nul || set "found="
 if defined found (set LineNumber=%%a&set ProductName=%%b)
REM  else (echo Main Micro Focus product NOT FOUND. Nothing to do. Exit.&exit /b)
 set "found="
)


find "2." temp1.txt > temp3.txt
for /f "tokens=2,3 delims==" %%c in (temp3.txt) do (echo %%c >> %LocalDir%\software_list1.txt)
for /f "tokens=1*delims=[]" %%a in (' find /n /v "" ^< "software_list1.txt" ') do IF %%a==%LineNumber% SET ProductVersion=%%b 

REM ECHO %LineNumber%
REM ECHO %ProductName%
REM ECHO %ProductVersion%

set ProductName=%ProductName:"=%
set ProductName=%ProductName: =%
set ProductVersion=%ProductVersion:"=%
set ProductVersion=%ProductVersion: =%




set out_file_name=%ProductName%_%ProductVersion%_%COMPUTER_NAME%
REM echo %out_file_name:"=%

非常感谢大家。

4

3 回答 3

1

我在您的代码中看到了一些问题:
这个块没有意义,因为它在新的 setlocal 上下文中设置变量,并且在 endlocal 之后变量丢失了。

    @echo off & setLocal EnableDelayedExpansion
    set var=%%b
    set Line_num=%%a
    endlocal

在第二个块中,您为每次迭代打开一个 setlocal 上下文,这将导致溢出错误。
而 Part2 之后的 endlocal 似乎也适得其反。

该行if !Line_num!==%%e总是会产生语法错误

顺便提一句。为什么要@echo off在代码中使用?批处理开始时的第一个应该足够了。

您应该只setlocal EnableDelayedExpansion在脚本的开头使用一个。
如果您遇到感叹号问题,您应该只使用 DelayedExpansion 切换。你可以使用一些回声来看看会发生什么,比如

for /f "tokens=1* delims=" %%e in ("%%d") do (
    echo Compare: !Line_num!==%%e
    if !Line_num!==%%e set var2=!FullVersion!
    echo !var2! 
)
于 2013-06-06T07:40:30.260 回答
0
@echo off

rem I need to create a batch file that searches through 2 text FILEs.
rem Captures a line of text in a variable (that contains at least one of 3
rem strings, but doesn't contain forth string) and its line number.

set Line_num=
for /F "tokens=1* delims=:" %%a in (
       'findstr /N /I /C:"one two small" /C:"one two birds" /C:"one two strings" "%LocalDir%\list.txt"
     ^| findstr /V /C:"one two small red apples"' ) do (

   echo %%a found
   set var=%%b
   set Line_num=%%a
)

REM part2--------------------

if defined Line_num (

   rem Searches through the second text and captures in another variable 
   rem the line of text that exists on the line-number of the first variable.

   for /F "tokens=1* delims=:" %%d in ('findstr /N "^" list1.txt') do (
      if %Line_num% == %%d (
         set var2=%%e
         echo %%e
      )
   )
)

echo %var%
echo %var2%
于 2013-06-06T11:58:39.673 回答
0
@echo off
set "found="
for /f "tokens=1*delims=[]" %%a in (
  ' find /n /v "one two small red apples" ^< "%LocalDir%\list.txt" ') do (
 echo "%%b"|findstr /i /c:"one two small" /c:"one two birds" /c:"one two strings" >NUL
 IF NOT ERRORLEVEL 1 SET lnbr=%%a&SET ltext=%%b
)

for /f "tokens=1*delims=[]" %%a in (' find /n /v "" ^< "list1.txt" ') do IF %%a==%lnbr% SET L1txt=%%b 

ECHO(line number %lnbr%
ECHO(from LIST   %ltext%
ECHO(from LIST1  %L1txt%

这应该做你想要的 - 如果我理解正确的话。更好地显示您的数据和所需输出的示例。试图修复不做未定义的事情的代码是令人沮丧的。

于 2013-06-06T09:23:48.263 回答