Script to count files in a directory and size of the directory

just like right clicking on a directory i get size and number of files, i'm looking for a script that does the job. Can anyone help me?

Thank you.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yo_beeDirector of Information TechnologyCommented:
you can try using ROBOCOPY.
robocopy.exe <Source> <Destination> [<File>[ ...]] [<Options>]
use these swtiches  options /E /LOG:<Logfile location and name>  /L /NP /NDL /NFL
robocopy <Source> <Destination> /E /LOG:<Logfile location and name>  /L /NP /NDL /NFL
This will give you a remote of total number of files & directories w/ total size.
Steve KnightIT ConsultancyCommented:
We could use a for loop to add them all up (not very well with big numbers) or use:

dir /s | find "File(s)" which can then be read with:

@echo off
for /f "tokens=1,3 delims= " %%a in ('dir /s /-c ^| find "File(s)"') do @set files=%%a & set size=%%b
echo There are %files% files totalling %size% bytes which is approx %size:~0,-3% Kb and %size:~0,-6% Mb

PowerShell solution. After you define the function below, you can call it like these:

- One specific folder by its name:

"c:\ee" | FSProperties

The output:
Files Folders Path     Size
----- ------- ----     ----
    8       2 c:\ee 1592592

- Multiple foders under a root folder:

dir c:\ee | FSProperties

The output:

Files Folders Path         Size
----- ------- ----         ----
    1       1 C:\ee\Subfold1    15
    0       1 C:\ee\Subfold2    0
function FSProperties {

        Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true
    [string] $Fullname
process {
    if((get-item -Path $fullname -Force).psiscontainer){
        $size = (New-Object -ComObject Scripting.FileSystemObject).GetFolder($fullname).Size
        $stat = Get-ChildItem -Path $fullname -Force | Group-Object -Property psiscontainer -NoElement
        $files = $dirs = 0
        $stat | ForEach-Object {
            if($ -eq $true){$dirs = $_.count}
            else{$files = $_.count}
        New-Object -TypeName PSObject -Property @{
            Path = $fullname
            Size = $size
            Files = $files
            Folders = $dirs

Open in new window

Cloud as a Security Delivery Platform for MSSPs

Every Managed Security Service Provider (MSSP) needs a platform to deliver effective and efficient security-as-a-service to their customers. Scale, elasticity and profitability are a few of the many features that a Cloud platform offers. View our on-demand webinar to learn more!

Bill PrewIT / Software Engineering ConsultantCommented:
Here's a simple VBS approach, save as a VBS and execute like this:

cscript EE26914554.VBS c:\ee

' Get folder name to process from command line
If (WScript.Arguments.Count > 0) Then
  sDirName = WScript.Arguments(0)
  WScript.Echo "No folder name specified."
End If

' Get a handle to the folder
Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sDirName)

' Initialize counts and sizes
iSize = oFolder.Size
iFiles = 0
iFolders = 0

' Drill down into all subfolders, tallying data
GetInfo oFolder

' Display results
Wscript.Echo oFolder.Path
Wscript.Echo "Size:    " & iSize
Wscript.Echo "Files:   " & iFiles
Wscript.Echo "Folders: " & iFolders

' Recursize routine to gather needed data for all subfolders
Sub GetInfo(f)
   iFiles = iFiles + f.Files.Count
   iFolders = iFolders + f.SubFolders.Count
   For Each sf In f.SubFolders
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hi...For the script all of above are helpful..Further You said you want to execute the script by just right clicking on the folder.. In other words you want to add the script to the right click context menu of each windows folder. If thats the case then this might help.

1. Open regedit by executing regedit.exe from a command shell or RUN dialog box
2. Navigate to "HKEY_CLASSES_ROOT\Directory\shell"
3. Right Click on "Shell" and select "new=>Key"
4. Give any name for Reference.For example "DirStat"
5. Now in right pane you will see a value name "Default".. Double click on it and Put a name which you want to see in Right click menu of folders.. lets use same "DIrStat" .
6. Now right click on the newly created key (in step 3-4) and select new Key.
7. Give the name "Command" as exactly.
8. Click on Command, In right side you will a value Default again.
9. This is the command which will getexecuted when we select our custome menu from right click.
10. Put the full path of the script or batch with paramertes.

like if you want to use the batch file then you should use below as command.
c:\mybatch.bat "%1"

Open in new window

where C:\mybatch.bat is the path of your batch.

if you want to use the VBscript by BillP then use as below.

cscript C:\EE26914554.VBS "%1"

Open in new window

Thank You.
Bill PrewIT / Software Engineering ConsultantCommented:
Of course, if you aren't tied to scripting this, there's a great free util from Microsoft here that does this for you:

mathayus_ITAAuthor Commented:
Steve KnightIT ConsultancyCommented:
don't worry about the batch and powershell solutions that you posted in those zones....
mathayus_ITAAuthor Commented:
@yo_bee: robocopy <Source> <Destination> /E /LOG:<Logfile location and name>  /L /NP /NDL /NFL  works fine, a good solution too.

@subhashchy: I don't need to execute the script by just right clicking on the folder, only a script that executed on a folder gives me file count and size; the .vbs from billprew just do it perfectly. Thank you for the explanation about getting the script in the context menu, it could be very useful.

@soostibi:i didn't try your solution because i was looking for a .bat or .vbs, something that doesn't use PowerShell (i'm don't know powershell very well).

@dragon-it: i didn't understand how to use it :) (i'm really newbie).

@billprew: the accepted solution, simple and perfect for my needs.

Thanks all for the help.
Steve KnightIT ConsultancyCommented:
@dragon-it: i didn't understand how to use it :) (i'm really newbie).

You can always ask if not sure...

Open notepad
copy and paste the code in.
save it as dirsize.cmd or dirsize.bat
run it from cmd prompt or double click in explorer

It shows size of the dir it is in or add cd /d C:\someotherdir

VBScript most likely runs faster anyway mind, though either will run faster a second time due to caching.
mathayus_ITAAuthor Commented:
Sure, i can ask of course.

Thank you for clarification about dragon-it solution
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.