Windows count lines in all files

Windows count lines in all files

This forum is closed. Thank you for your contributions.

Answered by:

Question

I am new to batch scripting.

I have to count the no of lines for all the files that is present in particular windows folder and save the result in a file.And it should delete the result.txt first while running it second time.

Example : Suppose a folder name is ABC and in the files a.dat,b.dat,c.dat files are present.

Result.txt will contain the below.

I tried using the below commands to count a single file count. But i dont know how to do this for multiple files in a single folder.

Answers

You are awesome.I am getting the filename and the file record count with this.

But i need the each file creation date also in this. Just see the example below.

Since you’re new to batch scripting this would be a good time to reconsider your options. Here they are:

  • Batch files: A dying art. Batch files are very limited in what they can do and their syntax is often hard to understand. String handling is poor and so is date arithmetic.
  • VBScript: Far more powerful but according to many experts on the way out (which I personally doubt).
  • PowerShell: The replacement for batch files. Far more powerful than batch files, extremely tight code, takes a fair while to get used to.

In other words, why spend time on learning a dying language?

@echo off
echo Audit created on %date% at %time:

:Sub
for /F %%c in (‘type «%*» ^| find /c /v «AABBCC»‘) do set Count=%%c
for /F %%d in («%*») do set FileDate=%%

Windows count lines in all files

This forum is closed. Thank you for your contributions.

Answered by:

Question

How can I count the number of lines using a batch file?

Answers

Try this to count the number of lines in a text file and display it the way you want .

set /p =First Query Text: Tom Lavedas

Try this to count the number of lines in a text file and display it the way you want .

set /p =First Query Text: Tom Lavedas

  • Edited by Tom Lavedas Tuesday, July 26, 2011 1:17 PM to fix minor typos
  • Marked as answer by Boe Prox MVP Wednesday, July 27, 2011 3:01 AM

All replies

You can use the Type and Find commands to count the number of lines in the file that do not contain a string. For example:

If the file does not contain the string, the result will be the number of lines in the file. You can put this in a batch file, and pass the file name as a parameter.

Richard Mueller — MVP Directory Services

this answers my question, thanks a lot. One more. If I want to output this result together with a text. Example:

echo Result of the Query >> Result.txt

Type c:\Scripts\Example.txt | Find /V /C «ZZZXXXYYY» >> Result.txt

Type c:\Scripts\Example.txt | Find /V /C «ZZZ» >> Result.txt

output on Result.txt will be:

Result of the Query

I wanted to do it like this:

output on Result.txt will be:

Result of the Query

First Query Text: 100

Second Query Text:200

Perhaps if you describe your overall goal?

Here is a little simple Powershell script that will count the lines, regardless of content. As Bill said, it would be easier if we understood what you are attempting to achieve:

If you found this post helpful, please give it a «Helpful» vote. If it answered your question, remember to mark it as an «Answer».

Cmon it’sPowerSHell. Nothing takes that much code in PoSH.

(gc test.txt).count

Like you said, wrong again. I love being wrong!

Edit: jv, How did you know to use .count? If I pipe gc test.txt | get-member, I see no count method.

Try it like this:

Find /V /C «ZZZXXXYYY» c:\Scripts\Example.txt
Find /V /C «ZZZXXXYYY» c:\Scripts\Example.txt >> Result.txt
Find /V /C «ZZZXXXYYY» c:\Scripts\*.txt

Try this to count the number of lines in a text file and display it the way you want .

set /p =First Query Text: Tom Lavedas

  • Edited by Tom Lavedas Tuesday, July 26, 2011 1:17 PM to fix minor typos
  • Marked as answer by Boe Prox MVP Wednesday, July 27, 2011 3:01 AM

Hey — I was just teasing him. I didn’t say he was wrong. I was agreeng that PowerShell can do this much more easily.

What? Doesn’t anyone have a sense of humour around here? I know BigTeddy does.

Hey — I was just teasing him. I didn’t say he was wrong. I was agreeng that PowerShell can do this much more easily.

What? Doesn’t anyone have a sense of humour around here? I know BigTeddy does.

Edit: jv, How did you know to use .count? If I pipe gc test.txt | get-member, I see no count method.

That’s a common mistake. Remember that the Powershell pipeline will «unroll» an array into individual elements. When you did this:

you piped an array to get-member. The pipeline peeled the first element of the array off and did a get-member on it, and gave you the result, and you got all the properties and methods of a string object.

To see the members of the array itself, you need to do a get-member on it directly, not through the pipeline:

gm -inputobject (gc test.txt)

then you’ll get the properties methods of the array.

Now mj you know I am not going to let you get away with that and her is why.

