Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How many lines  (Read 9494 times)

0 Members and 2 Guests are viewing this topic.

devil0150

    Topic Starter


    Beginner

    Thanked: 1
    How many lines
    « 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.

    Dias de verano

    • Guest
    Re: How many lines
    « Reply #1 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%

    Reno



      Hopeful
    • Thanked: 32
      Re: How many lines
      « Reply #2 on: March 10, 2009, 02:28:09 AM »
      dias, how about blank lines?

      Dias de verano

      • Guest
      Re: How many lines
      « Reply #3 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?






      Reno



        Hopeful
      • Thanked: 32
        Re: How many lines
        « Reply #4 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.

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: How many lines
        « Reply #5 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.
        I was trying to dereference Null Pointers before it was cool.

        Dias de verano

        • Guest
        Re: How many lines
        « Reply #6 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

        « Last Edit: March 10, 2009, 04:54:04 AM by Dias de verano »

        Dias de verano

        • Guest
        Re: How many lines
        « Reply #7 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)



        Of course, a "blank" line could have tabs, spaces, etc. (these are still ignored if the regex is ".")
        « Last Edit: March 10, 2009, 04:42:26 AM by Dias de verano »

        Reno



          Hopeful
        • Thanked: 32
          Re: How many lines
          « Reply #8 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.  :)

          devil0150

            Topic Starter


            Beginner

            Thanked: 1
            Re: How many lines
            « Reply #9 on: March 10, 2009, 02:15:20 PM »
            Thanks everybody.
            Very helpful.

            BC_Programmer


              Mastermind
            • Typing is no substitute for thinking.
            • Thanked: 1140
              • Yes
              • Yes
              • BC-Programming.com
            • Certifications: List
            • Computer: Specs
            • Experience: Beginner
            • OS: Windows 11
            Re: How many lines
            « Reply #10 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.
            I was trying to dereference Null Pointers before it was cool.

            Dias de verano

            • Guest
            Re: How many lines
            « Reply #11 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.


            BC_Programmer


              Mastermind
            • Typing is no substitute for thinking.
            • Thanked: 1140
              • Yes
              • Yes
              • BC-Programming.com
            • Certifications: List
            • Computer: Specs
            • Experience: Beginner
            • OS: Windows 11
            Re: How many lines
            « Reply #12 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...
            I was trying to dereference Null Pointers before it was cool.

            Dias de verano

            • Guest
            Re: How many lines
            « Reply #13 on: March 11, 2009, 01:44:06 AM »
            Ah now I see your cross-platform strategy.

            Dias de verano

            • Guest
            Re: How many lines
            « Reply #14 on: March 11, 2009, 02:10:23 AM »