Hey all i have this piece of code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strFileSize As String = ""
Dim di As New IO.DirectoryInfo("C:\")
Try
di.GetFiles("*.*", SearchOption.AllDirectories)
Catch
End Try
Dim aryFi As IO.FileInfo() = di.GetFiles("*.*")
Dim fi As IO.FileInfo
For Each fi In aryFi
strFileSize = (Math.Round(fi.Length / 1024)).ToString()
Debug.Print("File Name: {0}", fi.Name)
'Debug.Print("File Full Name: {0}", fi.FullName)
'Debug.Print("File Size (KB): {0}", strFileSize)
'Debug.Print("File Extension: {0}", fi.Extension)
'Debug.Print("Last Accessed: {0}", fi.LastAccessTime)
Next
End Sub
and it works just fine. However, saying that, i need to find a way to loop through ALL the directory's on the "c" drive and not just the main folder.
Does anyone have any code that can do that?
Thanks!
SOLVED
Imports System.IO
Imports System
Imports System.Collections.Generic
Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim vFolder As String = "c:\"
WalkDirRecursive(vFolder)
End Sub
Private Sub WalkDirRecursive(ByVal vPath As String)
Dim vDirInfo As New System.IO.DirectoryInfo(vPath)
If Not vDirInfo.Exists Then Exit Sub
'get all files' sizes in current path
On Error Resume Next
For Each vFile As String In System.IO.Directory.GetFiles(vDirInfo.FullName)
'do something with this file
Debug.Print(vFile)
Next
'do the same for all subfolders
For Each vSubDir As String In System.IO.Directory.GetDirectories(vDirInfo.FullName)
WalkDirRecursive(vSubDir)
Next
End Sub
Private Sub RecurseDirectories(ByVal di As DirectoryInfo)
Try
For Each d In di.GetDirectories()
ProcessData(d)
RecurseDirectories(d)
Next
Catch
End Try
End Sub
Private Sub ProcessData(ByVal di As IO.DirectoryInfo)
Dim strFileSize As String = ""
Dim fi As IO.FileInfo
Try
di.GetFiles("*.*", SearchOption.AllDirectories)
Catch
End Try
Try
Dim aryFi As IO.FileInfo() = di.GetFiles("*.*")
For Each fi In aryFi
strFileSize = (Math.Round(fi.Length / 1024)).ToString()
Debug.Print("File Name: {0}", fi.Name)
'Debug.Print("File Full Name: {0}", fi.FullName)
'Debug.Print("File Size (KB): {0}", strFileSize)
'Debug.Print("File Extension: {0}", fi.Extension)
'Debug.Print("Last Accessed: {0}", fi.LastAccessTime)
Next
Catch
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim di As New IO.DirectoryInfo("C:\")
RecurseDirectories(di)
End Sub
End Class
David