Count Files by Type

Posted on 2006-06-01
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
    LVL 43

    Accepted Solution

    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.
    LVL 6

    Assisted Solution

    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 (“)
    LVL 6

    Expert Comment


    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
    Clever :-)
    LVL 82

    Assisted Solution

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    At the beginning of the year, the IT world was taken hostage by the shareholders of LogMeIn. Their free product, which had been free for ten years, all of the sudden became a "pay" product. Now, I am the first person who will say that software maker…
    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…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now