none
Count number of lines in a text file from a windows batch file

Answers

  • Try this to count the number of lines in a text file  and display it the way you want ...

      set /p =First Query Text: <nul
      Find /V /C "" < c:\Scripts\Example.txt

    Or you can add a string to match and report its count ...

      set /p =Count of matching text: <nul
      Find /C "Match string" < c:\Scripts\Example.txt

    The SET /P line acts like an ECHO, but without the newline at the end, so that subsequent text is appended to that line.

    Another approach, which may be more versatile, is to place the result into an environment variable, so that you can use it later in an ECHO or other statement.  The FOR statement with it's /F switch is used for that ...

      for /f %%C in ('Find /V /C "" ^< c:\Scripts\Example.txt') do set Count=%%C
      echo The file has %Count% lines.

    Note the carat (^) escape character for the otherwise problematic less than sign (<) within the FORs SET part (the stuff between the parens).  Type FOR/? at a command prompt for more information on this syntax.


    Tom Lavedas

    This completes my question. Thanks a lot!
    • Marked as answer by mpMS Wednesday, July 27, 2011 12:50 AM
    Wednesday, July 27, 2011 12:50 AM
  • Try this to count the number of lines in a text file  and display it the way you want ...

      set /p =First Query Text: <nul
      Find /V /C "" < c:\Scripts\Example.txt

    Or you can add a string to match and report its count ...

      set /p =Count of matching text: <nul
      Find /C "Match string" < c:\Scripts\Example.txt

    The SET /P line acts like an ECHO, but without the newline at the end, so that subsequent text is appended to that line.

    Another approach, which may be more versatile, is to place the result into an environment variable, so that you can use it later in an ECHO or other statement.  The FOR statement with it's /F switch is used for that ...

      for /f %%C in ('Find /V /C "" ^< c:\Scripts\Example.txt') do set Count=%%C
      echo The file has %Count% lines.

    Note the carat (^) escape character for the otherwise problematic less than sign (<) within the FORs SET part (the stuff between the parens).  Type FOR/? at a command prompt for more information on this syntax.


    Tom Lavedas
    Tuesday, July 26, 2011 1:16 PM
    Moderator

