Batch file to show count of different filetypes in a folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

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

#16 Post by Endoro » 15 May 2013 22:35

this works here:

Code: Select all

C:\TEST>dir *. /a-d /b
file1
out
rd
rgb
test1

kid
Posts: 2
Joined: 03 Jun 2023 11:37

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

#17 Post by kid » 03 Jun 2023 12:07

hi, I'm a new member, can anyone explain this code to me line by line in a way that I can understand it completely please:

Code: Select all

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

OJBakker
Expert
Posts: 90
Joined: 12 Aug 2011 13:57

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

#18 Post by OJBakker » 04 Jun 2023 01:27

This batchfile lists for the files in the current directory the file extensions found and how many files have these extensions.

Line by line:
The setlocal is needed so the extensions/counters will not be stored in the environment and persist after the batchfile is finished.

The dir /b/a-d "%cd%" part of the for line lists all files in the current directory and returns only the complete filename.
The set /a part creates or updates a variable with the name of the extension (including the .) and increases this variable by 1.

The set . lists all variables with names starting with a dot.

Example:
directory with only file ListExt.cmd
The dir part will list only 1 file with the name 'ListExt.cmd'
The set part will create a variable with the name .cmd
This variable will be empty on start (implicit zero for numerical operations) and will be increased by 1.
The set . line will show the variable as: .cmd=1

Post Reply