"If - Then" needed for text searcher

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

"If - Then" needed for text searcher

#1 Post by mjfoxtrot » 02 Jun 2014 19:26

This is my first post on this board. I appreciate any help that someone can provide.

I use a relatively simple batch script in conjunction with the excellent "findrepl.bat" script that I downloaded from DOS Tips. The end result is that the batch scripts will scan a folder containing 48 .txt files, find the second line of each text file, and copy all those lines to a "TextID.txt" file. This is the script that I invoke to do it:

@echo off
set linenum=2
for %%a in (.\split\*.txt) do (
type "%%a"|findrepl /o:%linenum%:%linenum% >>tmp.tmp
)
move /y tmp.tmp "TextID.txt"

It works great with one small exception: two of the 48 .txt files have blank lines on line #2. So, the line that I need on those two files is on line #3. What I need is to modify the above script so that it contains an if-then statement to check if line number two is blank, and if it is, it will then type the text from line #3. It seems like this would be fairly easy to do, but I am new to batch scripting and I just can't get my head around it. Can someone please help me?

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

Re: "If - Then" needed for text searcher

#2 Post by foxidrive » 02 Jun 2014 19:52

Test this - it uses more IO but for 48 files it should be pretty swift.

The first test checks for any alphanumeric character in line 2 - if one isn't found then it'll use line 3.

Code: Select all

@echo off
setlocal enabledelayedexpansion
for %%a in (.\split\*.txt) do (
type "%%a"|findrepl /o:2:2 |findstr "[a-zA-Z0-9]" > nul
if errorlevel 1 (set linenum=3) else (set linenum=2)
type "%%a"|findrepl /o:!linenum!:!linenum! >>tmp.tmp
)
move /y tmp.tmp "TextID.txt"

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

Re: "If - Then" needed for text searcher

#3 Post by mjfoxtrot » 02 Jun 2014 20:11

Very nice, Foxidrive. It does what I need, although you are certainly right that it takes more time to run the scan. But hey, I couldn't figure it out, and now I have a solution thanks to you! Much appreciated. By any chance, could you walk me through how your change solved the problem? I am trying to understand the concepts. I am a self-taught tinkerer in getting code to work, and I'm interested in knowing more.

"enabledelayedexpansion" seems to have been the key here, no?

Thanks again.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: "If - Then" needed for text searcher

#4 Post by Yury » 02 Jun 2014 21:29

One more variant:


Code: Select all

@echo off
for %%a in (.\split\*.txt) do (
 (
 for /l %%b in (1 1 2) do (
  set line=
  set /p "line="
  if %%b==2 (
   call:# "%%a"
   )
  )
 )<"%%a"
 )
move /y tmp.tmp "TextID.txt"
exit /b

:#
if defined line (
 set linenum=2
 ) else (
 set linenum=3
 )
type %1|findrepl /o:%linenum%:%linenum% >>tmp.tmp
exit /b



.

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

Re: "If - Then" needed for text searcher

#5 Post by mjfoxtrot » 02 Jun 2014 22:02

Thanks, Yuri, but for some reason, the variant you sent does not pick up the third line where it is supposed to. Here is an example of the TextID file it produces:

***** Indy Colts TEAM REPORT *****
***** Acadia Black Fly TEAM REPORT *****
***** AMERICAN LEAGUE STANDINGS *****
***** AMERICAN TEAM-by-TEAM RECORDS *****

***** AMERICAN LEAGUE SCORING REPORT *****
***** AMERICAN LEAGUE PITCHING REPORT *****
***** AMERICAN LEAGUE FIELDING REPORT *****

. . . as you can see, it is including a blank line from one of the .txt files that has its first line of text on line 3. Not sure why it is not picking it up. Too bad, because the batch file ran VERY fast. If you see any possibility of fixing it, please let me know.

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

Re: "If - Then" needed for text searcher

#6 Post by foxidrive » 02 Jun 2014 22:25

Try this - it will be much more efficient as it will only reprocess the same file if line 2 did not contain any alphanumeric characters.

Code: Select all

@echo off
for %%a in (.\split\*.txt) do (
type "%%a"|findrepl /o:2:2 |findstr "[a-zA-Z0-9]" >>tmp.tmp
if errorlevel 1 type "%%a"|findrepl /o:3:3 >>tmp.tmp
)
move /y tmp.tmp "TextID.txt"

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

Re: "If - Then" needed for text searcher

#7 Post by foxidrive » 02 Jun 2014 22:41

mjfoxtrot wrote:could you walk me through how your change solved the problem? I am trying to understand the concepts. I am a self-taught tinkerer in getting code to work, and I'm interested in knowing more.

