Solved

Counting total files in a folder including subdirs

Posted on 2014-02-27
8
1,028 Views
Last Modified: 2014-03-10
Hello,

  I was working on a batch script to count all the files inside a folder. The folder includes two level of sub directories. Only the lowest subdirectory contains files of different formats - csv, pdf, txt, doc, docx.

So, Alphabets has 27 subfolders A-Z and Number. Under A, I will have a level of subdirs like Apple, Apollo. Under each of these subdirs there will be the files. I need to count all such files.

Alphapbet
A                                              B                               C - Z                                  number
Apple       Apollo                                                                                                1
a1.pdf     a2.pdf a3.pdf                                                                                     3muskateer.pdf

So total file count is 4.
0
Comment
Question by:LuckyLucks
  • 4
  • 2
  • 2
8 Comments
 
LVL 43

Accepted Solution

by:
Steve Knight earned 400 total points
Comment Utility
You can use a dir command and the find command, so you want just one total, not one per letter?

cd /d C:\alphabets
dir /b/s /a-d

That will give you list off all the filenames, the /a-d excludes subdirectory names.

find /v "" /c will give you a count of all non blank lines

so

@echo off
cd /d c:\alphabets
dir /b /s /a-d | find /v "" /c

To get that into a variable:

@echo off
cd /d c:\alphabets
for /f %%a in ('dir /b /s /a-d ^| find /v "" /c') do set count=%%a

If you want it for multiple directories it can be adjusted etc.

Steve
0
 

Author Comment

by:LuckyLucks
Comment Utility
If my structure is like:


Alphabets

A                           B                    C                          1
Apple Appolo                                                         3muskateers
a1.pdf a2.pdf                             c1.pdf                  3 muskateers.pdf

So a total of 4 files.
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
did you try it?
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Yes, if all you are looking for is a total count the solution Steve presented will do that.  I saw the question, saw his answer, and said "nothing to add here" and moved on...

~bp
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:LuckyLucks
Comment Utility
Is it possible to add a user input parameter so that I can run it like :

> count.bat A:\All

> count.bat B:\
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 100 total points
Comment Utility
Give this a try:

@echo off
if "%~1" EQU "" (
  echo Usage: %~nx0 [directory-name]
) else (
  set DirName=%~1
  if "%DirName:~-1" NEQ "\" set DirName=%DirName%\
  dir /b /s /a-d "%DirName%*.*"| find /v "" /c
)

Open in new window

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
http://scripts.dragon-it.co.uk/links/batch-gui-folder-mk2

If you incorporate the script into above script of mine you can either use the command line, drag a folder onto the icon using explorer, or if not then it uses a windows dialog box to ask you to choose the folder.

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
OK getting a bit silly for a batch file but.... drag folder onto it, type on command line, or select using GUI if not....

@echo off
REM Script from: http://scripts.dragon-it.co.uk/links/batch-gui-folder-mk2
set folder=
if "%~1"=="" (
echo No folder selected.
call :getfolder
) ELSE (
if exist "%~1" (
set folder=%~1
) else (
echo Folder not found or you have passed a filename instead
pause
exit /b
)
)
if "%folder%"=="" exit /b
pushd "%folder%
for /f %%a in ('dir /b /s /a-d ^| find /v "" /c') do set count=%%a

call :MessageBox "There are %count% files under %folder%"

exit /b

:GetFolder
(echo Set objShell = CreateObject^( "Shell.Application" ^)
echo set objFolder = objShell.BrowseForFolder^( 0, "Select a folder", ^&H10^&, ""^)
echo if objFolder is nothing then wscript.quit
echo wscript.echo objFolder.Self.Path)> "%temp%\getpath.vbs"

for /f "tokens=*" %%a in ('cscript //nologo "%temp%\getpath.vbs"') do set folder=%%a

exit /b

:MessageBox
set heading=%~2
set message=%~1
echo msgbox WScript.Arguments(0),0,WScript.Arguments(1) >"%temp%\input.vbs"
START "" Wscript //nologo "%temp%\input.vbs" "%message%" "%heading%"
exit /b

Open in new window

CountFiles.cmd
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now