3

I want to count the number of lines in a file using batch.

I have gone through this, but couldn't understand as I am a beginner. I have written my own piece of code with basic knowledge.

@echo off
set "file=abc.csv"
set /a x=0
for /f "usebackq delims=" %%p in ("%file%") do (
  echo %x%
  pause>nul
  set /a x=%x%+1
)

When I run the above code I am getting 0 as output. Can someone please help me in sorting out the error?

| |
  • 2
    I know you said "Batch" but it always hurts me to see way too difficult and unreadeable code for such an easy task. So here's a PowerShell Solution: $a = (get-content "C:\abc.csv" | measure-object).count the $a variable now contains the number of lines in your file. – SimonS Apr 28 '16 at 12:35
  • 2
    @SimonS Command line is also a one liner ;) See my answer. I find batch files easier to read than PowerShell ;) – DavidPostill Apr 28 '16 at 13:06
  • 2
    @DavidPostill but you're (don't get me wrong here) an old-school master of disaster ;-). if you're new to windows scripting, powershell is definetly easier to read. no /v or /c switch where you need to read a help file to get it's meaning. additionally PowerShell is the new cmd (it's successor) and I would encourage any admin/scripter to use PowerShell instead of cmd. – SimonS Apr 28 '16 at 13:21
2

I want to count the number of lines in a file using batch

Specific solution

From a command line:

F:\test>for /f "usebackq" %b in (`type abc.csv ^| find "" /v /c`) do @echo line count is %b
line count is 1

From a batch file (countlines.cmd):

@echo off
Setlocal EnableDelayedExpansion
  for /f "usebackq" %%b in (`type abc.csv ^| find "" /v /c`) do (
    echo line count is %%b
    )
  )

Example:

F:\test>countlines
line count is 1
F:\test>

Flexible solution

Use the following batch file (countlines.cmd):

@echo off
Setlocal EnableDelayedExpansion
for /f "usebackq" %%a in (`dir /b /s %1`)  do (
  echo processing file %%a
  for /f "usebackq" %%b in (`type %%a ^| find "" /v /c`) do (
    echo line count is %%b
    set /a lines += %%b
    )
  )
echo total lines is %lines%

Notes:

  • Total number of lines is stored in %lines%.
  • Batch file supports wildcards.
  • Tweak echo ... commands as appropriate.

Usage:

countlines filename_expression

Example:

F:\test>countlines *.csv
processing file F:\test\abc.csv
line count is 1
processing file F:\test\def.csv
line count is 1
total lines is 2

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • dir - Display a list of files and subfolders.
  • find - Search for a text string in a file & display all the lines where it is found.
  • for /f - Loop command against the results of another command.
| |
1

A simple way to count the number of lines in a file on a Microsoft Windows system is by using the following command:

find /v /c "" somefile.txt

The /c option counts the number of lines while the /v option displays all lines NOT containing the specified string. Since the null string, i.e. "", is treated as never matching, you should see the number of lines in the file displayed - see the Stupid command-line trick: Counting the number of lines in stdin article at Raymond Chen's Microsoft Developer Blog, The Old New Thing for an explanation of why this works and how a bug in the earliest MS-DOS version of the find command became a feature that remains to this day. The MS-DOS operating system was an operating system for early PCs provided by Microsoft long before the company created Microsoft Windows.

| |
  • This is also working perfect @moonpoint . Thanks for the detailed explanation. – learner1 Apr 29 '16 at 10:52
0

You're getting count of zero because delayed expansion is turned off.
You've already set /a x=0

The value of x inside the for loop doesn't change with %x%. Replace it with !x!

Try this:

Setlocal EnableDelayedExpansion
@echo off
set "file=abc.csv"
set /a x=0
for /f "usebackq delims=" %%p in ("%file%") do (
  echo !x!
  pause>nul
  set /a x=!x!+1
)

A simpler way to count the number of lines in a file using batch:

find /v "" /c < abc.csv

| |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.