hi!
I am working with VB 2008. I want to be able to run this program say in N:\ and it will show me in an excel sheet the following:
Folder Path Size(GB) Count of Files
N:\Clients 0.53 308
where clients contains subfolders and files and the size is the total of all those files within each folder and the count is the total within each folder also.
This works fine as it is but i have to select one by one the top level folders and some of them are huge so it takes forever to give me an answer.
1) I would like to see in my spreadsheet the following by only select the network drive n:\
Folder Path Size(GB) Count of Files
N:\Clients 0.53 308
N:\Software 10.7 15430
N:\Billing 0.98 105
2) I would also like to know if this is the faster method.
3) I tried adding a progress bar so that the user can have an idea of how much this will take but i had to remove because it was not working.
4) I would like to see the folder name, size of folder and count of files in the listview.
Your help will be greatly appreciated.
Thanks a lot for your help!
Here is the code:
Imports Microsoft
Imports Microsoft.Win32
Imports Microsoft.Win32.Registry
Imports System.Collections
Imports System.Windows.Forms
Imports System.IO
Imports System.Threading
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Text
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Declare a variable named theFolderBrowser of type FolderBrowserDialog.
Dim theFolderBrowser As New FolderBrowserDialog
' Set theFolderBrowser object's Description property to give the user instructions.
theFolderBrowser.Description = "Please select a folder to get info on."
' Set theFolderBrowser object's ShowNewFolder property to false when
' the a FolderBrowserDialog is to be used only for selecting an existing folder.
theFolderBrowser.ShowNewFolderButton = False
' Optionally set the RootFolder and SelectedPath properties to
' control which folder will be selected when browsing begings
' and to make it the selected folder.
' For this example start browsing in the Desktop folder.
theFolderBrowser.RootFolder = System.Environment.SpecialFolder.Desktop
' Default theFolderBrowserDialog object's SelectedPath property to the path to the Desktop folder.
theFolderBrowser.SelectedPath = My.Computer.FileSystem.SpecialDirectories.Desktop
' If the user clicks theFolderBrowser's OK button..
If theFolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then
' Set the FolderChoiceTextBox's Text to theFolderBrowserDialog's
' SelectedPath property.
TextBox1.Text = theFolderBrowser.SelectedPath
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
Dim sum As Long = 0
Dim filecount As Integer
Dim folderpath As String
folderpath = TextBox1.Text
Dim dInfo As New DirectoryInfo(folderpath)
For Each filePath As FileInfo In dInfo.GetFiles("*.*", SearchOption.AllDirectories)
sum += filePath.Length
filecount = dInfo.GetFiles("*.*", SearchOption.AllDirectories).Length
Next
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'======================================================
'export to excel
'======================================================
Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
Dim xlapp As New Excel.Application
Dim xlbooks As Excel.Workbooks = xlapp.Workbooks
Dim xlbook As Excel.Workbook = xlapp.Workbooks.Add(5)
Dim xlsheet As Excel.Worksheet = xlapp.ActiveSheet
xlsheet.Columns("A

").AutoFit()
'this inserts the sheet title and formats it
xlapp.Workbooks(1).Worksheets("Sheet1").Cells(1, 1) = "Projects Info Report"
xlapp.Workbooks(1).Worksheets("Sheet1").Cells(1, 1).Font.Size = 12
xlapp.Workbooks(1).Worksheets("Sheet1").Cells(1, 1).Font.Bold = True
xlsheet.Range("A3").Formula = "Folder Path:"
xlsheet.Range("B3").Formula = "Size (GB):"
xlsheet.Range("C3").Formula = "Files:"
xlsheet.Range("A3

3").Font.Bold = True
Dim sum As Long = 0
Dim filecount As Integer
Dim folderpath As String
folderpath = TextBox1.Text
Dim dInfo As New DirectoryInfo(folderpath)
Dim r As Long
For Each filePath As FileInfo In dInfo.GetFiles("*.*", SearchOption.AllDirectories)
sum += filePath.Length
filecount = dInfo.GetFiles("*.*", SearchOption.AllDirectories).Length
Next
r = xlsheet.Range("A65536").End(Excel.XlDirection.xlUp).Row + 1
xlapp.Workbooks(1).Worksheets("Sheet1").Cells(r, 1).Formula = folderpath
xlapp.Workbooks(1).Worksheets("Sheet1").Cells(r, 2).Formula = sum / 1024 / 1024 / 1024
xlapp.Workbooks(1).Worksheets("Sheet1").Cells(r, 3).Formula = filecount
xlapp.Workbooks(1).Worksheets("Sheet1").Columns("A

").AutoFit()
'=============================
'saves spreadsheet in server
'=============================
xlsheet.Activate()
xlapp.Goto(xlsheet.Range("D1"))
Dim strFileName As String = "C:\Projects Info Reporting Tool\Projects Info Report" & " " & Now().Month & "-" & Now().Day & "-" & Now().Year & " " & Date.Now.ToString("hh mm ss") & ".xls"
Dim blnFileOpen As Boolean = False
Try
Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
fileTemp.Close()
Catch ex As Exception
blnFileOpen = False
End Try
If System.IO.File.Exists(strFileName) Then
System.IO.File.Delete(strFileName)
End If
xlbook.SaveAs(strFileName)
' The excel is created and opened for insert value. We most close this excel using this system
Dim pro() As Process = System.Diagnostics.Process.GetProcessesByName("EXCEL")
For Each i As Process In pro
i.Kill()
Next
MessageBox.Show("The Projects Info Report has been saved to " & strFileName & ".")
'=========================================================
'opens R folder automatically after report has been saved
'=========================================================
System.Diagnostics.Process.Start("C:\Projects Info Reporting Tool\")
Cursor.Current = System.Windows.Forms.Cursors.Default
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
TextBox1.Text = String.Empty
ListBox1.Text = String.Empty
End Sub
End Class