Look again at teh original statemnent:

Edit: jv, How did you know to use .count? If I pipe gc test.txt | get-member, I see no count method.

How? Well I dereferenced the results with teh dot operator. Yes, in POwerShell, the dot is an operator. It is also a very much overloaded operator.

#This gets a file as a collection of lines into a variable
$lines=cat file.txt
$lines|gm
$lines.count

# This get a file as a collection of lines with no variable that is visible. This is called an ‘anonymous’ variable.
(cat file.txt).Count

The variable is still created but it is not dsiplayed and it will not be sent to the console because I have asked to to be acted on by teh dot operato to give me only the count of items. It is a very good short hand and makes code more compact, readable and usable. We do it in C all of the time. It is why I have never beencomfortable with VB languages because, until VB.NET, they didn’t lend to doing this. VB code rends to get big and clunky.

So mj. while you were correct about gm you missed the underlying issue which was «what the h**l is that?»

Yes you cannot inspect aa file name.and find a count property. Even if you do this you will not find count.

Why? Because the count property is an alias for array lenght anda few other object collections. As MJ points up. Jsut doing GM on the file result gers only the first element of [string[]] (string array)

Now try these two:

(«hello»).length
(«hello»,»world»).length

(«hello»).count
(«hello»,»world»).count

How to count no of lines in text file and store the value into a variable using batch script?

I want to count the no of lines in a text file and then the value has to be stored into a environment variable. The command to count the no of lines is

I am getting the error message,

How could i get rid of this error.

16 Answers 16

You could use the FOR /F loop, to assign the output to a variable.

I use the cmd-variable , so it’s not neccessary to escape the pipe or other characters in the cmd-string, as the delayed expansion passes the string «unchanged» to the FOR-Loop.

There is a much simpler way than all of these other methods.

If your android device is connected to your PC and you have the android SDK on your path, this prints out the number of apps installed on your device.

Inspired by the previous posts, a shorter way of doing so:

The number of lines

Try it. It works in my console.

EDITED:

(the «$» sign removed)

$ reduces the number by 1 because it is accepting the first row as Field name and then counting the number of rows.

It eliminates the extra FindStr and doesn’t need expansion.

— edited to use ChrisJJ’s redirect suggestion. Removal of the TYPE command makes it three times faster.

@Tony: You can even get rid of the type %file% command.

For long files this should be even quicker.

fa»‘) do set lines=%%b handles short and long file names. – jwalker Jan 22 ’15 at 16:00

I usually use something more like this for /f %%a in (%_file%) do (set /a Lines+=1)

in a batch file, use %%A instead of %A

You don’t need to use find.

This iterates all lines in the file and increases the counter variable by 1 for each line.

The perfect solution is:

I found this solution to work best for creating a log file that maintains itself:

Environmental variables included are %clientname% the computername of the remote client %Date% is the current date and %Time% the current time. :NEXT is called after getting the number of lines in the file. If the file line count is greater than the %maxlines% variable it goes to the :EXITLOOP where it overwrites the file, creating a new one with the first line of information. if it is less than the %maxlines% variable it simply adds the line to the current file.

The :countLines subroutine below accepts two parameters: a variable name; and a filename. The number of lines in the file are counted, the result is stored in the variable, and the result is passed back to the main program.

The code has the following features:

  • Reads files with Windows or Unix line endings.
  • Handles Unicode as well as ANSI/ASCII text files.
  • Copes with extremely long lines.
  • Isn’t fazed by the null character.
  • Raises an error on reading an empty file.
  • Counts beyond the Batch max int limit of (31^2)-1 .

I know it looks hideous, but it covers most edge-cases and is surprisingly fast.

In the below code, the variable name are SalaryCount and TaxCount

Now if you need to output these values to a csv file, you could use the below code.

The > will overwrite the existing content of the file and the >> will append the new data to existing data. The CSV will be generated in D:\CSVOutputPath

You can pipe the output of type into find inside the in(…) clause of a for /f loop:

But the pipe starts a subshell, which slows things down.

Or, you could redirect input from the file into find like so:

But this approach will give you an answer 1 less than the actual number of lines if the file ends with one or more blank lines, as teased out by the late foxidrive in counting lines in a file.

And then again, you could always try:

The trouble is, the output from the above command looks like this:

You could split the string on the colon to get the count, but there might be more than one colon if the filename had a full path.

Here’s my take on that problem:

This will always store the count in the variable.

Just one last little problem… find treats null characters as newlines. So if sneaky nulls crept into your text file, or if you want to count the lines in a Unicode file, this answer isn’t for you.

Читайте также:  Виртуальный рабочий стол linux это
Оцените статью