Solved

Counting total files in a folder including subdirs

Posted on 2014-02-27
8
1,046 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
ID: 39893235
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
ID: 39893897
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
ID: 39894022
did you try it?
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 39894608
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:LuckyLucks
ID: 39896373
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 52

Assisted Solution

by:Bill Prew
Bill Prew earned 100 total points
ID: 39896502
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
ID: 39896863
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
ID: 39896900
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

22 Experts available now in Live!

Get 1:1 Help Now