Although not strictly meeting the requirement for a batch script, I have used a single-line powershell script:
Get-Childitem C:\MyDirectory -Recurse | WHERE { -NOT $_.PSIsContainer } | Group Extension -NoElement | Sort Count -Desc > FileExtensions.txt
You could potentially run it from the command line/batch file:
Powershell -Command "& Get-Childitem C:\MyDirectory -Recurse | WHERE { -NOT $_.PSIsContainer } | Group Extension -NoElement | Sort Count -Desc > FileExtensions.txt"
I claim no credit for it, and of course, you will need Powershell installed. For newer versions of Windows, there isn't any getting around this.
If you remove C:\MyDirectory
it will execute in the current directory.
At the end it will produce a FileExtensions.txt containing something like the following:
+-------+------+
| Count | Name |
+-------+------+
| ----- | ---- |
| 8216 | .xml |
| 4854 | .png |
| 4378 | .dll |
| 3565 | .htm |
| ... | ... |
+-------+------+
Depending on your folder structure, you may occasionally get errors notifying you that you have a long path.
Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
Any subdirectories in there will also not be parsed but the results for everything else will still show.
1It worked PERFECTLY! I used the following syntax:
batchfile "path" >filename.txt
– lucaferrario – 2014-10-28T20:41:41.893Great script! But there is a small bug with it : if the folder contain files
aaa.css
andzzz.cs
, extension.cs
will not be reported by the script. – Goozak – 2016-10-20T17:24:10.7531@Goozak Whoops. Fixed now. The wonders of text searching... had to make sure the search query ended with
:
to force it to match boundaries. – Bob – 2016-10-20T23:08:01.500+1 @Bob: Amazing answer, adding the explanation was a huge help too. Just tested the script, reviewed the results of the test, and everything worked great. Again, thanks! – blunders – 2012-03-07T17:04:09.207