1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Batch File for Counting Lines in Text or CSV files

Discussion in 'Software Development' started by allenkc, Jan 13, 2011.

Thread Status:
Not open for further replies.
  1. allenkc

    allenkc Thread Starter

    Feb 6, 2003
    Hello All,

    I am looking for something to help me in my work. I have to keep line counts in text and CSV files in a log. I was wondering if it was possible to do this with a batch file?

    Here is what i would like for it to do:
    Open the DOS window in XP.
    Drag the Bat file to the dos window
    Drag the file needing to be counted to the same window(or copy the network address), click enter
    and have a line count

    Currently I use KEdit to get this information from the status bar (size) but thought a batch file might be easier (and quicker).

    Can someone help me?

    Tannks a bunch
  2. IsolatedSnail


    Nov 7, 2009
    Did some Googling around, never really thought of doing this in a batch file. I found a VBscript alternative you might be able to use from command line though.


    here's a vbscript alternative:
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objArgs = WScript.Arguments
    myFile = objArgs(0)
    Set objFile = objFSO.OpenTextFile(myFile,1)
    Do Until objFile.AtEndOfLine
    line = objFile.Line
    WScript.Echo "Line count of", myFile , "is", line
    cscript myscript myfile

    The link above is the link to the page where I found this information, wanted to give credit where credit is due. With this script you could just call the script and give it the parameter my file from command line. If you really wanted a batch, you could easily create a batch that echo's each of those lines into a temp.vbs, calls the temp.vbs with a parameter %myfile using the "cscript myscript myfile" command then "del temp.vbs" at the end. Seems very redundant and pointless though.

    Hope the VBS helps you.

  3. TheOutcaste


    Aug 7, 2007
    The vbscript might be a bit faster on very large files, but you can use the Find command in a batch file:
    Save this as counter.cmd someplace.
    Create a shortcut to it on the desktop (Rightclick the file, click Send to | Desktop)
    Drop any file on the shortcut and a Command Prompt window will opoen and display the line count.
    @Echo Off
    Find /C /V "[email protected]#anyfile" %1
    Use this version to drop multiple files:
    @Echo Off
    If "%~1"=="" Pause&Goto EOF
    Find /C /V "[email protected]#anyfile" %1
    Goto _Loop
    You'll get a display like this:
    ---------- C:\TEMP DIR\TEST\ASCFUNC.CMD: 77
    ---------- C:\TEMP DIR\TEST\AUTOLOG.CMD: 23
    ---------- C:\TEMP DIR\TEST\BIGSUB.CMD: 27
    ---------- C:\TEMP DIR\TEST\CHKENC.CMD: 17
    ---------- C:\TEMP DIR\TEST\CHKENC1.CMD: 19
    Press any key to continue . . .
    You can replace the Find command with this to use the vbscript file:
    cscript /nologo myscript.vbs %1
    just put the myscript.vbs file in the same folder as counter.cmd.

    You can copy and paste the result, or you could have it output directly to the log file.
    You could create a file that will automatically do every file in a specified folder when run, or a series of folders, or folders you drop on the shortcut.
  4. allenkc

    allenkc Thread Starter

    Feb 6, 2003
    Thank you guys so much. Both versions worked perfectly. I even did a little test to see if the VBS was indeed a little faster and to my suprise it was.
    I counted a file that had 3,313,097 lines.
    For the BAT file it took 4 minutes and 22 seconds
    For the VBS it took 2 minutes and 54 seconds

    Thanks again, this forum is awesome!!
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/974448

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice