简体   繁体   English

Windows 命令获取文件扩展名列表

[英]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 one-liner 来做到这一点:

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

If using this in a .bat script, double the % sign.如果在 .bat 脚本中使用它,请将%符号加倍。


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):或浓缩为单行(仍在 .bat 脚本中):

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

Or directly from the cmd console:或者直接从 cmd 控制台:

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.) (那里的cmd /c行为类似于 bat 脚本中的setlocal ,帮助您避免使用无用的变量破坏您的环境。)

Here is solution from superuser.com .这是来自superuser.com 的解决方案。 Save this code inside a .bat file.将此代码保存在.bat文件中。

@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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