Bat file, count lines in if statement.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jargen
Posts: 3
Joined: 26 Oct 2011 07:24

Bat file, count lines in if statement.

#1 Post by jargen » 26 Oct 2011 07:26

I know how to count the number of lines using console: FIND /C /I "<TD" example.html
However how do i make a if statement so if the lines are above 100 then it executes a command, but if not simply exits?


Any help is appreciated.
Thanks,

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Bat file, count lines in if statement.

#2 Post by dbenham » 26 Oct 2011 07:59

When you provide a file name to FIND, the count is prefixed with dashes followed by the file name: and then the count. A FOR loop can set : as the delimiter and use tokens to specify which token is the count. There is a problem though if the file name may include the path with drive letter. This gives the prospect of an additional :, so we are not sure which token represents the count. The problem can be solved, but it is simpler to pipe the contents of the file to FIND instead. This results in only the count being reproduced so there is no token parsing issue.

assuming you are including this in a batch file:

Code: Select all

for /f %%C in ('type example.html ^| find /c /i "<TD"') do if %%C gtr 100 (command) else exit /b

I would probably restructure the above as follows:

Code: Select all

for /f %%C in ('type example.html ^| find /c /i "<TD"') do if %%C leq 100 exit /b
command


Dave Benham

jargen
Posts: 3
Joined: 26 Oct 2011 07:24

Re: Bat file, count lines in if statement.

#3 Post by jargen » 26 Oct 2011 08:23

Thanks for the help however it dosent appear to quite be working. I changed the txt file to the correct name and changed exit to pause (to do some debugging) and i get this

C:\Users\Jargen>for /F %C in ('type programs.txt | find /c /i "<TD"') do if %C L
EQ 2 pause /b

C:\Users\Jargen>if 0 LEQ 2 pause /b

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Bat file, count lines in if statement.

#4 Post by dbenham » 26 Oct 2011 14:53

I'm not following - what did you expect?

Dave Benham

jargen
Posts: 3
Joined: 26 Oct 2011 07:24

Re: Bat file, count lines in if statement.

#5 Post by jargen » 26 Oct 2011 15:03

It does that no matter what how many lines the txt file has.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Bat file, count lines in if statement.

#6 Post by dbenham » 26 Oct 2011 16:52

Are you trying to simply count the number of lines in the file
or the number of lines that contain "<TD" :?:

I tested the code on a file that contained "<TD" and it worked fine.

Dave Benham

Post Reply