All replies

  • You can use the Type and Find commands to count the number of lines in the file that do not contain a string. For example:

    Type c:\Scripts\Example.txt | Find /V /C "ZZZXXXYYY"

    If the file does not contain the string, the result will be the number of lines in the file. You can put this in a batch file, and pass the file name as a parameter.

     


    Richard Mueller - MVP Directory Services
    Tuesday, July 26, 2011 3:10 AM
    Moderator
  • this answers my question, thanks a lot. One more... If I want to output this result together with a text. Example:

    echo Result of the Query >> Result.txt 

     Type c:\Scripts\Example.txt | Find /V /C "ZZZXXXYYY" >> Result.txt

     Type c:\Scripts\Example.txt | Find /V /C "ZZZ" >> Result.txt

     

    output on Result.txt will be:

    Result of the Query

    100

    200

     

    I wanted to do it like this:

    output on Result.txt will be:

    Result of the Query

    First Query Text: 100

    Second Query Text:200

     

     

    Tuesday, July 26, 2011 3:52 AM
  • Perhaps if you describe your overall goal?

    Bill

    Tuesday, July 26, 2011 4:24 AM
    Moderator
  • Here is a little simple Powershell script that will count the lines, regardless of content.  As Bill said, it would be easier if we understood what you are attempting to achieve:

    $linecount = 0
    gc test.txt |% {$linecount ++}
    write-host "First Query Text: " $linecount
    


    If you found this post helpful, please give it a "Helpful" vote. If it answered your question, remember to mark it as an "Answer".
    Tuesday, July 26, 2011 8:30 AM
  • Teddy

    Cmon it'sPowerSHell. Nothing takes that much code in PoSH.

    (gc test.txt).count

     


    jv
    Tuesday, July 26, 2011 10:19 AM
  • Like you said, wrong again.  I love being wrong!


    Edit: jv, How did you know to use .count?  If I pipe gc test.txt | get-member, I see no count method. 

     

    Tuesday, July 26, 2011 10:22 AM
  • Try it like this:

    Find /V /C "ZZZXXXYYY"   c:\Scripts\Example.txt 
    Find /V /C "ZZZXXXYYY"   c:\Scripts\Example.txt  >> Result.txt
    Find /V /C "ZZZXXXYYY"   c:\Scripts\*.txt 


    jv
    Tuesday, July 26, 2011 10:25 AM
  • Try this to count the number of lines in a text file  and display it the way you want ...

      set /p =First Query Text: <nul
      Find /V /C "" < c:\Scripts\Example.txt

    Or you can add a string to match and report its count ...

      set /p =Count of matching text: <nul
      Find /C "Match string" < c:\Scripts\Example.txt

    The SET /P line acts like an ECHO, but without the newline at the end, so that subsequent text is appended to that line.

    Another approach, which may be more versatile, is to place the result into an environment variable, so that you can use it later in an ECHO or other statement.  The FOR statement with it's /F switch is used for that ...

      for /f %%C in ('Find /V /C "" ^< c:\Scripts\Example.txt') do set Count=%%C
      echo The file has %Count% lines.

    Note the carat (^) escape character for the otherwise problematic less than sign (<) within the FORs SET part (the stuff between the parens).  Type FOR/? at a command prompt for more information on this syntax.


    Tom Lavedas
    Tuesday, July 26, 2011 1:16 PM
    Moderator
  •  I love being wrong!



    Awesome!

    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks
    Tuesday, July 26, 2011 1:24 PM
  • Hey - I was just teasing him.  I didn't say he was wrong.  I was agreeng that PowerShell can do this much more easily.

    What? Doesn't anyone have a sense of humour around here?  I know BigTeddy does.

     

     

     

     


    jv
    Tuesday, July 26, 2011 3:25 PM
  • Hey - I was just teasing him.  I didn't say he was wrong.  I was agreeng that PowerShell can do this much more easily.

    What? Doesn't anyone have a sense of humour around here?  I know BigTeddy does.

     


    Humour is subjective. 
    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, July 26, 2011 3:37 PM
    Moderator
  • Edit: jv, How did you know to use .count?  If I pipe gc test.txt | get-member, I see no count method. 

     


    That's a common mistake.  Remember that the Powershell pipeline will "unroll" an array into individual elements.  When you did this:

    gc test.txt | gm 

    you piped an array to get-member.  The pipeline peeled the first element of the array off and did a get-member on it, and gave you the result, and you got all the properties and methods of a string object.

    To see the members of the array itself, you need to do a get-member on it directly, not through the pipeline:

    gm -inputobject (gc test.txt)

    then you'll get the properties methods of the array.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Proposed as answer by Bigteddy Tuesday, July 26, 2011 4:19 PM
    • Unproposed as answer by Bigteddy Tuesday, July 26, 2011 4:20 PM
    Tuesday, July 26, 2011 4:05 PM
    Moderator
  • Now mj you know I am not going to let you get away with that and her is why:::

    Look again at teh original statemnent:

    Edit: jv, How did you know to use .count?  If I pipe gc test.txt | get-member, I see no count method. 

    How?  Well I dereferenced the results with teh dot operator.  Yes, in POwerShell, the dot is an operator.  It is also a very much overloaded operator.

    #This gets a file as a collection of lines into  a variable
    $lines=cat file.txt
    $lines|gm
    $lines.count

    # This get a file as a collection of lines with no variable that is visible.  This is called an 'anonymous' variable.
    (cat file.txt).Count

    The variable is still created but it is not dsiplayed and it will not be sent to the console because I have asked to to be acted on by teh dot operato to give me only the count of items.  It is a very good short hand and makes code more compact, readable and usable.  We do it in C all of the time.  It is why I have never beencomfortable with VB languages because, until VB.NET, they didn't lend to doing this.  VB code rends to get big and clunky.

    So mj. while you were correct about gm you missed the underlying issue which was "what the h**l is that?"

    Yes you cannot inspect aa file name.and find a count property.  Even if you do this you will not find count. 

    (gc test.txt)|gm

    Why? Because the count property is an alias for array lenght anda few other object collections.  As MJ points up.  Jsut doing GM on the file result gers only the first element of [string[]] (string array)

    Now try these two:

    ("hello").length
    ("hello","world").length

    ("hello").count
    ("hello","world").count

     


    jv
    Tuesday, July 26, 2011 5:18 PM
  • I answered the question that was asked, after it appeared that it was going to go unanswered. 

     

     


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, July 26, 2011 6:16 PM
    Moderator
  • Hi mjolinor,

    It's probably best not to feed the trolls :)

    Bill

    Tuesday, July 26, 2011 6:30 PM
    Moderator
  • We should probably take this elsewhere.  Moderator's Forum?


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, July 26, 2011 7:12 PM
    Moderator
  • Guys!  Please!  Can I just remind you of the opening question:

    "How can I count the number of lines using a batch file?"... and it's got to this!  No, no,

    mjolinor, your explanation was clear and understandable, and I thank you for it.  I also thought jv wasn't going to answer me, I thought he'd missed that.

    His explanation gave me further insight into the workings of the dot operator, and I thank him for that too.

    You both have very different styles of answering questions, and I have got used to jv, he doesn't pull punches, but he is very helpful too (when he wants to be).  I am not offended in any way, and I don't think you should be either.  Please lighten up, guys.  There's nothing wrong with healthy positive debate. (and a sprinkling of levity)


    If you found this post helpful, please give it a "Helpful" vote. If it answered your question, remember to mark it as an "Answer".

    Tuesday, July 26, 2011 9:30 PM
  • IMHO:

     the forums are an exercise in peer review and cooperative programming and debugging.  This is exactly the enviroment that Jerry Weinberg's idea of "Egoless Programming" was intended to be applied to. 

    That idea has proven itself over and over again in the last 40 years, and to the degree that it is ignored, the forums will eventually suffer.

    You address the code, you don't go after the coder.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, July 26, 2011 11:00 PM
    Moderator
  • We got on fine until one day you lit into me about "how much I don't know".  I have never directed a personal attack or remark against you. 

    If I have had any disagreement with you "since the day you got here". it's been strictly about the code. 

    Review the threads.  Nobody here goes after people the way you do.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, July 26, 2011 11:23 PM
    Moderator
  • Try this to count the number of lines in a text file  and display it the way you want ...

      set /p =First Query Text: <nul
      Find /V /C "" < c:\Scripts\Example.txt

    Or you can add a string to match and report its count ...

      set /p =Count of matching text: <nul
      Find /C "Match string" < c:\Scripts\Example.txt

    The SET /P line acts like an ECHO, but without the newline at the end, so that subsequent text is appended to that line.

    Another approach, which may be more versatile, is to place the result into an environment variable, so that you can use it later in an ECHO or other statement.  The FOR statement with it's /F switch is used for that ...

      for /f %%C in ('Find /V /C "" ^< c:\Scripts\Example.txt') do set Count=%%C
      echo The file has %Count% lines.

    Note the carat (^) escape character for the otherwise problematic less than sign (<) within the FORs SET part (the stuff between the parens).  Type FOR/? at a command prompt for more information on this syntax.


    Tom Lavedas

    This completes my question. Thanks a lot!
    • Marked as answer by mpMS Wednesday, July 27, 2011 12:50 AM
    Wednesday, July 27, 2011 12:50 AM
  • Hey - I was just teasing him.  I didn't say he was wrong.  I was agreeng that PowerShell can do this much more easily.
    What? Doesn't anyone have a sense of humour around here?  I know BigTeddy does.

    Was this because I posted 'awesome' when BigTeddy said he loved being wrong? If it was then don't stress the flex man... I love being wrong as well. It's the only way to learn anything around here (here being in the real world pointing about at real people sitting about me. It's awesome someone else thinks the same way. Anyway I've taken this off topic... easy.

    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks
    Wednesday, July 27, 2011 7:38 AM
  • Hi Derek,

    This thread has been a thrilling ride from "How to count lines in a text file" to what really set things alight, which was jv's statement:

    "Now mj you know I am not going to let you get away with that and her is why".

    I think the teasing of me was in some way a catalyst for a smouldering thing which has nothing to do with me or coding or positive healthy debate.  It actually got very nasty (calling jv a fat troll), and I think that's childish.

    I think we should take the idea of "Egoless Programming" one step futher, to "Egoless Forum Participation".


    If you found this post helpful, please give it a "Helpful" vote. If it answered your question, remember to mark it as an "Answer".
    Wednesday, July 27, 2011 8:03 AM
  • That was indeed a mistake (the first rule of egoless programming is "you will make mistakes)". 

    I will delete that post. 

     


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Wednesday, July 27, 2011 10:50 AM
    Moderator
  • We should probably take this elsewhere.  Moderator's Forum?

    Hi mjolinor,

    Yes. Please see my post there.

    Bill

    Wednesday, July 27, 2011 2:41 PM
    Moderator
  • It actually got very nasty (calling jv a fat troll), and I think that's childish.

    Hi Bigteddy,

    I intended a very specific definition of "troll": http://en.wikipedia.org/wiki/Troll_%28Internet%29

    It is my fault for taking the thread off-topic. Please accept my apologies.

    Bill


    Wednesday, July 27, 2011 2:51 PM
    Moderator
  • No need to apologise.  JV must look after himself.  I'm cool, and I don't have any quarrels with anyone here.  JV, if you're reading this, you are a bit heavy and rude sometimes.  Take that from whence it comes: a place of positve criticism of your posting style.


    If you found this post helpful, please give it a "Helpful" vote. If it answered your question, remember to mark it as an "Answer".
    Wednesday, July 27, 2011 4:01 PM