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

Lam*_*fif 0 windows filesystems cmd batch-file file-attributes

我需要一个命令来获取文件夹中不同的扩展文件,其中包含每个扩展的出现次数。

那么我该怎么做呢?

roj*_*ojo 5

你可以用这样的 PowerShell one-liner 来做到这一点:

powershell "$ext = @{}; gci *.* | %{ $ext[$_.Extension]++ }; $ext"
Run Code Online (Sandbox Code Playgroud)

如果在 .bat 脚本中使用它,请将%符号加倍。


在纯批处理中,这是我能想到的最简单的方法:

@echo off & setlocal

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

set ext[
Run Code Online (Sandbox Code Playgroud)

或浓缩为单行(仍在 .bat 脚本中):

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

或者直接从 cmd 控制台:

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

cmd /c那里的行为类似于setlocalbat 脚本,帮助您避免使用无用的变量破坏您的环境。)