Count Files in each subfolder

Microsoft Windows vista ultimate w/sp1
January 7, 2010 at 15:01:22
Specs: Windows Vista, p4/3gb
Hi. I have about 1000 subfolders in c:\images.
I am looking for a batch or .vbs file that will
count the files in each sub folder. Hopefully
something I can out put to a text file to copy
into Excel and do what I gotta do.

I have tried:

dir c:\images /b /s | find /c /v "::"

but it just gives me a grand total of all files. I
also tried:

@echo off

dir c:\images /ad /b /s y* > mylist.txt
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%f in (mylist.txt)
do (
pushd %%f
for /f "tokens=* delims= " %%a in ('dir/b/a-d')
do (
set /a N+=1
popd
)
)
echo !N!

pause..

This created a list of folders into mylist.txt and
then counted. Sseemed like it would be on
the right track, but 1) it didn't like folders with a
space in the name (I'm sure there is a way
around this). 2) It just gave me a grand total
as well.

I'm hoping to get an output like:
BOOK98, 487
BOOK96, 401
BOOK99, 440

etc. Not sure if this is even possible, but
hoping a brilliant mind can help.

Thanks!


See More: Count Files in each subfolder

Report •


#1
January 7, 2010 at 15:32:55
@echo off > newfile & setLocal enableDELAYedeXpansion

set curr=%CD%

pushd c:\images

for /f "tokens=* delims= " %%a in ('dir/s/b/ad') do (
pushd "%%a"
set N=0
for /f "tokens=* delims= " %%f in ('dir/b/a-d 2^>nul') do (
set /a N+=1
)
if !N! neq 0 (
echo %%a has !N! >> "!curr!\newfile"
)
)


=====================================
Helping others achieve escape felicity

M2


Report •

#2
January 7, 2010 at 15:52:23
Thanks mechanix2go! That worked perfectly!

Report •

#3
January 11, 2010 at 08:10:01
Would it be possible to change batch to not to count dot files?

THA!


Report •

Related Solutions

#4
January 11, 2010 at 21:16:29
Define dot files.


=====================================
Helping others achieve escape felicity

M2


Report •

#5
January 11, 2010 at 23:01:20
i think he might mean "no extensions" if so:
for /f "tokens=* delims= " %%f in ('dir/b/a-d 2^>nul') do (

to this:
for /f "tokens=* delims= " %%f in ('dir *. /b/a-d 2^>nul') do (

only reason i am intruding, you beat me to the punch. I had
a more primitive version, but it could be defeated by files
name "Directory" or "Directory of", "Files(s)" etc.

@echo off & setlocal enabledelayedexpansion
for /f "tokens=1-2* delims=F " %%a in ('dir /s ^| findstr "Directory of File(s)"') do (
if "%%a" equ "Directory" (
set line=DIRECTORY:%%c
) else (
if "!line!" neq "" >>logfile echo !line! %%a
set line=
)
)


Report •

#6
January 13, 2010 at 11:40:22
I am sorry for confusion, what I meant was invisible files, file name has a dot infront.

Report •

#7
January 13, 2010 at 21:13:23
A file with a dot as firat char is one thing.

An invisible[sic] file is another.

One has little to do with the other.


=====================================
Helping others achieve escape felicity

M2


Report •

#8
January 13, 2010 at 23:14:48
Perhaps the OP is used to linux/unix where hidden files start with a dot rather than it being a file system attribute(or at least that is my understanding).

In any case to exclude hidden files use the dir "/a-h" switch, since I believe M2's code already used "/a-d" it would be changed to "/a-d-h".

On a side not xp won't let me rename a file to start with a dot from the gui, but allows it from the cli....


Batch Variable how to


Report •

#9
January 14, 2010 at 08:39:17
thank you for taking your time to respond to my question. I
have added "/a-d-h" switch, but it still counts invisible files.
The reason I am asking is I have 85% Mac users upload to
web portal, and they upload resource files along with data files.
Resource files are . in front and I don't want to count them.

I am sorry for not being specific.


Report •

#10
January 14, 2010 at 09:47:54
Perhaps:

in (' dir/b/a-d-h 2^>nul^|findstr /v /b /l "." ') do

Edit:

You still say they are invisible, perhaps they have "read only" or system attributes?

if so it's "/a-r" to exclude read only files and "/a-s" for system files.


Batch Variable how to


Report •

#11
January 14, 2010 at 11:54:19
or:
for /f "tokens=* delims= " %%f in ('dir/b/a-d 2^>nul') do (
set xx=%%f
set xx=!xx:~0,1!
if "!xx!" neq "." set /a N+=1
)

(shouldn't there have been a "popd" somewhere in the code?)


Report •

#12
January 14, 2010 at 12:53:35
"(shouldn't there have been a "popd" somewhere in the code?)"

Nope. It pops when the bat ends.
===================================
We still don't know which files to count.

LOL


=====================================
Helping others achieve escape felicity

M2


Report •

#13
January 14, 2010 at 13:03:59
in (' dir/b/a-d-h 2^>nul^|findstr /v /b /l "." ') do
worked!!

Thank you for all your hardwork, and again I am sorry for not being specific at first.


Report •

#14
January 14, 2010 at 14:48:31
@M2: I think you do need a POPD somewhere near the end.
Ending a batch file doesn't automatically popd. Perhaps you
were thinking of SETLOCAL, which indeed performs an
ENDLOCAL implicitly.

@everyone who's confused: I think the description of these
files as "invisible" means they were invisible on the Mac,
where they originally came from, because the Mac is Unix-
based and the ls command by default does not list files
beginning with a dot. As we all know, they are treated just like
any other file on Windows, and are not invisible at all.

Bit of background: MacOS 9 and earlier had resource forks, which are a bit like Windows alternative data streams. MacOS X doesn't have resource forks, because files are Unix-like, so instead the resource forks are in fact separate files, beginning with a dot to make them invisible. Some utilities may be able to transfer resource forks to Windows as alternative data streams in the base file, which would make counting much simpler.


Report •

#15
January 14, 2010 at 17:04:25
Hi klint,

Good point. I did see a case where it failed to pop but cannot now recall or reproduce it. I'll be on the lookout.

And you're right about the .bla files inm unix. Like the good ol' .redirect.


=====================================
Helping others achieve escape felicity

M2


Report •

#16
January 14, 2010 at 20:28:27
Klint, you left me in the dust way back there... (starting with the "bit of background)nice job clarifying, though, for those who understand the concepts. I'm clueless when it comes to Mac/Apple environments.

Report •


Ask Question