Batch file to show count of different filetypes in a folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
sifar786
Posts: 27
Joined: 15 May 2013 01:26

Batch file to show count of different filetypes in a folder

#1 Post by sifar786 » 15 May 2013 01:37

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

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

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

#2 Post by Endoro » 15 May 2013 03:08

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.

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

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

#3 Post by trebor68 » 15 May 2013 05:31

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%

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

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

#4 Post by Squashman » 15 May 2013 08:14

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

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

#5 Post by !k » 15 May 2013 08:30

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#6 Post by foxidrive » 15 May 2013 10:37

Nice one !k. That works well and is elegant.

sifar786
Posts: 27
Joined: 15 May 2013 01:26

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

#7 Post by sifar786 » 15 May 2013 11:49

!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?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#8 Post by foxidrive » 15 May 2013 12:27

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

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

#9 Post by !k » 15 May 2013 12:56

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

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

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

#10 Post by Endoro » 15 May 2013 13:05

for files without extension you get a "missing operand" error message

sifar786
Posts: 27
Joined: 15 May 2013 01:26

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

#11 Post by sifar786 » 15 May 2013 13:11

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

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

#12 Post by !k » 15 May 2013 13:20

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

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

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

#13 Post by trebor68 » 15 May 2013 14:40

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%

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

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

#14 Post by Aacini » 15 May 2013 19:19

This version also manage files without extension:

Code: Select all

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

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

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

#15 Post by Squashman » 15 May 2013 19:39

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.

Post Reply