Batch file for parsing names out of text files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mjfoxtrot
Posts: 23
Joined: 02 Jun 2014 19:03

Batch file for parsing names out of text files

#1 Post by mjfoxtrot » 07 Apr 2015 01:55

I've had wonderful help on this board before and I am hoping someone might have a solution for my latest quandary. Please bear with my long-winded explanation, as I want to make sure that I'm including all relevant information.

My goal: a batch file that will go through the box scores generated by my MS-DOS baseball program and generate a text file that contains the name of the starting pitchers for each game.

The box scores are simple .txt files. They are each in subdirectories like this:

C:\1DOS\Factory\BAN @ IND\GAME1.TXT
C:\1DOS\Factory\BAN @ IND\GAME2.TXT
C:\1DOS\Factory\BAN @ IND\GAME3.TXT
C:\1DOS\Factory\BAN @ IND\GAME4.TXT
C:\1DOS\Factory\MID @ ACA\GAME1.TXT

. . . and so on and so on. The number of subdirectories may vary from three or four to as many as 34.

What I need is for the batch file to read each text file in the subdirectories and find the starting pitchers. There is a pattern that I think would make this possible. The starting pitcher is always listed on the line immediately following this line:

-Pitchers- IP R ER H 2B 3B HR SO BB WP HB BK

. . . the next line contains the starting pitcher's name and his stats, like so:

PORCELLO L 6.0 2 2 3 1 0 0 2 2 0 0 0 0 2 0 0

What I am hoping for is that the batch file would output all the lines containing the starting pitchers and their stats to a separate text file. So it would be like this:

PORCELLO L 6.0 2 2 3 1 0 0 2 2 0 0 0 0 2 0 0
HERNANDEZ W 7.1 1 1 1 0 0 1 9 1 0 0 0 0 0 0 0
GIBSON L 5.1 5 5 11 5 0 0 5 0 0 0 0 1 1 0 0
TEHERAN W 7.0 1 1 4 1 0 0 8 1 0 0 0 0 0 0 0

. . . and so on and so on. Ideally, each line would be prefaced by the name of text file and the subdirectory that the text file resides in, i.e.,

BAN @ IND: GAME1.TXT: PORCELLO L 6.0 2 2 3 1 0 0 2 2 0 0 0 0 2 0 0
BAN @ IND: GAME1.TXT: HERNANDEZ W 7.1 1 1 1 0 0 1 9 1 0 0 0 0 0 0 0
BAN @ IND: GAME2.TXT: GIBSON L 5.1 5 5 11 5 0 0 5 0 0 0 0 1 1 0 0
BAN @ IND: GAME2:TXT: TEHERAN W 7.0 1 1 4 1 0 0 8 1 0 0 0 0 0 0 0


If it helps, I have included the full text of one of the box scores:


************************************ BOXSCORE *******************************


TEAM 1 2 3 4 5 6 7 8 9 R H E DATE/TIME
Dragons 0 0 0 0 0 1 0 0 2 3 6 0 04-06-15
Colts 0 0 0 2 2 1 4 0 0 9 16 0 9:21pm

Bangor Last
Dragons Pos AB R H 2B 3B HR TB RBI S HP BB SO SB CS PO A E PB
1 DYSON LF 2 1 1 0 0 0 1 0 0 0 2 0 1 0 0 0 0 0
2 LAGARES CF 4 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0
3 VOTTO 1B 4 1 1 0 0 1 4 1 0 0 0 0 0 0 7 3 0 0
4 SEAGER 3B 3 1 1 1 0 0 2 1 0 0 1 0 0 0 1 1 0 0
5 LeMAHIEU 2B 4 0 3 0 1 0 5 1 0 0 0 0 0 0 2 1 0 0
6 MONTERO C 4 0 0 0 0 0 0 0 0 0 0 1 0 0 9 1 0 0
7 ANDRUS SS 4 0 0 0 0 0 0 0 0 0 0 2 0 0 1 2 0 0
8 BLANCO RF 4 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0
9 GIBSON P 2 0 0 0 0 0 0 0 0 0 0 2 0 0
VEAL P 0 0 0 0 0 0 0 0 0 0 0 0 0 0
PARRA P 1 0 0 0 0 0 0 0 0 0 0 0 0 0
--- -- -- -- -- -- -- --- -- -- -- -- -- --
Totals 32 3 6 1 1 1 12 3 0 0 3 8 1 0

