Count Files by Type

I need to determine the number of files, by type, on a UNC path.  The expected output would be like this:
.TXT 100 files
.DOC 2000 files
.MP3 4000 files
...etc...
UCLAEAAsked:
Who is Participating?
 
Steve KnightIT ConsultancyCommented:
Obvious easy solution would be Tree size Pro I suppose... google for it.

Other than that, assuming you don't want to just check a particular file type (in which case dir *.mp3 /s | Find "File(s)" will show the total line )
that you could do something with a command like this -- details taken from last time I had to this myself without TS Pro available :-)

dir \\server\share\*.*/s | find "." | find /v "<DIR>" | find /v "Directory of" > afile.txt

which sorts out all files with extensions then excludes the directory entries and send them into a text file.

Load up Excel
Open the text file
In the wizard select fixed width and select the defaults Excel gives you
Column D will contain the filename.  Delete column A-C
Insert line at A1 and enter A1 as Filename and B1 as extension
In B2 enter  =MID(A2,FIND(".",A2),99)
Copy B2 and paste to rest of B column
Highlight column A & B
data  | Pivot table report
Drag extension to row
Drag Filename to Data (will show as count of filename)
Next, new worksheet.
Double click on extension, click on Advanced
Autosort options -- select descedning by count of filename
Autoshow options -- Automatic, top 50 or something of "count of filename"
OK a few times.
0
 
Dark_KingCommented:
It took time to figure out this one.

make a batch file like this and name it ass.bat

******Batch start*****
@echo off
set path1="%~f1"
SET /a count=0
assoc > %TEMP%.\ass1.txt
   If Not Exist %TEMP%.\ass1.txt (
     Echo Can not write to: %TEMP%.\ass1.txt
     Goto :EOF1)

FOR /F "tokens=1 delims==" %%G IN (%TEMP%.\ass1.txt) DO (call :proc1 "%%G")

goto :eof1

:proc1
SET /a count=0
FOR /R %path1% %%G IN (*%~x1) DO (call :s_do_sums "%%G")
IF %count% GTR 0 echo %~x1 %count% files
GOTO :eof

:s_do_sums
rem echo %count%:%1
 set /a count+=1
 GOTO :eof

:eof1
Del %TEMP%.\ass1.txt
echo Done!
******Batch end*****

Usage: ass <path>

Example
ass c:\mydokuments
ass “c.\my documents”
 
You see if you have space in path you need to use (“)
0
 
Dark_KingCommented:
Explanation.

First I use assoc to make a file list with all extension in your computer
assoc > %TEMP%.\ass1.txt

Then a loop reading this file with extension, and for every one it call proc1
FOR /F "tokens=1 delims==" %%G IN (%TEMP%.\ass1.txt) DO (call :proc1 "%%G")

Then a loop reading in your path for this extension and every time it found one it call “s_do_sums” to count.
FOR /R %path1% %%G IN (*%~x1) DO (call :s_do_sums "%%G")

And if count is not zero it echo it to cmd window
IF %count% GTR 0 echo %~x1 %count% files

And I forget to say you need to open CMD and run ass.bat from there
0
 
Steve KnightIT ConsultancyCommented:
Clever :-)
0
 
oBdACommented:
Different approach:
Create a directory listing including subdirectories, and gather only the file extensions. Sort this list with file extensions, and finally just count.
Just pass the folder to search as first argument; if no folder is specified in the command line, the current folder will be used.

====8<----[CountExt.cmd]----
@echo off
setlocal enabledelayedexpansion
if "%~1"=="" (set Folder=.) else (set Folder=%~1)

if not exist "%Folder%" (
  echo Folder not found.
  goto :eof
)
set TmpFile1=%~dpn0-1.tmp
set TmpFile2=%~dpn0-2.tmp
if exist "%TmpFile1%" del "%TmpFile1%"
if exist "%TmpFile2%" del "%TmpFile2%"
echo Scanning folders ...
for /r "%Folder%" %%a in ("*.*") do >>"%TmpFile1%" echo %%~xa
echo Sorting extensions ...
sort "%TmpFile1%" /o "%TmpFile2%"
echo Counting extensions ...
set CurrentExtension=::NONE::
for /f %%a in ('type "%TmpFile2%"') do (
  if /i "!CurrentExtension!"=="::NONE::" (
    set CurrentExtension=%%a
    set CurrentCount=1
  ) else (
    if /i "%%a"=="!CurrentExtension!" (
    set /a CurrentCount += 1
    ) else (
      echo !CurrentExtension! !CurrentCount! files
      set CurrentExtension=%%a
      set CurrentCount=1
    )
  )
)
echo !CurrentExtension! !CurrentCount! files
del "%TmpFile1%"
del "%TmpFile2%"
====8<----[CountExt.cmd]----
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.