简体   繁体   中英

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?

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

Here is solution from superuser.com . Save this code inside a .bat file.

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM