Counting number of lines, split file

July 27, 2009 at 09:20:18
Specs: Windows XP
I am VERY VERY new to batch programming/ batch files.

I have a problem here where I need to count the number of lines/records in a text file. If the number is say greater than 900, I need to split the file or cut all the lines from 891 to the end and place in a new smaller file.

This is what I have:

@echo off

set /a count=0
for /f "delims=" %%i in (c:\FILE.txt) do (
set /a count=!count!+1
echo There are !count! records in FILE.txt

if "!count!" GTR "900" GOTO splitfiles else GOTO end


for/f "delims= skip=890" %%m in (c:\FILE.txt) do (TYPE %%m>>c:\POST.txt

echo There are less than 900 records in this file.

See More: Counting number of lines, split file

Report •

July 27, 2009 at 18:55:57
I can see a couple of small problems:


"for/f "delims= skip=890" %%m in (c:\FILE.txt) do (TYPE %%m>>c:\POST.txt"

Type is used to display whole files not lines of text. Echo is sufficient.

 for/f "skip=890 delims=" %%m in (c:\FILE.txt) do (
    >>c:\POST.txt echo %%m


"delims= skip=890"

"delims=" should always be the last option(presumably so it can detect spaces)

"skip=890 delims="


This problem may or may not be an issue depending on the input file.

For /f %%v in...... do ....

For /f only picks up lines that are not blank, take this as an example:
This is line 1

this is line 3

this is line 4
this is line 5


set count=
for /f "delims=" %%i in (c:\FILE.txt) do set /a count+=1
echo %count%

Returns 4, not 6. This would be fine except "skip" includes blank lines so:

for /f "skip=3 delims=" %%i in (FILE.txt) do echo %%i


this is line 4
this is line 5

If you want an accurate line count you can use find:

for /f "delims=" %%i in ('type c:\FILE.txt^|find /c /v "" ') do set count=%%i
echo %count%

Returns 6

Report •

July 27, 2009 at 19:33:02
Thanks very much Judago...that helped a whole whole lot!

one more thing tho...whilst the records that should be in the new file that is created are there...they are also still in the old there anyway I could fix that?

Report •

July 27, 2009 at 20:15:49
Are blank lines an issue(I ask because there are a whole lot less hoops to jump through if they are not important)?

If they are not then the whole process could be intergrated.

This script is untested, the move command has an echo prefixing it so you can check the output before overwriting it(if c:\post.txt has anything important in it then you may want to change the name in the script to check the output). This is the "easy" process that ignores blank lines.

set count=0
for /f "delims=" %%a in (c:\file.txt) do (
    if !count! lss 890 (
        set /a count+=1
        >> $tmpfile.txt.txt echo %%a
    ) else (
        >>c:\post.txt echo %%a
echo move "%cd%\$tmpfile.txt.txt" "c:\file.txt"

Their is however one thing to note, the delayed expansion has a caveat, and !'s will dissapear, if you record contains them there is again more hoops to jump through.

Report •

Related Solutions

July 28, 2009 at 01:45:00
If you have a C compiler available, it would be much more
efficient to write this sort of thing in C.

Report •

July 28, 2009 at 06:31:19
Thanks Very much...and no Blank lines are not an issue..
The untested code worked...I just tweaked it a bit and now its all good!

@klint--- I started it in C...not so confident in my C I thought the Batch would be the next best thing. I will look into it though...Thanks.

Report •

Ask Question