Count Files by Type

Posted on 2006-06-01
Medium Priority
Last Modified: 2010-04-13
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
Question by:UCLAEA
  • 2
  • 2
LVL 43

Accepted Solution

Steve Knight earned 336 total points
ID: 16811834
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.

Assisted Solution

Dark_King earned 332 total points
ID: 16815702
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

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

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

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

Usage: ass <path>

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

Expert Comment

ID: 16815763

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
LVL 43

Expert Comment

by:Steve Knight
ID: 16816049
Clever :-)
LVL 85

Assisted Solution

oBdA earned 332 total points
ID: 16823399
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.

@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%"

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question