I have a folder in which there are a bunch of various kinds of text files. The filenames are constructed so that there's a main body and a date in the filename, for example:
AAA_20150630.txt
AAA_20150629.txt
BBB_20150630.txt
CCC_20150630.txt
CCC_20150701.txt
Now I want to count how many files there are for each type of file, so that in this case the desired result would be:
AAA 2
BBB 1
CCC 2
I also have another text file, filelist.txt
, that lists all possible types, ie.
AAA
BBB
CCC
I'm trying to build a CMD script that would loop through the rows of filelist.txt
and count the occurrances of each type of file in the folder.
So far, I've been following the advice in this question, succesfully building a script for counting how many times a single type of file appears (%source_dir% is my folder that has been set earlier on):
set count=0
for /f %%k in ('dir /b /a-d %source_dir%\AAA_* ^| find /c /v ""') do set count=%%k
echo number of AAA files = %count%
However, I ran into a problem when trying to build another for-loop around this one in order to go through all the types of file listed in filelist:
for /f %%i in (%filelist%) do (
set count=0
for /f %%j in ('dir /b /a-d %source_dir%\%%i_* ^| find /c /v ""') do set count=%%j
echo number of %%i files = %count%
)
This code does not work: it loops through all the rows, but it only counts the actual number for the first item in %filelist%, and then returns that result for all consecutive ones.
Why does this happen and how can I fix it?