Bat文件,它从文本中获取特定行的特定变量

时间:2013-06-06 07:08:02

标签: batch-file

我需要创建一个搜索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:"=%

非常感谢大家。

3 个答案:

答案 0 :(得分: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切换。 你可以用一些echo来看看会发生什么,比如

for /f "tokens=1* delims=" %%e in ("%%d") do (
    echo Compare: !Line_num!==%%e
    if !Line_num!==%%e set var2=!FullVersion!
    echo !var2! 
)

答案 1 :(得分: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%

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

答案 2 :(得分: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%
相关问题