Batch Coount No of Folders

Gigabyte / P35c-ds3r
March 24, 2010 at 07:49:20
Specs: Microsoft Windows XP Professional, 3 GHz / 3582 MB

I have been googling for a batch file that can be used to count the number of folders in a given location, for example "h:\". So far all i can seem to find it ways of counting files but not folders.

Is it possible to count folders? i only need to count root folders and not sub folder, can someone please help?


See More: Batch Coount No of Folders

Report •

March 24, 2010 at 08:00:16
There are two solutions that come to mind:


set count=
for /d %%a in (h:\*) do set /a count+=1
echo %count%


for /f %%a in ('dir /b /ad h:\^|find /c /v "" ') do set count=%%a
echo %count%

Just note that #2 as is will count hidden/system folders while #1 won't. To make #2 do the same it would be "/ad-s-h" rather than "/ad".

Report •

March 24, 2010 at 09:04:12
Hi Judago,

Thank you for your help, two things though, one is when i run the batch the command prompt window opens then vanishes, also is it possible to output the count in a text file in h:\? possibly call the text file counter.txt??

i just used the pause command to paus the CMD windows, excellent... all i need now is to output the count into a textfile please

Thanks again for your assistance.


Report •

March 24, 2010 at 09:12:42
OMG awesome just figured it out

i used the >>d:/counter.txt after the echo %count% and it worked :-)

Mate appreciate the help, batch is awesome im learning every second.

cheers for your help much appreciated :)

Report •

Related Solutions

March 24, 2010 at 17:50:49
Glad to hear you figured it out, however I would suggest redirecting before the echo rather than after:

>> d:\counter.txt echo %count%

The reason for this is because:

echo %count% >> d:\counter.txt

Will leave <number><space> in the text file rather than just <number> and:

echo %count%>> d:\counter.txt

will result in the command processor interpreting the number as stream redirection. See

The main reason for this is because %variables% are treated exactly the same as code in the text file.

Report •

March 24, 2010 at 18:54:01
get coreutils and findutils from GNU win2 and then

find c:\  -type d | wc -l

GNU win32 packages | Gawk

Report •

March 25, 2010 at 04:14:51
You guys a well clever and very very helpful thank you very much for all your input, i also found that if i wanted to append the text file i would use >> but if i wanted to overwrite it i use >. i am enjoying learning about this its very useful.

Here is another question,

I am using my VB app to run the batch file then read the content of the textfile the batch generates. the problem is that the batch takes a while to go through and VB reads the old value from the file.

I used the Del command to delete the old and create a new one, and VB then does a check to see if the file exists.

I was wondering if you guys new of a better way of doing this? can i pass a value from my batch to my VB app? is this is a stupid question?


Report •

March 25, 2010 at 10:19:52
I don't know much VB so I can't really help too much.

I'm fairly sure that it would be possible to use VB to count the folders and avoid the batch script altogether.

I was wondering if you guys new of a better way of doing this? can i pass a value from my batch to my VB app? is this is a stupid question?

If you could get VB to wait and read the return code(I'm fairly sure it will be possible) you could avoid the text file by using:

exit /b %count%

There are a few people that frequent that are fairly good with VB/VBS that may jump in. I am pretty sure they will want to know if its VB(with version number) or VBS......

Report •

March 26, 2010 at 02:11:18
Hey Judago,

Thanks man all you help has been awesome, ghostdog same as mate thank you so much for your help.

I was trying to get this to work in VB for a while problem was that i was having a dumb moment (the usual). I finally got down to it yesterday and figured out how to do it.

But i guess the good thing that came out of this is that i learnt a little bit of batch and also am now very interested in learning more.

Now i am able to get a count of folders in a given directory :-) finally woohoo.


Report •

March 26, 2010 at 10:40:40
fwiw department (water-under-the-bridge division):
this worked (same in both vbasic and vbscript):
'set whatever path here
Set fso = createobject("Scripting.FileSystemObject")
Set Folder = fso.GetFolder(Path1)
for each file in folder.files
msgbox i

Report •

March 28, 2010 at 04:55:18
Hi nbrane, thats for your time, however the code you have given will only count files and not folders (directories). Here is a solution for those who are interested.....This will count all the root folders (not sub or files) in a given path..

Private Sub Command1_Click()
End Sub

Sub ListDirectories()

Dim folName As String
Dim folPath As String
Dim folCounter As Integer
folCouunter = 0

folPath = "J:\"

folName = Dir(folPath, vbDirectory)

Do Until Len(folName) = 0

If GetAttr(folPath & folName) = vbDirectory Then
folCounter = folCounter + 1
End If

folName = Dir

MsgBox folCounter
End Sub

Report •

March 28, 2010 at 15:45:53
good point, i missed the target entirely!
here's another method, folders only:
set folder=fso.getfolder(".\")
for each subdir in folder.subfolders
msgbox i&" "&subdir
what i like about programming in general, almost always plenty of ways to accomplish objective! :)

Report •

March 29, 2010 at 01:54:44
Exactly and i might be a bit sad and wierd but i enjoy finding solutions to every question i am asked or every question i ask... its fun :-) cheers everyone for all your help i have now finished my program :-) woohoooo......


Report •

Ask Question