You have your asterisk in the wrong place. I think you intended a*.txt
. But that will match any text file whose name begins with "a". It does not limit the results to text files that start with "a", followed by a number.
You can pipe the results of your DIR command to FINDSTR and use a regular expression to be more specific. The FINDSTR regex support is primitive, but it often gets the job done.
I'm going to assume you want to match names like "a1.txt", "a143.txt", but you don't want to match files like "a1b.txt" or "aba1.txt". If I got that wrong then you need to change the regex expression to match your requirements.
This regex \\a[0-9][0-9]*\.txt$
works as follows:
\\
is an escaped backslash, matching the last backslash before the file name
a
matches itself of course
[0-9]
matches a single digit (there must be at least 1)
[0-9]*
matches 0 or more additional digits
\.txt$
escapes the dot and matches the ".txt" extension. The $ matches the end of the string - it will not match if additional characters follow.
The last thing to do is pipe the result of FINDSTR to FIND to let it count the number of files for you. FIND /C /V ""
matches any line and the /C option gives the count of matching lines. It is more efficient than incrementing the counter in your loop.
@echo off
setlocal
set /a count=0
for /F %%N in ('dir/s/b/a-d "C:\Users\xyz\desktop\Project\a*.txt"^|findstr /ric:"\\a[0-9][0-9]*\.txt$"^|find /c /v ""') do set count=%%N
echo count=%count%