Thursday, September 13, 2012

Get File Line Count on Windows

This is just a small trick I have learned. Usually when you have to work with really really huge files you need to know the number of lines if has. For example, if you have to import a huge CSV file into a DB you need the count so you can verify the process or to have an idea of how long it will take. On Linux this is easy just a
wc -l [filename]
and that is all. On windows though it is different unless you install cygwin your command line options are limited. Specially if you have to do the job on customer's servers where you can't install what ever you want. So to do the trick on windows you have 2 options:
findstr /R /N "^" [filename] | find /C ":"
type [filename] | find /c /v "~~~"
The speed depends on how big the file is. I tried it on an 10GB CSV file and took around 15 minutes, both gave me the right result though. Hope it helps some one. Happy coding!