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?
-
Are you wanting counts per folder, per file, or just one grand total? Do spaces count as characters?mjolinor– mjolinor2014-07-11 20:20:55 +00:00Commented Jul 11, 2014 at 20:20
-
Along those same lines, do punctuation count, or are you only looking for an alpha-numeric count?TheMadTechnician– TheMadTechnician2014-07-11 20:27:57 +00:00Commented Jul 11, 2014 at 20:27
4 Answers
$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
1 Comment
Does the below suits to your requirement:
wc -l */*
This is a UNIX command.
1 Comment
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
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.