Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get size of Folders and subfolders

Status
Not open for further replies.

wasserfa89

Technical User
Apr 15, 2012
24
0
0
CH
Hi all
I use Visual Basic 2010 Express and the Code below
-------------------------------------------------------
Module Module1
Dim fln, result ' Declare Variables
Dim fname As String
' Freespace
' Shows Space Usage of every Folder on the fileserver


Sub Main()
fname = "c:\temp\test.txt"
fln = FileLen(Trim(fname))
result = CInt((fln / 1024) / 1024) & " MB" & "(" & fln & "BYTES)"
MsgBox("Grösse des Files --> " & result)
End Sub

End Module
--------------------------------------------------------
so far so good, Script works fine, but now i need the following:

How can read out recursivly a path for example

Drive D:
and list file Size of every folder recursivly

The idea behind is a little statistic tool which shows the consumption of the disk space on the fileserver

Thanks for your helfp
 
You can use DirectoryInfo to loop through all the files on the drive and get info about each file and directory.


Code:
Imports System.IO

        Dim dDir As DirectoryInfo
        dDir = New DirectoryInfo("D:\")

        For Each fFileSystemInfo In dDir.GetFiles("*.*", SearchOption.AllDirectories)

            'fFileSystemInfo.DirectoryName
            'fFileSystemInfo.Length
            'fFileSystemInfo.Name

        Next
 
ok thanks works fine

and how can i print out the results with a MSGBox or so?

 
I would guess something like:

Code:
MsgBox("Name: " & fFileSystemInfo.DirectoryName & vbcrlf & "Length: " & fFileSystemInfo.Length & vbcrlf & etc. etc. )

Obviously I haven't checked that, but I think you get the gist of the idea. Though instead of using a message box, perhaps a multi-line textbox would be better? then you could scroll through it afterwards and read it all, copy/paste the info, etc.

If memory serves me, something like:
Code:
textbox1.text = textbox1.text & vbcrlf & "Name: " & fFileSystemInfo.DirectoryName & vbcrlf & "Length: " & fFileSystemInfo.Length & vbcrlf & etc. etc.

That'll append new info to the existing contents of the textbox. Or you could just use TextBox.AppnedText instead (I just remembered that).

In fact...

Code:
Private Sub SetText([text] As String)
        ' InvokeRequired compares the thread ID of the
        ' calling thread to the thread ID of the creating thread.
        ' If these threads are different, it returns true and fires, Else not.
        Try
            If Me.txt_folderactivity.InvokeRequired Then
                'Dim d As New SetTextCallback(AddressOf SetText)
                'Me.Invoke(d, New Object() {[text]})
                Me.txt_folderactivity.Invoke(New SetTextCallback(AddressOf SetText), [text])
            Else
                'Me.txt_folderactivity.Text &= [text]
                Me.txt_folderactivity.AppendText([text])
                Me.txt_folderactivity.SelectionStart = txt_folderactivity.Text.Length
                Me.txt_folderactivity.ScrollToCaret()
            End If
        Catch ex As Exception
            Me.txt_folderactivity.AppendText(ex.Message & vbCrLf)
        End Try
    End Sub
Private Sub scrollTextDel()
        'txt_folderactivity.SelectionLength = 0
        txt_folderactivity.SelectionStart = txt_folderactivity.Text.Length
        txt_folderactivity.ScrollToCaret()
    End Sub
 Private Sub scrollText()
        myDel = New scrollTextCallback(AddressOf scrollTextDel)
    End Sub

I used that a few months back and forgot about it. You'd use it something like: SetText("File " & e.FullPath & " has been created.") and calling ScrollText() would make sure that the newest info was always visible by scrolling the multi-line textbox to the bottom. So I would call that after adding line to the box to keep the newest info visible all the time.

Each time you use SetText it puts whatever info/text on a new line in the textbox. I had to figure this out because I was trying to write to a textbox from another thread and was getting invoke errors about it. SetText gets me around the invoke problem. The program this came from watches a folder on the hard drive, and when files of a certain name or type/extension are created, it starts doing things with them. In my case, it watches for files from my TiVo and converts them to MPG and then moves that new file to a different folder. The textbox was named txt_folderactivity.


Hopefully this'll help or maybe get you on the right track or something. If I wasted your time then I apologize.


____________
Buzzwang
"I can fix it!"
(maybe...)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top