Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Within a directory, and recursively within it's sub-directories, meaning every directory within a directory is processed, how do I compile a complete list of unique extensions within the directory?

OS is Windows XP with all the current updates, but I okay running script if I'm able to tell what it's doing, though I would prefer not to have to install dot-net, since I really do not like it.

share|improve this question

3 Answers 3

up vote 10 down vote accepted

This batch script will do it.

@echo off

set target=%1
if "%target%"=="" set target=%cd%

setlocal EnableDelayedExpansion

set LF=^


rem Previous two lines deliberately left blank for LF to work.

for /f "tokens=*" %%i in ('dir /b /s /a:-d "%target%"') do (
    set ext=%%~xi
    if "!ext!"=="" set ext=FileWithNoExtension
    echo !extlist! | find "!ext!" > nul
    if not !ERRORLEVEL! == 0 set extlist=!extlist!!ext!:
)

echo %extlist::=!LF!%

endlocal

Save it as any .bat file, and run it with the command batchfile (substitute whatever you named it) to list the current directory, or specify a path with batchfile "path". It will search all subdirectories.

If you want to export to a file, use batchfile >filename.txt (or batchfile "path" >filename.txt).

Explanation

Everything before the for /f... line just sets things up: it gets the target directory to search, sets delayed expansion which lets me do some magic with the variables in the loop and defines a newline (LF) that I can use for neater output.

The loop uses that dir /b /s /a:-d "%target%" command, grabbing a list of all files in all subdirectories under the target.

%%~xi extracts the extension out of the full paths the dir command returns.

An empty extension is replaced with "FileWithNoExtension", so you know there is such a file - if I added an empty line instead, it's not quite as obvious.

The whole current list if sent through a find command, to ensure uniqueness. The output of the find command is sent to nul, essentially a black hole - we don't want it.

%ERRORLEVEL% is set by the find command, a value of 0 indicates there was a match. So if it's not 0, the current extension is not on the list so far and should be added.

The echo line basically outputs, and I also replace my placeholders (:) with newlines to make it look nice.

share|improve this answer
    
+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 Mar 7 '12 at 17:04

Here's a detailed answer using PowerShell (with Windows XP you'll have to install PowerShell):

Hey, Scripting Guy! How Can I Use Windows PowerShell to Pick Out the Unique File Extensions Used in a Collection of Files?

share|improve this answer
1  
While PowerShell is definitely much easier than the command line, it is based on .NET. Which, unfortunately, goes against "I would prefer not to have to install dot-net". –  Bob Mar 7 '12 at 15:36
1  
+1 @RichardM: Agree with Bob. Also, the code related to the counting of extension instances found -- not knowing anything about PowerShell -- appears very memory heavy; meaning instead of just keeping a count of every instance, it's I believe creating an array to store duplicate instances of an extension for each extension, then doing a count for each extension array at the end, which to me seems like a very odd way of counting extension instances. Am I missing something? (That said, the first PowerShell one-liner is nice, and I'd try it if I didn't dislike dotnet.) –  blunders Mar 7 '12 at 22:05
1  
That's fair. This question may draw searchers who are more open to a PowerShell solution. Mind you, a decent Google search will find the above link as well. –  RichardM Mar 9 '12 at 13:48
2  
+1 for this link. blunders obvious dislikes everything .net, but that doesn't mean that the solution above is the best long term solution to this problem. The more languages the better i think. –  Steve Rathbone Jun 16 '12 at 4:12
    
Here's another link that addresses recursive search, using powershell. robertbigec.wordpress.com/2011/01/07/… –  goodeye Jan 4 '13 at 3:09

For posterity, the powershell script I have used is:

Get-Childitem C:\MyDirectory -Recurse | WHERE { -NOT $_.PSIsContainer } | Group Extension -NoElement | Sort Count -Desc > FileExtensions.txt

Also, 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 OSs, there isn't any getting around this.

Also, you don't need C:\MyDirectory and, if you remove it, 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 |
| ...   | ...  |
+-------+------+
share|improve this answer
    
Thanks, agree that it's a useful answer. On an unrelated note, bit puzzled how you've posted only single answer, yet have the "Fanatic" badge for visiting Superuser for 100 consecutive days. Do you have the site bookmarked or something? –  blunders Sep 18 at 4:02
    
The badge was awarded in 2010 when I effectively lurked, but I'm far more active on SO: stackoverflow.com/users/31532/dan-atkinson. :) –  Dan Atkinson Sep 18 at 12:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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