Counting carriage returns in a text file

I am importing text files into a database.

These text files have a row delimiter  of CR (or possible CR/LF)

What I want to do is import the text files into a table, count the number of rows imported, then I want to run a DOS command to count the number of CR's in the text file.... in theory they should match.

I can't work out how to use FIND or FINDSTR to search for a CR and/or CRLF in a file. If I could then I could use these to return the count of CR's

I've tried \n and that doesn't work


Any ideas?

LVL 30
nmcdermaidAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SteveGTRCommented:
You could try something like this:

@echo off

set /a lineCnt=0

if "%~1"=="" echo Please enter file name&goto :EOF
if not exist "%~1" echo %~1 does not exist&goto :EOF

for /f "delims=" %%a in ('type "%~1" 2^>NUL ^| findstr /V /N __DONOTFINDTHIS__') do set /a lineCnt+=1

echo Line count: %lineCnt%

Good Luck,
Steve
0
nmcdermaidAuthor Commented:
hmm I just treid it and it just echoed 'please enter file name' and then finished.


How could I alter it to accept a path/filename and then jsut spit out a single number, that being the count of carriage returns?
0
Steve KnightIT ConsultancyCommented:
how about using just find?

Find /v /c "gdfhfdhfdx" <file.txt

/c counts lines
/v includes lines that don't have the string gdf etc. in

Works for me

Steve
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

SteveGTRCommented:
You have to enter a file name on the command line.
0
Dushan De SilvaTechnology ArchitectCommented:
Please Save SteveGTR 's file at a bat file. Then when you running that bat file fomr the dos command line, please give the input file name as a parameter for that bat file.

abc.bat youinput.txt

And make sure you have put bat file and input data files in same folders
or else you can give path with that perticular file.

BR Dushan
0
nmcdermaidAuthor Commented:
Oops didn't realise I needed to put it on the command line.

I would like to use 'FIND' but I cant work out how to tell it to search for a carriage return. Any idea how to do that?
0
SteveGTRCommented:
I don't know of a way to tell FIND to look for CR. How about the batch file I provided. How well did it do?
0
Steve KnightIT ConsultancyCommented:
like I said above, find /v searches for all lines that don't include a string so searching for a string that cannot exist will count all lines in the file which is surely the same as the number of crlf?  Steve

I imagine you will find stevegtr's batch file will do the job too somehow, they normally do :-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nmcdermaidAuthor Commented:
Ah I see!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.