mathayus_ITA
asked on
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.
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.
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
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
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
- 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
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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\Directo ry\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.
if you want to use the VBscript by BillP then use as below.
Thank You.
1. Open regedit by executing regedit.exe from a command shell or RUN dialog box
2. Navigate to "HKEY_CLASSES_ROOT\Directo
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"
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"
Thank You.
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
http://technet.microsoft.com/en-us/sysinternals/bb896651.aspx
~bp
ASKER
Perfect!
don't worry about the batch and powershell solutions that you posted in those zones....
ASKER
@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.
@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.
@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.
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.
ASKER
Sure, i can ask of course.
Thank you for clarification about dragon-it solution
Thank you for clarification about dragon-it solution
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