Solved

Counting total files in a folder including subdirs

Posted on 2014-02-27
8
1,099 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

 
LVL 55

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
 

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 55

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

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
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 …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

691 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