Counting Lines in a Text File MS DOS Batch

Microsoft Upg-vc msdos 6.22
February 19, 2010 at 12:15:59
Specs: Windows XP
I am having some trouble counting all of the
records in a text file. I used some other forum
threads to create the code below and it was
working perfectly for smaller files but then I ran
a file that has 36,513 records and for some
reason the below code will count 36,520
records. Ive tried other solutions I found but
nothing has worked.

Is there anything in the file that could counted
twice? Is there a more efficient way to get the
record count in a text file?


setlocal ENABLEDELAYEDEXPANSION

set /a count1=0

for /f %%A in ('type
"C:\Data\*CEI90D01*.txt"^|find /c /v "-------------"
') do set count1=%%A

echo There are !count1! records in the Data
File


See More: Counting Lines in a Text File MS DOS Batch

Report •

#1
February 19, 2010 at 13:20:02
I don't know for sure. You might have trailing null lines in one of the files (btw: about how many files are being scanned?)
Aside from that, the only thing i can suggest is to look at a detail output from "find" and see where the line no.s are going off from what they should be. I know this sounds burdensome with 36000 records, not to mention x no. of files, but it might not take as long as it sounds. send the output to file and use EDIT to count the lines:
type "C:\Data\*CEI90D01*.txt" | find /v /n "----------">test.txt
EDIT test.txt
"edit" shows you the line number that the cursor is on (lower right corner screen), and find /n puts line no.s at beginning of each line, so you can compare them by paging down thru the test.txt and seeing where they get out of sync, (or skip to the end (ctrl-END) and page up).
Since you're scanning multiple files ("type" won't even take wildcards on my pre-XP systems), that kind of muddies things but hopefully it can be sorted out.

Report •

#2
February 19, 2010 at 14:08:53
I am only scanning 1 file and Notepad++ doesn't showing any
trailing null lines and I have tried to delete anything at the end.

I will try creating this output file and let you know what I come up
with.

Thanks for the help,


Report •

#3
February 19, 2010 at 16:59:57
Can you count in prompt over 32767? Max 16 bit integer,

Report •

Related Solutions

#4
February 20, 2010 at 02:17:10
[1] This is not DOS

[2] If we assume the file has no special chars, the likely snag is blank lines.

In XP, try this:

===================================
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=1 delims=[]" %%a in ('find /v /c "" ^< myfile') do (
echo %%a
)


=====================================
Helping others achieve escape felicity

M2


Report •

#5
February 20, 2010 at 05:48:23
@echo off

for /F "tokens=3" %%f in ('find /V /C "-------------" test.txt') do (
echo %%f
)

If the file you look for, is not just one file (like test.txt) but a series of files, you need to loop around, because you need a way to tell that this or that file contains this or that amount of lines ...


Report •

#6
February 22, 2010 at 08:48:55
I have tested everyones solution and the count still comes as
36,520 but I have discovered the records that are causing the
count to increase thanks to nbrane's edit suggestion.

The file I am reading was created by a mainframe and some of
the records have a 'null' in some locations. When I printed the
the records/line number these records were being split into
two lines and increasing the total record count.

Is there a way to remove the nulls before I count the lines?
The nulls will never appear in the first 10 characters of each
record, is there a way to count just the first value or each line
or something?


Report •

#7
February 22, 2010 at 09:07:17
Are the nulls FF hex [255 decimal] ?


=====================================
Helping others achieve escape felicity

M2


Report •

#8
February 22, 2010 at 09:33:23
this might work (i assumed nulls were zeros):

for /f "tokens=1 delims=:" %%a in ('findstr /n . infile') do set cc=%%a
echo %cc%


Report •

#9
February 22, 2010 at 12:06:26
nbrane - I let that code run for 30 min and it never returned.

I am working with the team to have them remove any nulls before
they send it to me so my original solution will work.

Thanks all the for the help.


Report •

Ask Question