Page 1 of 2

Batch file to show count of different filetypes in a folder

Posted: 15 May 2013 01:37
by sifar786
hi,

Does anyone know how to create a batch file to show count of unique filetypes in a folder?

.xls 3
.pdf 2
.doc 5
.txt 12
.mdb 10
.xlsm 6
.xml 6

etc

the following only gives the total count of files in a folder:

Code: Select all

DIR /B /A:-D | FIND "" /V /C

Re: Batch file to show count of different filetypes in a fol

Posted: 15 May 2013 03:08
by Endoro
try this:

Code: Select all

for %i in (.xls .pdf .doc .txt .mdb .xml) do @if exist *%i dir /b /a-d *%i|find /c /v ""&echo %i

Windows counts .xls and .xlsm together.

Re: Batch file to show count of different filetypes in a fol

Posted: 15 May 2013 05:31
by trebor68
With this file you can find various types of files within that folder and count. But there are problems if the file name has more than one point in the file name.

Code: Select all

@echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set num=0
set "suffix="
for /f "tokens=1* delims=." %%s in ('dir /b /a-d /o:gen') do (
   if /i "%%t" equ "!suffix!" (
      set /a num+=1
   ) else (
      echo .!suffix!     !num!
      set num=1
      set suffix=%%t
   )
)
echo .%suffix%     %num%

Re: Batch file to show count of different filetypes in a fol

Posted: 15 May 2013 08:14
by Squashman
trebor68 wrote:With this file you can find various types of files within that folder and count. But there are problems if the file name has more than one point in the file name.

Code: Select all

@echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set num=0
set "suffix="
for /f "tokens=1* delims=." %%s in ('dir /b /a-d /o:gen') do (
   if /i "%%t" equ "!suffix!" (
      set /a num+=1
   ) else (
      echo .!suffix!     !num!
      set num=1
      set suffix=%%t
   )
)
echo .%suffix%     %num%

Don't use a period as a delimiter. Use the command modifiers. %%~xI

Re: Batch file to show count of different filetypes in a fol

Posted: 15 May 2013 08:30
by !k

Code: Select all

@echo off

setlocal
for /f "delims=" %%f in ('dir /b/a-d "%windir%"') do set /a files%%~xf = files%%~xf +1
set files.
pause

Re: Batch file to show count of different filetypes in a fol

Posted: 15 May 2013 10:37
by foxidrive
Nice one !k. That works well and is elegant.

Re: Batch file to show count of different filetypes in a fol

Posted: 15 May 2013 11:49
by sifar786
!K, this is really elegant as Foxdrive mentioned. Thanks :)

However, could you breakdown the steps and explain to me how it works in getting a unique list without using a ' | FIND "" /V /C' ?

Code: Select all

@echo off

setlocal
for /f "delims=" %%f in ('dir /b/a-d "%cd%"') do set /a files%%~xf = files%%~xf +1
set files.
pause


Also, can the "files." that gets prefixed to every extension be removed?

Re: Batch file to show count of different filetypes in a fol

Posted: 15 May 2013 12:27
by foxidrive
This removes the "files" part of the variable name.

Code: Select all

@echo off
setlocal
for /f "delims=" %%f in ('dir /b/a-d "%cd%"') do set /a %%~xf=%%~xf +1
set .
pause

Re: Batch file to show count of different filetypes in a fol

Posted: 15 May 2013 12:56
by !k
DIR lists the files (you can use *.filters). Each line of its output increases the value of the variable named ".%extension%"

Code: Select all

setlocal
for /f "delims=" %%f in ('dir /b/a-d *.xls *.pdf *.doc *.txt') do set /a %%~xf = %%~xf +1
set .

Because file extensions are different, each file increases the value of the variable corresponding to its type

Re: Batch file to show count of different filetypes in a fol

Posted: 15 May 2013 13:05
by Endoro
for files without extension you get a "missing operand" error message

Re: Batch file to show count of different filetypes in a fol

Posted: 15 May 2013 13:11
by sifar786
we are not ECHOing anything, so how does the For print the output?

Re: Batch file to show count of different filetypes in a fol

Posted: 15 May 2013 13:20
by !k
Endoro wrote:for files without extension you get a "missing operand" error message

Code: Select all

setlocal
for /f "delims=" %%f in ('dir /b/a-d *.*) do (
   if "%%~xf"=="" set /a .without.extension +=1
   set /a %%~xf +=1 2>nul
)
set .

sifar786 wrote:we are not ECHOing anything, so how does the For print the output?

Not FOR, but SET

Re: Batch file to show count of different filetypes in a fol

Posted: 15 May 2013 14:40
by trebor68
Here the modified batch file.

Thanks to Squashman for your info.

Code: Select all

@echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set num=0
set "suffix="
for /f "delims=" %%s in ('dir /b /a-d /o:gen') do (
   if /i "%%~xs" equ "!suffix!" (
      set /a num+=1
   ) else (
      if !num! neq 0 echo "!suffix!"     !num!
      set num=1
      set suffix=%%~xs
   )
)
echo "%suffix%"     %num%

Re: Batch file to show count of different filetypes in a fol

Posted: 15 May 2013 19:19
by Aacini
This version also manage files without extension:

Code: Select all

@echo off
setlocal
for %%f in (*.*) do set /A FilesOfType%%~Xf+=1
set FilesOfType

Re: Batch file to show count of different filetypes in a fol

Posted: 15 May 2013 19:39
by Squashman
I find it odd that DIR can find files without an extension but there no native way to force DIR to show you files with an extension only.

Code: Select all

C:\Users\Squashman\Downloads>dir /b /a-d *.
noext

In my logical mind the period should force it not to match anything unless the filename was noext.