如何按副檔名確定資料夾和子資料夾中的檔案總數?

如何按副檔名確定資料夾和子資料夾中的檔案總數?

如何遍歷一個資料夾及其子資料夾(目錄及其子目錄)並列出這些資料夾中出現的每個副檔名總共有多少個特定副檔名的檔案?

與此類似的東西,

folders               48 total
.jpg                2842 total
.gif                 142 total
.CR2                2842 total

dir/s上述總和應與列出某個資料夾中所有檔案和子資料夾的總數相同。

答案1

如果不需要命令列,您可以使用 WinDirStat。http://windirstat.info/download.html在此輸入影像描述

答案2

下一個程式碼片段可能會有所幫助:

@ECHO OFF >NUL
SETLOCAL EnableExtensions
rem EnableDelayedExpansion
rem next line: clear/delete all `_filesTotal` environment variables
for /F "delims==" %%G in ('set _filesTotal 2^>NUL') do set "%%G="
rem next line: initialize `_foldersTotal` environment variable
set "_foldersTotal=0"
pushd "folder where count"
for /F "delims=" %%G in ('dir /B /S /A') do (
  if exist "%%~G\" (
    rem folder %%G
    set /A "_foldersTotal+=1"
  ) else (
    rem   file %%G
    if "%%~xG"=="" (set /A "_filesTotal.+=1") else (set /A "_filesTotal%%~xG+=1")
  )
)
rem next two lines: display raw results
echo %CD%
set _foldersTotal
set _filesTotal
popd

資源(必讀):

答案3

我強烈建議您考慮使用 Powershell 方法來解決您的問題。它的程式碼行稍微少一些,並且能夠對輸出進行排序...

$foldercount = 0
$hash = @{}
Get-ChildItem -Path "C:\Code Samples" -Recurse | ForEach-Object { 
    if ($_.Attributes -eq 'Directory') 
        { 
        ++$foldercount
        }
    else
        {
        if ($hash.ContainsKey($_.Extension)) 
            { $count = $hash.Get_Item($_.Extension); ++$count; $hash.Set_Item($_.Extension, $count) }
        else
            { $hash.Add($_.Extension, 1) } 
        }
}

Write-Host $foldercount folders
$hash.GetEnumerator() | Sort-Object Value -descending

並且,為我的範例資料夾產生此輸出...

90 folders

Name                           Value                                                                                                                                                                                                 
----                           -----                                                                                                                                                                                                 
.tlog                          186                                                                                                                                                                                                   
.h                             72                                                                                                                                                                                                    
.obj                           56                                                                                                                                                                                                    
.cpp                           54                                                                                                                                                                                                    
.pdb                           26                                                                                                                                                                                                    
.manifest                      24                                                                                                                                                                                                    
.res                           23                                                                                                                                                                                                    
.rc                            22                                                                                                                                                                                                    
.log                           15                                                                                                                                                                                                    
.lastbuildstate                12                                                                                                                                                                                                    
.ipch                          12                                                                                                                                                                                                    
.ico                           12                                                                                                                                                                                                    
.exe                           12                                                                                                                                                                                                    
.idb                           12                                                                                                                                                                                                    
.vcxproj                       11                                                                                                                                                                                                    
.ilk                           11                                                                                                                                                                                                    
.user                          11                                                                                                                                                                                                    
.sdf                           11                                                                                                                                                                                                    
.zip                           11                                                                                                                                                                                                    
.filters                       11                                                                                                                                                                                                    
.sln                           11                                                                                                                                                                                                    
.pch                           11                                                                                                                                                                                                    
.txt                           8                                                                                                                                                                                                     
.gif                           8                                                                                                                                                                                                     
.rc2                           8                                                                                                                                                                                                     
.aps                           8                                                                                                                                                                                                     
.bmp                           7                                                                                                                                                                                                     
.dsw                           6                                                                                                                                                                                                     
.dsp                           6                                                                                                                                                                                                     
.png                           5                                                                                                                                                                                                     
.css                           5                                                                                                                                                                                                     
.html                          4                                                                                                                                                                                                     
.old                           4                                                                                                                                                                                                     
.XML                           4                                                                                                                                                                                                     
.vcproj                        4                                                                                                                                                                                                     
.xslt                          4                                                                                                                                                                                                     
.exp                           3                                                                                                                                                                                                     
.dll                           3                                                                                                                                                                                                     
.lib                           3                                                                                                                                                                                                     
.clw                           2                                                                                                                                                                                                     
.def                           2                                                                                                                                                                                                     
.opt                           2                                                                                                                                                                                                     
.ncb                           2                                                                                                                                                                                                     
.plg                           2                                                                                                                                                                                                     
.recipe                        2                                                                                                                                                                                                     
.rtf                           1                                                                                                                                                                                                     
.jpg                           1                                                                                                                                                                                                     
.h original                    1                                                                                                                                                                                                     
.bat                           1                                                                                                                                                                                                     
.js                            1                                                                                                                                                                                                     
.cpp original                  1                                                                                                                                                                                                     
.DPbcd                         1                                                                                                                                                                                                     

答案4

這是一個非常簡單有效的方法 find . -type f | sed -n 's/..*\.//p' | sort | uniq -c

相關內容