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



## allenkc (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


----------



## 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.

http://www.computing.net/answers/programming/batch-getting-the-number-of-lines/15728.html

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
objFile.ReadLine
Loop
WScript.Echo "Line count of", myFile , "is", line
```
usage:
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.

-Kyle


----------



## TheOutcaste (Aug 8, 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
pause
```
Use this version to drop multiple files:

```
@Echo Off
:_Loop
If "%~1"=="" Pause&Goto EOF
Find /C /V "[email protected]#anyfile" %1
Shift
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

---------- C:\TEMP DIR\TEST\COPYKEEPDUPLICATES.CMD: 33
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.


----------



## allenkc (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!!


----------

