wengwashere Posted July 9, 2003 Posted July 9, 2003 hi! 1. how do i get the size of a certain directory? 2. how can i use the filesystem watcher to tell me that a certain directory size has reached a certain limit? Quote
JABE Posted July 9, 2003 Posted July 9, 2003 If by directory size, you mean the sum of all file sizes in a certain directory, you can use Directory.GetFiles() to get all the files w/in a directory, then iterate thru each file to accumulate the FileInfo.Length property. On the FileSystemWatcher, I think what it can monitor is when files are added, deleted, modified, etc. but not "directory size". Quote
wengwashere Posted July 9, 2003 Author Posted July 9, 2003 If by directory size, you mean the sum of all file sizes in a certain directory, you can use Directory.GetFiles() to get all the files w/in a directory, then iterate thru each file to accumulate the FileInfo.Length property. is there any other way than that? like getting the directory size automatically? Quote
*Gurus* Derek Stone Posted July 9, 2003 *Gurus* Posted July 9, 2003 The point is there is no such thing as directory size. Only files have size. Period. Open up Windows Explorer, right click a directory with numerous files in it and select "Properties". Watch as Windows increases the "directory size", as it enumerates each file and requests its size. You'll need to use the method JABE mentioned, above. Quote Posting Guidelines
sschradle Posted November 14, 2003 Posted November 14, 2003 I'm totaly new to VB.Net and am looking to do the same thing. In VB Script it is really straight forward. You use the Scripting.FileSystemObjtect function with a call to GetFolder. Then you can just use objSubFolder.Size and it returns the directory size. Shoot an email if you want an example script. I really thought this would be a fun project in VB.Net, but I am having a hard time with it. Quote
Leaders dynamic_sysop Posted November 17, 2003 Leaders Posted November 17, 2003 you can still add a reference to the Scripting library ( project , add reference , COM ) , then you can get the Size of a directory / folder easily without having to write a Loop. eg: Dim fs As New Scripting.FileSystemObject() Dim folder As Scripting.FolderClass = fs.GetFolder(Environment.GetFolderPath(Environment.SpecialFolder.Personal)) MessageBox.Show(folder.Size) Quote
Reapz Posted November 17, 2003 Posted November 17, 2003 Lol... I got well told off in here once for still using the FSO.:D I've learnt my lesson... This is the way I do it now... Dim MyFolderSize As Double = 0 Dim MyDir As New IO.DirectoryInfo(FOLDER PATH HERE) Dim MyFilesArray As IO.FileSystemInfo() = MyDir.GetFiles If MyFilesArray.Length > 0 Then Dim MyFile As IO.FileInfo For Each MyFile In MyFilesArray MyFolderSize += MyFile.Length Next End If 'To change it to Megabytes MyFolderSize = Math.Round(((MyFolderSize / 1024) / 1024), 2) See I do pay attention. This will only total up the size of the files in the folder not the subfolders though. Quote I'm getting the hang of this now... No really I am!
btoniob Posted December 4, 2003 Posted December 4, 2003 (edited) But since you need the size of the directories in the directories: Function FolderSize(ByVal strFolderPath As String) As Decimal Dim TopDir As New IO.DirectoryInfo(strFolderPath) Dim Dir As IO.DirectoryInfo Dim FilesArray As IO.FileSystemInfo() Dim DirSize As Decimal For Each Dir In TopDir.GetDirectories DirSize += FolderSize(Dir.FullName) Next Dir FilesArray = TopDir.GetFiles If FilesArray.Length > 0 Then Dim MyFile As IO.FileInfo For Each MyFile In FilesArray DirSize += MyFile.Length Next MyFile End If FolderSize = DirSize End Function Thanks for geting me started. Edited December 4, 2003 by btoniob Quote
jmcilhinney Posted July 4, 2005 Posted July 4, 2005 Try this for a succinct implementation: Private Function GetFolderSize(ByVal path As String) 'Add the size of each file in this folder. For Each fileName As String In IO.Directory.GetFiles(path) GetFolderSize += (New IO.FileInfo(fileName)).Length Next 'Add the size of each subfolder. For Each folderName As String In IO.Directory.GetDirectories(path) GetFolderSize += Me.GetFolderSize(folderName) Next End Function Quote
Asharon Posted January 19, 2006 Posted January 19, 2006 (edited) Isn't it somehow strange that the FileSystemWatcher class can check for changes in the size of a directory without knowing how big it really is? Would be nice if some1 explains this. Thx Ash Edited January 19, 2006 by Asharon Quote
Asharon Posted January 23, 2006 Posted January 23, 2006 By the way this is how u do it in c# U call that method with a ne DirectoryInfo(YourPath). E.g.: public class SomeClass() { public SomeClass() { DirectoryInfo aInfo = new DirectoryInfo(_sMyDocPath); getDirectorySize(aInfo); } } private void getDirectorySize(DirectoryInfo diMyDocInfo) { foreach(FileInfo aFileInfo in diMyDocInfo.GetFiles()) { _lTotalSize += aFileInfo.Length; } foreach (DirectoryInfo aDirInfo in diMyDocInfo.GetDirectories()) { getDirectorySize(aDirInfo); } } While _sMyDocPath) is the path of your directory. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.