Reputation: 1
I'm rather new to the use of batch files and the codes involved. What I would like to do is get both a list of directories/sub-directories and get the number of specific files (.txt, .MP4, etc.) in each directory, and then store both in a .txt file. I've already figured out how to get a list of directories and store them:
dir /b /ad "C:\FILE" > "C:\List.txt"
Now how would I get the number of files in each directory? I would prefer the format in the text file to be something like:
"folder1 - 5 files"
"folder2 - 3 files"
Folder name and the number of files in a line, then the next folder. Thank you for any help you can give me.
Upvotes: 0
Views: 420
Reputation: 30113
Next script might work as expected. Some basic explanation given using rem
comments.
@ECHO OFF >NUL
SETLOCAL enableextensions
rem set up initial folder
set "myFolder=C:\FILE"
rem set up extension list (no * or ? wildcards, please)
rem could be defined explicitly as follows
set "myExtens=bat html"
rem or, with next proposed usage: 28786418.bat "bat html"
set "myExtens=%~1"
rem or, with next (current) usage: 28786418.bat bat html
set "myExtens=%*"
rem call a subroutine to output folder name & file count
call :filecount "%myFolder%"
rem call that subroutine for ech subfolder (cf. /A:D /S switch)
for /F "tokens=*" %%g in ('
DIR /B /A:D /S "%myFolder%"
') do call :filecount "%%~g"
ENDLOCAL
goto :eof
:filecount
rem subroutine
rem input: (full) path to folder
rem output: echo "input" - file count
rem init a file counter to zero
set /A "numFiles=0"
rem increment file counter by 1 for each file in folder
rem note '2^>nul' to avoid "file not found" message if folder empty
rem note 'set' does not require delayed variable expansion
rem as the 'numFiles' variable correctly increments
for /F "tokens=*" %%G in ('
DIR /B /A:-D "%~1" 2^>nul
') do (
rem treat the extension list if any
rem ( or insert here any other logic instead )
if defined myExtens (
for %%H in (%myExtens%) do if /i ".%%~H"=="%%~xG" set /A "numFiles+=1"
) else (
set /A "numFiles+=1"
)
)
rem display output to console (command line window)
echo "%~1" - %numFiles%
goto :eof
To store output to a text file, you could use (suppose the script is saved as 28786418.bat
)
28786418.bat>list.txt
Edit (a supplement): output with set "myFolder=d:\bat\StackOverflow"
==>D:\bat\StackOverflow\28786418.bat
"d:\bat\StackOverflow" - 44
"d:\bat\StackOverflow\files" - 27
"d:\bat\StackOverflow\files\backup_done" - 0
"d:\bat\StackOverflow\files\Roboto" - 0
"d:\bat\StackOverflow\files\root" - 0
"d:\bat\StackOverflow\files\Roboto\backup_done" - 0
"d:\bat\StackOverflow\files\root\Ian-ionescu" - 2
"d:\bat\StackOverflow\files\root\John-doe" - 2
"d:\bat\StackOverflow\files\root\Nicola-sheperd" - 1
"d:\bat\StackOverflow\files\root\Sara-smith" - 2
==>D:\bat\StackOverflow\28786418.bat bat html>files\28786418.txt
==>type files\28786418.txt
"d:\bat\StackOverflow" - 40
"d:\bat\StackOverflow\files" - 3
"d:\bat\StackOverflow\files\backup_done" - 0
"d:\bat\StackOverflow\files\Roboto" - 0
"d:\bat\StackOverflow\files\root" - 0
"d:\bat\StackOverflow\files\Roboto\backup_done" - 0
"d:\bat\StackOverflow\files\root\Ian-ionescu" - 0
"d:\bat\StackOverflow\files\root\John-doe" - 0
"d:\bat\StackOverflow\files\root\Nicola-sheperd" - 0
"d:\bat\StackOverflow\files\root\Sara-smith" - 0
==>
Upvotes: 1