Page 1 of 1
Bat file, count lines in if statement.
Posted: 26 Oct 2011 07:26
by jargen
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,
Re: Bat file, count lines in if statement.
Posted: 26 Oct 2011 07:59
by dbenham
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
Re: Bat file, count lines in if statement.
Posted: 26 Oct 2011 08:23
by jargen
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
Re: Bat file, count lines in if statement.
Posted: 26 Oct 2011 14:53
by dbenham
I'm not following - what did you expect?
Dave Benham
Re: Bat file, count lines in if statement.
Posted: 26 Oct 2011 15:03
by jargen
It does that no matter what how many lines the txt file has.
Re: Bat file, count lines in if statement.
Posted: 26 Oct 2011 16:52
by dbenham
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