Solved

Counting total files in a folder including subdirs

Posted on 2014-02-27
8
1,082 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 54

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 54

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 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