counting .bin files in a folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
palireza
Posts: 6
Joined: 02 Oct 2012 11:20

counting .bin files in a folder

#1 Post by palireza » 02 Oct 2012 11:25

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.

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

Re: counting .bin files in a folder

#2 Post by Squashman » 02 Oct 2012 11:42

It thinks %?% is a variable name. Why do you have the percent signs in there?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: counting .bin files in a folder

#3 Post by abc0502 » 02 Oct 2012 12:50

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

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.

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

Re: counting .bin files in a folder

#4 Post by Squashman » 02 Oct 2012 13:02

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 *.

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

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: counting .bin files in a folder

#5 Post by abc0502 » 02 Oct 2012 13:04

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.

palireza
Posts: 6
Joined: 02 Oct 2012 11:20

Re: counting .bin files in a folder

#6 Post by palireza » 02 Oct 2012 14:02

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.

palireza
Posts: 6
Joined: 02 Oct 2012 11:20

Re: counting .bin files in a folder

#7 Post by palireza » 02 Oct 2012 14:16

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)

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

Re: counting .bin files in a folder

#8 Post by foxidrive » 02 Oct 2012 16:17

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.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: counting .bin files in a folder

#9 Post by abc0502 » 03 Oct 2012 03:05

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

palireza
Posts: 6
Joined: 02 Oct 2012 11:20

Re: counting .bin files in a folder

#10 Post by palireza » 04 Oct 2012 09:13

Thank you guys! All fixed :D

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

Re: counting .bin files in a folder

#11 Post by Squashman » 04 Oct 2012 11:06

palireza wrote:Thank you guys! All fixed :D

What was your final code?

palireza
Posts: 6
Joined: 02 Oct 2012 11:20

Re: counting .bin files in a folder

#12 Post by palireza » 04 Oct 2012 11:45

set count=0
for /f "tokens=* delims=" %%i in ('Dir /B /A:-D "Query.*.bin"') do set /a count+=1

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: counting .bin files in a folder

#13 Post by Ed Dyreen » 05 Oct 2012 20:52

palireza wrote:set count=0
for /f "tokens=* delims=" %%i in ('Dir /B /A:-D "Query.*.bin"') do set /a count+=1
Why tokenizing ?

Code: Select all

for /f %%i in ('Dir /B /A:-D "Query.*.bin"') do set /a count+=1
why use for /f at all ?

Code: Select all

for %%? in ("Query.*.bin") do set /a count+=1
echo. %count% pattern matches relative to active directory.

palireza
Posts: 6
Joined: 02 Oct 2012 11:20

Re: counting .bin files in a folder

#14 Post by palireza » 11 Oct 2012 14:32

Ed, I tried without token and /f and you were correct, it works without them fine.

Post Reply