windowsbatch-filecmdfilesystemsfile-attributes

Windows command to get list of file extensions


I need a command to get distinct extensions files inside a folder with the number of occurency of each extension.

So how can I do this?


Solution

  • You could do it with a PowerShell one-liner like this:

    powershell "$ext = @{}; gci *.* | %{ $ext[$_.Extension]++ }; $ext"
    

    If using this in a .bat script, double the % sign.


    In pure Batch, this is the simplest way I can think of:

    @echo off & setlocal
    
    for %%I in (*.*) do (
        set /a ext[%%~xI] += 1
    )
    
    set ext[
    

    Or condensed to a one-liner (still in a .bat script):

    @setlocal & @(for %%I in (*.*) do @set /a ext[%%~xI] += 1) & set ext[
    

    Or directly from the cmd console:

    cmd /c ">NUL (@for %I in (*) do @set /a ext[%~xI] += 1) & set ext["
    

    (The cmd /c there behaves like setlocal in a bat script, helping you avoid junking up your environment with useless variables.)