counting .bin files in a folder
Moderator: DosItHelp
counting .bin files in a folder
Hello,
I am trying to count the number of .bin files in my folder that include a particular name pattern. I basically need to count how many such files I have for each name type.
For instance in my folder I have files like
Query.1.bin
Query.2.bin
Query.3.bin
and the response should be 3.
I have
set count=0
set pattern=Query.%%?%%.bin
for /f %%i in ('dir /b /a-d %pattern% ^| find /c /v ""') do @call set count=%%i
but my code does not recognize "?" and doesn't replace it with numbers and I get 0 in response.
I would really appreciate it if you can give some help on this.
Best
P.A.
I am trying to count the number of .bin files in my folder that include a particular name pattern. I basically need to count how many such files I have for each name type.
For instance in my folder I have files like
Query.1.bin
Query.2.bin
Query.3.bin
and the response should be 3.
I have
set count=0
set pattern=Query.%%?%%.bin
for /f %%i in ('dir /b /a-d %pattern% ^| find /c /v ""') do @call set count=%%i
but my code does not recognize "?" and doesn't replace it with numbers and I get 0 in response.
I would really appreciate it if you can give some help on this.
Best
P.A.
Re: counting .bin files in a folder
It thinks %?% is a variable name. Why do you have the percent signs in there?
Re: counting .bin files in a folder
first,you don't need to use the pattern out side the for loop, and if you want the .bin files only there is no need for a pattern, but if you have another bin files that you don't want to be counted then use a pattern.
second you don't append the number to the count variable
code should look like this
if want to use pattern (note the "Query.*.bin" instead of "*.bin" )
second you don't append the number to the count variable
code should look like this
Code: Select all
@echo off
set count=0
for /f "tokens=* delims=" %%a in ('Dir /B /A:-D "*.bin"') Do set /a count += 1
echo There is %count% .bin File(s)
pause
if want to use pattern (note the "Query.*.bin" instead of "*.bin" )
Code: Select all
@echo off
set count=0
for /f "tokens=* delims=" %%a in ('Dir /B /A:-D "Query.*.bin"') Do set /a count += 1
echo There is %count% .bin File(s)
pause
Last edited by abc0502 on 02 Oct 2012 13:06, edited 1 time in total.
Re: counting .bin files in a folder
I would agree with you on the * vs ?. If the file numbers were greater than 9 then it should not match.
He can use his original code as long has he removes the percent signs and uses the *.
He can use his original code as long has he removes the percent signs and uses the *.
Code: Select all
set count=0
set pattern=Query.*.bin
for /f %%i in ('dir /b /a-d %pattern% ^| find /c /v ""') do set count=%%i
Re: counting .bin files in a folder
and he should also use the pattern in the for loop, there is no need to be out in separate variable.
and the "set count=0" there is no need for it as long he won't append numbers to it.
and the "set count=0" there is no need for it as long he won't append numbers to it.
Re: counting .bin files in a folder
I did what you suggested and I got the correct answer, the only problem I have now is that when I rerun it, it gives me again zero. For some reason it only works one time and then cannot count again.
Re: counting .bin files in a folder
This is my revised code
set pattern=Query.*.bin
for /f "tokens=* delims=" %%i in ('Dir /B /A:-D %pattern%') do set /a count+=1
echo There is %count% .bin File(s)
set pattern=Query.*.bin
for /f "tokens=* delims=" %%i in ('Dir /B /A:-D %pattern%') do set /a count+=1
echo There is %count% .bin File(s)
Re: counting .bin files in a folder
palireza wrote:This is my revised code
set pattern=Query.*.bin
for /f "tokens=* delims=" %%i in ('Dir /B /A:-D %pattern%') do set /a count+=1
echo There is %count% .bin File(s)
Does that work for you?
I assume your filenames actually contain percent signs, and that is a recipe for problems.
Re: counting .bin files in a folder
you mixed many codes together, if you are going to use the "set /a count", then you have to "set count=0" before the for loop.
your first code is fine, just remove the "set count=0" & replace the "%%?%%" with "*"
and your's should work fine
your first code is fine, just remove the "set count=0" & replace the "%%?%%" with "*"
and your's should work fine
Re: counting .bin files in a folder
Thank you guys! All fixed 

Re: counting .bin files in a folder
palireza wrote:Thank you guys! All fixed
What was your final code?
Re: counting .bin files in a folder
set count=0
for /f "tokens=* delims=" %%i in ('Dir /B /A:-D "Query.*.bin"') do set /a count+=1
for /f "tokens=* delims=" %%i in ('Dir /B /A:-D "Query.*.bin"') do set /a count+=1
Re: counting .bin files in a folder
Why tokenizing ?palireza wrote:set count=0
for /f "tokens=* delims=" %%i in ('Dir /B /A:-D "Query.*.bin"') do set /a count+=1
Code: Select all
for /f %%i in ('Dir /B /A:-D "Query.*.bin"') do set /a count+=1
Code: Select all
for %%? in ("Query.*.bin") do set /a count+=1
echo. %count% pattern matches relative to active directory.
Re: counting .bin files in a folder
Ed, I tried without token and /f and you were correct, it works without them fine.