使用findstr解析Windows命令文件中的文本文件

时间:2015-08-14 06:07:36

标签: batch-file cmd

我正在编写bat文件,以便按顺序使用几个可执行文件(我无法更改的工具)。 该工具的第一个接收两个文件名并生成txt文件。

示例1:

Best score for a1_score.txt: 5712
Best score for a2_score.txt: 14696

示例2:

Best score for data\a1_score.txt: 3023
Best score for data\a2_score.txt: 451

示例3:

Best score for D:\tmp\a1_score.txt: 234
Best score for D:\tmp\a2_score.txt: 1062

由于包含完整文件路径,字符串不同。但是,幸运的是,文件的名称(" a1_score.txt"和" a2_score.txt")将不会被更改。

我需要两个数字来调用下一个工具,所以我尝试编写脚本...但我不熟悉命令语言,我在Internet上找到的示例并没有那么有用。 请帮助完成以下代码:

if exist scores.dat goto scores
echo Cannot read scores
goto BADEND

:scores
for /f "delims=" %%a in ('findstr /c:a1_score.txt /c:a2_score.txt "scores.dat"') do (
    set str=%%a
    :: some code to create a1_score and a2_score with corresponding values

)
echo %a1_score%
echo %a2_score%

:: some other code

goto END
:BADEND
echo Process was not completed due to errors
:END
pause

1 个答案:

答案 0 :(得分:1)

下一段代码片段可以提供帮助:

@ECHO OFF >NUL
SETLOCAL enableextensions enabledelayedexpansion

set "scores_dat=%~dp0files\scores32003340.dat"   :: my filename value for debugging
if exist "%scores_dat%" goto scores

echo Cannot read scores
goto BADEND

:scores
for /f "tokens=1,2,* delims=:" %%a in ('
  findstr /R "a[12]_score.txt" "%scores_dat%"
  ') do (
    if "%%c"=="" (
      set str=%%a
      set /A "!str:~-12,8!=%%b"
    ) else (
      set str=%%b
      set /A "!str:~-12,8!=%%c"
    )
)
set a|find /I "score"
echo(%a1_score%
echo(%a2_score%

:: some other code

goto END
:BADEND
echo Process was not completed due to errors
:END

资源(必读):