VolAnd
VolAnd

Reputation: 6407

Parsing text file in windows command file with findstr

I am writing bat-file to use several executable files (tools that I cannot change) sequentially. Firs of the tool receive two file names and produce txt file.

Example 1:

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

Example 2:

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

Example 3:

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

Strings are different because of including full file path. But, fortunately names of file ("a1_score.txt " and "a2_score.txt") will not be changed.

I need two numbers to call next tool, so I tried to write script … but I'm not familiar with the command language and examples I have found in the Internet are not so useful. Please, help ne to complete the following code:

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

Upvotes: 0

Views: 2517

Answers (1)

JosefZ
JosefZ

Reputation: 30258

Next code snippet could help:

@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

Resources (required reading):

Upvotes: 1

Related Questions