Reputation: 23
I have a batch file which totals the number of directories:
for /d %%a in (*) do set /a count+=1
Now I need to total the directory names ending with the string )x
, e.g. Mona Lisa (1986)x
I have tried unsuccessfully with:
for /d %%a in (")x") do set /a count+=1
Upvotes: 1
Views: 167
Reputation: 34909
You can let the find
command count the items filtered by the dir
command:
dir /B /A:D "*)x" | find /C /V ""
Prepend or append 2> nul
to the dir
part to suppress error messages in case no such directories exist.
To capture the resulting value and store it in a variable, use a for /F
loop:
for /F %%C in ('2^> nul dir /B /A:D "*)x" ^| find /C /V ""') do set "COUNT=%%C"
echo %COUNT%
Upvotes: 1
Reputation: 38589
Just an alternative, (the difference being that this doesn't ignore some directories, such as hidden ones):
For /F %%A In ('Dir /AD "*)x" 2^>Nul') Do Set "count=%%A"
You should really precede this with Set "count="
Upvotes: 1
Reputation: 4430
for /d %%d in (*^)x) do set /a count+=1
You may first want to check that it finds them correctly:
for /d %%d in (*^)x) do echo "%%~d"
Upvotes: 1