-Pitchers- IP R ER H 2B 3B HR SO BB WP HB BK
GIBSON L 5.1 5 5 11 5 0 0 5 0 0 0 0 1 1 0 0
VEAL 1.1 4 4 4 0 0 0 2 1 1 0 0 1 0 0 0
PARRA 1.1 0 0 1 0 0 0 2 0 0 0 0 0 1 0 0
---- --- --- --- --- --- --- --- --- -- -- -- -- -- -- --
Totals 8.0 9 9 16 5 0 0 9 1 1 0 0 24 10 0 0

INJURIES:


Indy Last
Colts Pos AB R H 2B 3B HR TB RBI S HP BB SO SB CS PO A E PB
1 GORDON 2B 4 3 2 1 0 0 3 1 0 0 1 1 1 0 2 4 0 0
2 DE AZA RF 5 1 3 0 0 0 3 2 0 0 0 1 0 1 1 0 0 0
3 JONES CF 4 0 0 0 0 0 0 0 0 0 0 3 0 0 6 0 0 0
4 LONGORIA 3B 4 1 1 0 0 0 1 1 0 0 0 2 0 0 2 1 0 0
5 DAVIS 1B 1 0 1 1 0 0 2 0 0 0 0 0 0 0 2 0 0 0
GIMENEZ 1B 3 2 3 2 0 0 5 2 0 0 0 0 0 0 3 0 0 0
6 SEGURA SS 4 0 2 1 0 0 3 2 0 0 0 0 0 0 1 0 0 0
HUDSON P 0 0 0 0 0 0 0 0 0 0 0 0 0 0
MATUSZ P 0 0 0 0 0 0 0 0 0 0 0 0 0 0
COLEMAN P 0 0 0 0 0 0 0 0 0 0 0 0 0 0
7 PEREZ C 4 0 2 0 0 0 2 1 0 0 0 0 0 0 8 0 0 0
8 BROWN LF 4 1 1 0 0 0 1 0 0 0 0 0 1 0 2 0 0 0
9 TEHERAN P 2 0 0 0 0 0 0 0 0 0 0 1 0 0
GARCIA SS 2 1 1 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0
--- -- -- -- -- -- -- --- -- -- -- -- -- --
Totals 37 9 16 5 0 0 21 9 0 0 1 9 2 1

-Pitchers- IP R ER H 2B 3B HR SO BB WP HB BK
TEHERAN W 7.0 1 1 4 1 0 0 8 1 0 0 0 0 0 0 0
HUDSON 1.0 2 2 2 0 1 1 0 2 0 0 0 0 0 0 0
MATUSZ 0.1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
COLEMAN 0.2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
---- --- --- --- --- --- --- --- --- -- -- -- -- -- -- --
Totals 9.0 3 3 6 1 1 1 8 3 0 0 0 27 6 0 0

INJURIES: DAVIS(1)



******************************* END OF BOXSCORE ****************************

ShadowThief
Expert
Posts: 984
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch file for parsing names out of text files

#2 Post by ShadowThief » 07 Apr 2015 03:33

It could probably stand to be tested a bit more thoroughly, but the results I got with this look promising.

Code: Select all

@echo off
setlocal enabledelayedexpansion
cls

pushd C:\1DOS\Factory\
for /F "delims=" %%A in ('dir /b /s Game*.txt') do (
   set place=%%~pA
   set place=!place:~-10,9!
   set game=%%~nA
   
   REM Get the line number containing the "-Pitchers-" string, and append the line after that to starters.txt
   for /F "tokens=1 delims=[]" %%B in ('find /N "-Pitchers-" "%%A" ^| find /V "-----"') do (
      call :add_line "!place!: !game!:" %%B "%%A"
   )
)
popd
exit /b

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Finds the string at line n+1 in file x and outputs it to another file
::
:: Arguments: %1 - Prefix for the string to output
::            %2 - Number of lines to skip
::            %3 - File to search
:: Returns:   None
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:add_line
set "prefix=%~1"
set getline=%2
set filename=%3
(
   for /L %%A in (1,1,%getline%) do set /p ignore=
   set /p starter=
)<%filename%