"enabledelayedexpansion" seems to have been the key here, no?


In my first solution it effectively checked each file twice - it checked each file for alphanumeric characters on line 2, and findstr returns an errorlevel to
show if it found the characters or not. Errorlevel 1 means it did not find the characters, and errorlevel 0 means that it did find one.

So the next line sets the appropriate number in the variable and setlocal enabledelayedexpansion allows you to change and use a variable within a loop, but replacing the usual % with !

My second solution checks line 2 in the same way but redirects the line to the file, and if no alphanumeric characters were found on line 2 then it processes the file again using line 3.
The regular expression [a-zA-Z0-9] means to show the line if it contains any character between a to z, or A to Z or from 0 to 9.

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

Re: "If - Then" needed for text searcher

#8 Post by mjfoxtrot » 02 Jun 2014 23:40

Absolutely brilliant, Foxidrive. Your follow-up script also worked like a charm, except now it runs more than twice as fast. The first script took 17 seconds; this one took 7! Woo-hoo!

And thank you as well for the explanation. I appreciate the chance to be able to understand how the script works.

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

Re: "If - Then" needed for text searcher

#9 Post by mjfoxtrot » 03 Jun 2014 04:15

One other quick thought on this: is it possible in the output file (in this case, TextID.txt) to add the name of the text file that each line was extracted from? In other words, if the line was extracted from split01.txt, the resulting line would be split01: ***** AMERICAN LEAGUE STANDINGS *****

I suppose it would also be cleaner if the multiple "*" were removed, too, although I can live with them if it adds too much processing time. But having the filename in each line would be helpful. I looked around and see a fair amount of discussion in forums about prepending or appending a filename to a line, but once again, it is unclear to me how it could be interjected into the script without breaking its functionality.

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

Re: "If - Then" needed for text searcher

#10 Post by mjfoxtrot » 03 Jun 2014 11:25

Gave that a try and got the dreaded "I was unexpected at this time" error. My understanding is that is usually attributable to a missing "%" somewhere but I fiddled with a few different variations and nothing worked.

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

Re: "If - Then" needed for text searcher

#11 Post by foxidrive » 04 Jun 2014 03:44

The code was more broken than that - try this, it works here.

It expects the *** to start on column one.

Code: Select all

@echo off
setlocal enabledelayedexpansion
(for %%a in (.\split\*.txt) do (
set "line="
for /f "tokens=1 delims=*" %%b in ('type "%%a" ^|findrepl /o:2:2 ') do set "line=%%b"
if not defined line for /f "tokens=1 delims=*" %%b in ('type "%%a" ^|findrepl /o:3:3 ') do set "line=%%b"
echo(%%a: !line!
))>"TextID.txt"
pause

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

Re: "If - Then" needed for text searcher

#12 Post by mjfoxtrot » 04 Jun 2014 07:14

Ah, I see, now I get it. Yes, this works like a charm. The "***" in the raw files have spaces in front of them, but a quick grep pass-through can take care of that. I gave it a test run and everything looks perfect; the file name and path precedes the title header on all 48 files. Wow, does this make my life easier. Thanks so much.

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

Re: "If - Then" needed for text searcher

#13 Post by foxidrive » 04 Jun 2014 07:35

mjfoxtrot wrote:The "***" in the raw files have spaces in front of them, but a quick grep pass-through can take care of that.


I'm glad to hear it works for you.

You can change tokens=1 to tokens=2 in both places and it should handle the spaces in front (they will be token 1 and the next block after the *** will be token 2).

Cheers

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

Re: "If - Then" needed for text searcher

#14 Post by mjfoxtrot » 04 Jun 2014 12:58

Yep, changing the tokens to 2 took care of everything. Fabulous. Here's a sample of the output:

.\split\split01.txt: NATIONAL LEAGUE STANDINGS
.\split\split02.txt: NATIONAL TEAM-by-TEAM RECORDS
.\split\split03.txt: NATIONAL LEAGUE BATTING REPORT
.\split\split04.txt: NATIONAL LEAGUE SCORING REPORT
.\split\split05.txt: NATIONAL LEAGUE PITCHING REPORT
.\split\split06.txt: NATIONAL LEAGUE FIELDING REPORT
.\split\split07.txt: NATIONAL BATTING LEADERS 2014
.\split\split08.txt: NATIONAL PITCHING LEADERS 2014

Foxidrive, I am writing you into my will ;)

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

Re: "If - Then" needed for text searcher

#15 Post by foxidrive » 04 Jun 2014 15:11

mjfoxtrot wrote:Foxidrive, I am writing you into my will ;)


hehe I might kick the bucket before you though. ;)

Post Reply