We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Counting carriage returns in a text file

nmcdermaid
nmcdermaid asked
on
Medium Priority
737 Views
Last Modified: 2008-02-01
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?

Comment
Watch Question

Top Expert 2007
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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?
Steve KnightIT Consultancy
CERTIFIED EXPERT

Commented:
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
Top Expert 2007

Commented:
You have to enter a file name on the command line.
Dushan De SilvaTechnology Architect

Commented:
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

Author

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?
Top Expert 2007

Commented:
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?
IT Consultancy
CERTIFIED EXPERT
Commented:
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 :-)

Author

Commented:
Ah I see!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.