echo %prefix%!starter!>>starters.txt

mjfoxtrot
Posts: 23
Joined: 02 Jun 2014 19:03

Re: Batch file for parsing names out of text files

#3 Post by mjfoxtrot » 07 Apr 2015 04:22

Thanks for the help, ShadowThief. Unfortunately I get an "invalid predicate error" when running that batch file. The syntax of the error is:

find: invalid predicate '-Pitchers-'
find: invalid predicate '-----'

ShadowThief
Expert
Posts: 984
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch file for parsing names out of text files

#4 Post by ShadowThief » 07 Apr 2015 04:35

That's not any find error message I've ever seen. Are you using some weird third-party shell?

You may have to replace find with C:\Windows\System32\find.exe

mjfoxtrot
Posts: 23
Joined: 02 Jun 2014 19:03

Re: Batch file for parsing names out of text files

#5 Post by mjfoxtrot » 07 Apr 2015 05:25

Yup, I am using a weird third-party shell, and I should have thought of that as the reason for why the find command was not working right. I adjusted the batch file to call find with the c:\windows\system32\find.exe, as you recommend, and it worked perfectly. The text file is created EXACTLY as I need it to be. Thank you very much, ShadowThief. This file will save me a tremendous amount of time.

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Batch file for parsing names out of text files

#6 Post by foxidrive » 07 Apr 2015 05:27

This uses a native Windows batch script called findrepl.bat (by aacini)
- download from: https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat
and it can also be found here: viewtopic.php?f=3&t=4697

Place it in the same folder as the batch file, or in a folder that is on the system path.

Windows blocks various downloaded files to keep you on your toes...
it can be unblocked by this step:

* Right click the batch file
* select the "Properties" entry
* and click "Unblock"


Code: Select all

@echo off
set "folder=C:\1DOS\Factory"
(
for /r "%folder%" %%a in (game*.txt) do (
  for %%b in ("%%~pa\.") do (
    for /f "delims=" %%c in ('type "%%a" ^| findrepl "-Pitchers-" /o:1:1 ') do echo "%%~nxb":%%c
  )
 )
)>"%folder%\Pitcher results.txt"
echo done
pause
 

mjfoxtrot
Posts: 23
Joined: 02 Jun 2014 19:03

Re: Batch file for parsing names out of text files

#7 Post by mjfoxtrot » 07 Apr 2015 06:21

Very nice, foxidrive. I added findrepl.bat to my path, that is definitely a keeper ;)

The only thing that your batch file does not do that ShadowThief's does is to include the text file name in the output. It only lists the folder where the text file is contained, but does not include the name of the text file that it pulled the pitcher data from. Is there an easy way with your script to add the file name to each line? Here's what the output looks like:

"BCB @ MAD": DeSCLAFANI L 2.2 9 8 9 1 0 1 3 2 0 1 0 0 1 0 0
"BCB @ MAD": KLUBER W 6.0 1 1 4 2 0 0 5 3 0 0 0 0 1 0 0
"BCB @ MAD": HUTCHISON W 5.0 2 2 5 1 0 0 4 1 0 0 1 0 0 0 0
"BCB @ MAD": ROSS L 4.0 3 3 2 1 0 1 6 4 0 0 0 0 0 0 0
"BCB @ MAD": VENTURA L 6.0 4 4 5 1 0 1 7 1 0 0 0 0 1 0 0
"BCB @ MAD": LEE W 5.0 2 2 5 2 0 1 3 1 0 0 0 1 1 0 0
"BCB @ MAD": MINOR L 6.1 3 3 5 0 0 0 6 2 0 0 0 0 3 0 0
"BCB @ MAD": STRASBURG W 7.0 0 0 1 0 0 0 9 1 0 1 0 0 0 0 0
"FOR @ BAN": KURODA W 6.0 1 0 4 0 0 0 4 1 1 0 0 1 2 0 0
"FOR @ BAN": PORCELLO L 7.0 2 2 5 0 0 0 4 2 0 1 0 1 1 0 0
"FOR @ BAN": BUMGARNER W 8.0 2 0 3 2 0 0 7 2 0 0 0 1 1 0 0
"FOR @ BAN": GIBSON 6.0 2 2 7 0 0 0 3 2 2 0 0 1 1 0 0

Post Reply