Computer Hope

Software => Computer programming => Topic started by: devil0150 on March 08, 2009, 01:14:27 PM

Title: How many lines
Post by: devil0150 on March 08, 2009, 01:14:27 PM
Guys, im new here and i'm looking for a batch that shows me how many lines are there in some text document and set the output as variable.

If any of you can help me i'd be grateful.
Title: Re: How many lines
Post by: Dias de verano on March 08, 2009, 04:43:22 PM
Code: [Select]
@echo off
setlocal enabledelayedexpansion
set /a lines=0
set filename=whatever.txt
for /f "delims==" %%L in ( ' type "%filename%" ' ) do (
    set /a lines+=1
    )
echo there are %lines% lines in the file %filename%
Title: Re: How many lines
Post by: Reno on March 10, 2009, 02:28:09 AM
dias, how about blank lines?
Title: Re: How many lines
Post by: Dias de verano on March 10, 2009, 02:59:54 AM
dias, how about blank lines?

For /f ignores blank lines. If it is desired to count them, then that script will not give the desired result. Got a suggestion?





Title: Re: How many lines
Post by: Reno on March 10, 2009, 03:41:43 AM
no, i dont have other solution than the one posted.  ;D

since the OP doesn't include blank line as requirement, the batch works perfect.
Title: Re: How many lines
Post by: BC_Programmer on March 10, 2009, 03:51:17 AM
my method for counting lines, including blanks, would be to simply count the number of crlf pairs. Not sure how one would do that in batch.
Title: Re: How many lines
Post by: Dias de verano on March 10, 2009, 04:10:44 AM
This was bugging me, and this is what I came up with:


Code: [Select]
@echo off
setlocal enabledelayedexpansion
set filename="whatever.txt"
echo This is the file:
echo -------------------
type %filename%
echo -------------------

echo (1) include blank lines
set /a lines=0 & for /f "delims==" %%F in ('findstr /R /N /C:".*" %filename%') do set /a lines+=1
echo count result=%lines% lines

echo (2) Don't include blank lines
set /a lines=0 & for /f "delims==" %%F in ('findstr /R /N /C:"." %filename%') do set /a lines+=1
echo count result=%lines% lines

Code: [Select]
This is the file:
-------------------
Monday
Tuesday
Wednesday
Thursday
Friday

Saturday
Sunday
-------------------
(1) Include blank lines
count result=8 lines
(2) Don't include blank lines
count result=7 lines

Title: Re: How many lines
Post by: Dias de verano on March 10, 2009, 04:12:10 AM
my method for counting lines, including blanks, would be to simply count the number of crlf pairs. Not sure how one would do that in batch.

