[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Counting total files in a folder including subdirs

Posted on 2014-02-27
8
Medium Priority
?
1,183 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 1600 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 58

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 58

Assisted Solution

by:Bill Prew
Bill Prew earned 400 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

656 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