1

I have a folder with 50 subfolders, within these subfolder are a variety of different text files. What's a powershell script I could run to count the characters in these files?

2
  • Are you wanting counts per folder, per file, or just one grand total? Do spaces count as characters? Commented Jul 11, 2014 at 20:20
  • Along those same lines, do punctuation count, or are you only looking for an alpha-numeric count? Commented Jul 11, 2014 at 20:27

4 Answers 4

2
$results= @{}
Get-ChildItem -Path C:\Temp -Filter *.txt -Recurse  | ForEach-Object{

    $count = Get-Content $_.FullName | Measure-Object -Character
    $results.Add($_.FullName, $count.Characters)}
$results

Output:

Name                           Value                                                                              
----                           -----                                                                              
C:\Temp\EventCombMT.txt        3724                                                                               
C:\Temp\features.txt           222                                                                                
C:\Temp\2test.txt              12                                                                                 
C:\Temp\winpe\realtekLan\no... 147   

Name: The full path of the txt file. Value: The word count. I'm sure it exists but i couldnt find info on how it calculates characters. If you need whitespace to NOT count there is a switch -IgnoreWhiteSpace:$true

The will take all txt files in the directory and subdirectories of "c:temp". Each file's content is piped into Measure-Object to count the characters. The fullname of the file and its character count is put into a hash table. In that form you can manipulate it further. There are other things you could have done of course. This was just my take on it.

For more information on hash: http://technet.microsoft.com/en-us/library/ee692803.aspx

Measure-Object http://blogs.technet.com/b/heyscriptingguy/archive/2011/10/09/use-a-powershell-cmdlet-to-count-files-words-and-lines.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

Sure, there's other ways to do it, but yours is simple and effective. Much better than what I was going to do (splitting it into a character array and counting that way).
0

Does the below suits to your requirement:

 wc -l */*

This is a UNIX command.

1 Comment

wc isn't even a powershell command, I don't see how this is an answer to his question at all.
0

The cmdlet Get-Content outputs string objects. The property "Length" of string objects corresponds to the number of characters in a string : http://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx

So you can contruct a calculated property called "Number of Characters" for each of your files, like so :

Get-ChildItem -Path C:\Test\*.txt -Recurse |
Select-Object -Property Name, @{N="Number of Characters";E={ (get-content $_).length } }

Output :
Name                      Number of Characters
----                      --------------------
New Text Document (2).txt                   15
New Text Document (3).txt                  852
New Text Document.txt                      629

If you want the total number of character for all .txt files in the folder, store the above command into a variable ($TextFiles, for example) and use Measure-Object :

$TextFiles | Measure-Object -Property "Number of Characters" -Sum

output :

Count    : 3
Average  :
Sum      : 1496
Maximum  :
Minimum  :
Property : Number of Characters

1 Comment

If there's more than one line in the file, I believe (get-content $_).length is going to tell you the number of lines in the file, rather than the number of characters.
0

Running V4, I ran about a quarter gig of old mail logs through this:

 Get-ChildItem C:\maillogs\*3.s |


foreach {
          &{
          $ofs = ''
          $Count=0
          Get-Content $_ -ReadCount 1000 | 
          foreach { $count += ([string]$_).length } 
          [PSCustomObject]@{Name = $_.Name ; Count = $Count}
          }
         } | Format-Table -AutoSize



Name                       Count
----                       -----
[email protected] 98479479
[email protected] 88578000
[email protected]   754640
[email protected] 61444158
[email protected] 31823480
[email protected]   633925

in about 22 seconds.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.