Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 710
  • Last Modified:

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?

0
nmcdermaid
Asked:
nmcdermaid
  • 3
  • 3
  • 2
  • +1
2 Solutions
 
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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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
 
nmcdermaidAuthor Commented:
Ah I see!
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now