2011-11-30 2 views
2

일부 파일/폴더 사용 데이터를 수집하려면이 응용 프로그램에서 일하고 있습니다. 목표는 객체에 파일 및 하위 폴더의 크기를 알리는 것입니다. 나무의 모든 지점에서.(Reverse?) 트리 열거 형

내 문제는; 내 객체에 디렉토리 구조를 가져온 후에는 거기에 부분합을 가져 오는 방법에 당황 스럽습니다.

실제로 나무를 만들 때 합계를 추가 할 수 있습니까? 아니면 내가 역순으로 나무를 밟아야합니까 (특히 혼란스럽게 보입니다 ...)

저는 컴퓨터 과학 교육이없는 초보자입니다. Google에 적절한 용어를 알고 있다면 그 답은 분명히있을 것입니다.

내 수업은 당신이 하위 노드를 호출하는 각 노드에있는 모든 파일 크기를 추가 한 후

Imports System.IO 
Public Class Form1 
    Public Class mrFURDirectory 
     Public Property Name As String 
     Public Property FileSize As Long = 0 
     Public Property FileCount As Long = 0 
     Public Property DirSize As Long = 0 
     Public Property DirCount As Long = 0 
     Public Property Parent As mrFURDirectory 
     Public Property Children As New List(Of mrFURDirectory) 
     Public Property PercentOfParent As Short = 0 
     Public Property DirChecked As Boolean = False 

     Public Sub New(ByVal Name As String) 
      Me.Name = Name 
     End Sub 
    End Class 

    Public Class mrBaseFURDirectory 
     Inherits mrFURDirectory 
     Property RootPath As String 

     Sub New(ByVal Name As String, ByVal RootPath As String) 
      MyBase.New(Name = Name) 
      Me.RootPath = RootPath 
     End Sub 

    End Class 

    Dim BaseDirectory As New mrBaseFURDirectory("A:\", "a:\") 'RAM disk full files/folders 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

     'setup IO 
     Dim IOBaseDirectory As IO.DirectoryInfo 
     IOBaseDirectory = New IO.DirectoryInfo(BaseDirectory.RootPath) 

     'go get 'em tiger! 
     GatherChildDirectoryStats(IOBaseDirectory, BaseDirectory) 

     MsgBox("Done.") 
    End Sub 

    Public Sub GatherChildDirectoryStats(ByVal IODirectory As IO.DirectoryInfo, ByVal FURDirectory As mrFURDirectory) 

     'get the file count and total size of files in this directory 
     For Each aFile As IO.FileInfo In IODirectory.GetFiles 
      FURDirectory.FileSize += aFile.Length 
      FURDirectory.FileCount += 1 
     Next 

     For Each Directory As IO.DirectoryInfo In IODirectory.GetDirectories 
      'DirCount likely redundant.. 
      FURDirectory.DirCount += 1 

      'if we're in here, we need another child, make one 
      Dim NewChildDir As mrFURDirectory 
      'attach it to the parent 
      FURDirectory.Children.Add(New mrFURDirectory(Directory.Name)) 
      'reference the child 
      NewChildDir = FURDirectory.Children(FURDirectory.Children.Count - 1) 
      'tell the child who the parent is 
      NewChildDir.Parent = FURDirectory 

      'recurse into sub again 
      GatherChildDirectoryStats(Directory, NewChildDir) 
     Next 

    End Sub 

답변

2

... 혼란의 비트, 그들은 여전히 ​​일부 반 형성 아이디어를 가지고 currnetly 있습니다 똑같이하십시오. 이 호출이 돌아 오면이 추가

'recurse into sub again 
GatherChildDirectoryStats(Directory, NewChildDir) 

'add child sums to this node  
FURDirectory.DirSize += NewChildDir.FileSize + NewChildDir.DirSize 

그래서 마지막 노드가 파일, 수익을 요약하고 합계는 N-1 노드 등 재귀 바위에 추가됩니다!

+1

'FURDirectory.DirSize + = NewChildDir.FileSize + NewChildDir.DirSize' (즉, 현재 값을 바꾸지 않고 현재 새 자식을 현재 값에 추가)입니다. – Chris

+0

@Chris 예, 물론 발견해 주셔서 감사합니다. –

+0

고마워, 그 트릭을. – PuzzledAboutVB

1

거의 다 있습니다. 디렉토리 크기는 하위 디렉토리 크기의 합계에 더한 파일 크기의 합계입니다.

하위 디렉토리 추가를 마친 직후에 총 크기를 구해서 전체 디렉토리 크기에 추가 할 수 있습니다.

1

이동 중에도 할 수 있습니다. 하위 디렉토리를 처리 한 다음 현재 디렉토리에 통계를 추가하기 만하면됩니다.

다른 옵션은 mrFURDirectory 개체에 속성을 추가하여 관련 통계를 모두 가져 오는 것입니다. 이 속성은 현재 mrFURDirectory에있는 통계를보고 관련 stat 속성을 사용하여 모든 자식을 반복하고 추가합니다. 물론 자식은 반복적으로 조회합니다.

정적 스냅 샷이라고 가정합니다. 이러한 결과를 무효화 할 수 있으므로 일단 계산하면 두 번째 계산을 건너 뛸 수 있습니다. 나는 이것이 역순으로 나무를 걷는 것에 대해 이야기했을 때 생각하고있는 것의 라인을 따르고 있다고 의심하지만, 그것을하는 데 훨씬 더 분명하고 직관적 인 방법입니다.

이 경우 후자의 방법은 당신이 그것에 대해 게으름을 피우는 것을 제외하고는 전자의 방법과 거의 같습니다. 첫 번째 방법 (자식 디렉터리 통계를 처리 할 때 추가)은 끝이 아니라 별개로 진행됩니다.