1.

Solve : Count Rows in a Text File?

Answer»

Hi Chaps,

Has anyone ever done this before?

I have a text file and want to count the number of rows it has. I know I can do this by importing into a database but this isn't feasible in the context of what I am doing.

I have some code (help from here already!!!!!!!) that provides me with a list of files in a directory eg

Filename1
Filename2
Filename3

This works great. However I would like to get the batch file to also count the number of rows in the text file and put this in eg

Filename1 25
Filename2   2
Filename3 178

Is this possible. I thought of perhaps counting the number of times the line feed appears in the file but am not really sure where to start.

I would really appreciate any help.

CheersWell, any programming language supports that. But I am just curious: DIR already reports file size. Why would you focus on number of lines rather than file size?

Mac
What OS are you running?  If Windows 2000 / XP / 2003 / Vista, you can USE:
Code: [Select]echo off
setlocal
if not {%1}=={} goto :Continue
echo No parameter specified. Syntax: %0 filename
goto :EOF
:Continue
for /f "tokens=1 delims=:" %%a in ('findstr /n . %1') do SET lines=%%a
echo The file %1 has %lines% lines.billsack,

Based on your other post I think this is what you want:

[edit](for /f "delims=" %%a in ('DIR *.XXX /b /s') do (
    for /f %%b in ('type "%file%"|find "" /v /c') do echo.%%a %%b
))>MyDIR.txt[/edit]

right or not? Hey guys,

Thanks for this. Cant get the code to work yet though.

Mac - the reason i am doing this is to get a ROW count for an import log. I don't need to know the filesize, just the number of records it contains!

The code to create the txt file with the file list is:

dir G:\share\CRS\FTPDownloads/b s>G:\share\CRS\FTPDownloads\CDSDataImport.txt

This works great.

I have tried:

(for /f "delims=" %%a in (G:\share\CRS\FTPDownloads*.txt* /b /s') do ( for /f %%b in ('type "%file%"|find "" /v /c') do echo.%%a %%b
))>G:\share\CRS\FTPDownloads\CDSDataImport2.txt

But I cant get it to work.

Cheers for all your help gents!Try this:
Code: [Select]echo off
setlocal enabledelayedexpansion
for /f "tokens=1 delims=:" %%a in ('findstr /n . G:\share\CRS\FTPDownloads\CDSDataImport.txt') do set lines=%%a
echo The file has !lines! lines.Should have tried it, sorry  :

[edit](for /f "delims=" %%a in ('DIR G:\share\CRS\FTPDownloads\*.txt* /b /s') do (
    for /f %%b in ('"type "%%a"|find "" /v /c"') do echo.%%a %%b
))>G:\share\CRS\FTPDownloads\CDSDataImport2.txt[/edit]

Now, how is that?



Discussion

No Comment Found