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

2 Answers 2

up vote 9 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!%


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).


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
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 @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
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
+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

Your Answer


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.