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

Hi,
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.
mathayus_ITAAsked:
Who is Participating?
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.
http://technet.microsoft.com/en-us/library/cc733145(WS.10).aspx
0
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

Steve
0
soostibiCommented:
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 {

param(
    [Parameter(
        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($_.name -eq $true){$dirs = $_.count}
            else{$files = $_.count}
        }
        New-Object -TypeName PSObject -Property @{
            Path = $fullname
            Size = $size
            Files = $files
            Folders = $dirs
        }
    }
}
}

Open in new window

0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

Bill PrewCommented:
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)
Else
  WScript.Echo "No folder name specified."
  WScript.Quit
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
Wscript.Quit

' 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
      GetInfo(sf)
   Next
End Sub

Open in new window

~bp
0

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
subhashchyCommented:
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.
0
Bill PrewCommented:
Of course, if you aren't tied to scripting this, there's a great free util from Microsoft here that does this for you:

http://technet.microsoft.com/en-us/sysinternals/bb896651.aspx

~bp
0
mathayus_ITAAuthor Commented:
Perfect!
0
Steve KnightIT ConsultancyCommented:
don't worry about the batch and powershell solutions that you posted in those zones....
0
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.
0
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.
0
mathayus_ITAAuthor Commented:
Sure, i can ask of course.

Thank you for clarification about dragon-it solution
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.