Or just LFs in Unix format text files (but we won't go there, since the OP said "batch".)

In essence, counting a CR/LF pair as a line is what findstr will do if the regex is ".*" (Of course it ignores the final cr/lf pair which DOS text files have)

(http://i124.photobucket.com/albums/p29/badoit/0D0A.jpg)

Of course, a "blank" line could have tabs, spaces, etc. (these are still ignored if the regex is ".")
Title: Re: How many lines
Post by: Reno on March 10, 2009, 06:30:36 AM
thanks for sharing the code. i haven't try regex before.

findstr /? reveal that regex can be powerful in string manipulation.
**going to google for regex tutorial.  :)
Title: Re: How many lines
Post by: devil0150 on March 10, 2009, 02:15:20 PM
Thanks everybody.
Very helpful.
Title: Re: How many lines
Post by: BC_Programmer on March 10, 2009, 06:22:16 PM
So, another way of doing it via a counting method would be to count the number of cr's, add the number of LFs, and subtract the number of crlf pairs.
Title: Re: How many lines
Post by: Dias de verano on March 11, 2009, 01:14:15 AM
So, another way of doing it via a counting method would be to count the number of cr's, add the number of LFs, and subtract the number of crlf pairs.

With DOS format files, that would be the equivalent of computing how many hands you had by counting the number of thumbs, adding the number of index fingers, and then subtracting the number of thumb/index pairs.

Title: Re: How many lines
Post by: BC_Programmer on March 11, 2009, 01:36:01 AM
indeed, but it will cover mac, unix, and DOS format files, giving an accurate count in each case.

count all the cr's for mac, count the lfs for unix, and since crlf pairs will be counted in that, subtract the number of crlf pairs.

But yes it is a roundabout way of doing it...
Title: Re: How many lines
Post by: Dias de verano on March 11, 2009, 01:44:06 AM
Ah now I see your cross-platform strategy.
Title: Re: How many lines
Post by: Dias de verano on March 11, 2009, 02:10:23 AM
See here

http://en.wikipedia.org/wiki/Newline
Title: Re: How many lines
Post by: ghostdog74 on March 14, 2009, 04:04:14 AM
install Gawk for windows (http://gnuwin32.sourceforge.net/packages/gawk.htm), then on the command line
Code: [Select]
c:\> gawk 'END{print NR}' file
17

if you want to do programming/batching on windows, get a real programming language.
Title: Re: How many lines
Post by: Reno on March 14, 2009, 04:21:19 AM
why install third party solution when you can do this in almost any windows-based PC without installing anything.
NT cmd.exe is a powerful interpreter for text based manipulation.

No need to install anything, then on the command line
Code: [Select]
C:\>type config.sys|find/c /v ""
2
Title: Re: How many lines
Post by: ghostdog74 on March 14, 2009, 09:03:38 AM
why install third party solution when you can do this in almost any windows-based PC without installing anything.
We don't live in prehistoric times. Given computing technology today, why is one restricted to what you have. If a tool can reduce hours of coding and increase productivity to the business, its best to use them all to our advantage. A computer is bought just to do that. Increase productivity. Your Windows goes out to the internet for auto updates. That's also installing, isn't it?. What's the difference.

Quote
NT cmd.exe is a powerful interpreter for text based manipulation.

No need to install anything, then on the command line
Code: [Select]
C:\>type config.sys|find/c /v ""
2
yes, its powerful, but not powerful enough. It can only do this much, anything more complex, it becomes ugly. try parsing a file and doing complex string manipulations EASILY using cmd.exe (and *.exe in system32). Try doing automated telnetting with proper error control. try grabbing a web page and getting some information out of it. Doing them in batch? no way.
Title: Re: How many lines
Post by: BC_Programmer on March 14, 2009, 09:10:30 AM
install Gawk for windows (http://gnuwin32.sourceforge.net/packages/gawk.htm), then on the command line
Code: [Select]
c:\> gawk 'END{print NR}' file
17

if you want to do programming/batching on windows, get a real programming language.

indeed, I always find it much more convenient to write a vbscript like this:

Code: [Select]
set fso = createobject("scripting.filesystemobject")
set fstream = fso.getfile(wsh.arguments(1)).OpenAsTextStream
wsh.echo fstream.readall
fstream.close

instead of the ridiculously difficult to understand:

Code: [Select]
type filename

Oh wait- NO I DON'T!


if you've seen some threads were I provide a VBScript solution and Dias provides a batch solution- oddly the batch solution is shorter.

And if you would like to contest wether VBScript is a programming language you might want to take that up with Eric Lieppert.
Title: Re: How many lines
Post by: ghostdog74 on March 14, 2009, 09:48:55 AM

if you've seen some threads were I provide a VBScript solution and Dias provides a batch solution- oddly the batch solution is shorter.

And if you would like to contest wether VBScript is a programming language you might want to take that up with Eric Lieppert.
you are going OT.
Title: Re: How many lines
Post by: BC_Programmer on March 14, 2009, 12:07:02 PM
We don't live in prehistoric times. Given computing technology today, why is one restricted to what you have. If a tool can reduce hours of coding and increase productivity to the business, its best to use them all to our advantage. A computer is bought just to do that. Increase productivity. Your Windows goes out to the internet for auto updates. That's also installing, isn't it?. What's the difference.
yes, its powerful, but not powerful enough. It can only do this much, anything more complex, it becomes ugly. try parsing a file and doing complex string manipulations EASILY using cmd.exe (and *.exe in system32). Try doing automated telnetting with proper error control. try grabbing a web page and getting some information out of it. Doing them in batch? no way.

And I'm the one going off-topic? Don't see much about counting the lines in a file there, mostly just a shameless plug for your ridiculous and unnecessary, if not useful, command-line tool of choice. (And no, unnecessary and useful are not mutually exclusive)

Which brings up the fact that windows updates- automatically,, as in, without requiring the user to search for an install a third party tool.

This is Reno's Point- you can make a third party tool as powerful as you want- but if it can be done in something that is included in the default install, whats the point of using a separate tool, third party or otherwise?

your proposed problems are merely invented scenarios devised for the sole purpose of being difficult- if not impossible- via CMD line batch processing commands; except perhaps the first one- since cscript.exe can be used to perform easy string manip via Jscript or VBScript code from batch; and in fact that goes for most of the other proposed "problems".

The funniest part is me and Dias are discussing various ways of performing this task with various levels of compatibility for different newline methods with tools provided in default OS installs and you jump in with a suggestion to install some third party tool and provide some code for it that accomplishes nothing more then that which Dias had already provided, except perhaps clutter up their PC with more software installations.

Hubris.
Title: Re: How many lines
Post by: Dias de verano on March 14, 2009, 12:46:10 PM
Although I quite like fooling around with awk, sed, perl, Ruby etc -- I am getting into Python at the moment -- also the Resource Kits can be useful -- I don't tend to push these others on people here because as you say, BC_Programmer, "if it can be done in something that is included in the default install, whats the point of using a separate tool, third party or otherwise?". Especially if people are prevented from installing 3rd party apps. In fact I get a kind of perverse pleasure in seeing what can be done with the default tools.
Title: Re: How many lines
Post by: ghostdog74 on March 14, 2009, 01:41:24 PM
And I'm the one going off-topic? Don't see much about counting the lines in a file there, mostly just a shameless plug for your ridiculous and unnecessary, if not useful, command-line tool of choice. (And no, unnecessary and useful are not mutually exclusive)
please read the quote where the reply is directed. A question was asked on "why install a third party ....". I gave the reasons why, mostly for productivity. So where did i go OT? Whereas for your case, since when did i say Vbscript is not a programming language and why should i contest the guy you mentioned? Isn't it going OT ? get your facts right.

Quote
Which brings up the fact that windows updates- automatically,, as in, without requiring the user to search for an install a third party tool.
is searching for a third party tool that difficult?

Quote
This is Reno's Point- you can make a third party tool as powerful as you want- but if it can be done in something that is included in the default install, whats the point of using a separate tool, third party or otherwise?
like i said, productivity. Even in the case of programming tools/languages. If you want to send email in your script, do you write a whole bunch of vbscript code or do you prefer to use things like blat ? That is just an example but if you have ever been a system administrator (or equivalent) that need to perform complex tasks like FTP, telnet to remote and execute jobs etc you will know that you definitely need better tools or Libraries to do your work than using merely cmd.exe

Quote
your proposed problems are merely invented scenarios devised for the sole purpose of being difficult- if not impossible- via CMD line batch processing commands; except perhaps the first one- since cscript.exe can be used to perform easy string manip via Jscript or VBScript code from batch; and in fact that goes for most of the other proposed "problems".
please read my post again. I said "...doing complex string manipulations EASILY...".  (sidenote: do you want to have a shot at solving those problems i mentioned using just vbscript.? )

Quote
The funniest part is me and Dias are discussing various ways of performing this task with various levels of compatibility for different newline methods with tools provided in default OS installs and you jump in with a suggestion to install some third party tool and provide some code for it that accomplishes nothing more then that which Dias had already provided, except perhaps clutter up their PC with more software installations.
It's funny because I was replying to the OP and giving an alternative suggestion (and its cross platform). Why should i be bothered about your discussion with Dias since OP didn't even mentioned he can't install 3rd party tools, or whatever you call it.
and wait, so you mean to say i cannot post my suggestions in this forum? I mean, my posts are  not meant for you , you know? If my suggestion does not suit the OP, its fine. He has the final say and I am a willing contributor. Its NOT up to you to say anything. Cluttering up the PC? what are you talking about? you mean to say, things like automatic updates which MS downloads from the internet is not cluttering up the PC?? so you mean to say useful tools from the resource kits cannot be used etc?
Title: Re: How many lines
Post by: ghostdog74 on March 14, 2009, 02:00:06 PM
, "if it can be done in something that is included in the default install, whats the point of using a separate tool, third party or otherwise?".
if i were to say "its for improving workplace productivity", would you accept that as a reasonable answer.