Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > loop through top level folder in mapped drive and give total count of files and total


Reply
 
Thread Tools Display Modes
  #1  
Old 10-28-2008, 09:20 AM
jtammyg jtammyg is offline
Freshman
 
Join Date: Jun 2008
Posts: 35
Default loop through top level folder in mapped drive and give total count of files and total


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("A33").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
Reply With Quote
  #2  
Old 10-28-2008, 02:17 PM
Roger_Wgnr's Avatar
Roger_Wgnr Roger_Wgnr is offline
CodeASaurus Hex

Forum Leader
* Expert *
 
Join Date: Jul 2006
Location: San Antonio TX
Posts: 2,427
Default

Did not have time right now to go over all your code but one thing I did notice that will greatly effect your speed.
Code:
For Each filePath As FileInfo In dInfo.GetFiles("*.*", SearchOption.AllDirectories) sum += filePath.Length filecount = dInfo.GetFiles("*.*", SearchOption.AllDirectories).Length Next
You are recounting the files everytime you check a file. Move the Filecount outside of the loop or since you are looping every file just increment a counter. Each call to dInfo.Getfiles will cause the system to search the directory path all over again. so really what you want to do is only call it once.
Get the results of the Getfiles to a collection and work with it instead of calling Getfiles over and over.
__________________
Code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. ~Martin Golding
The user is a peripheral that types when you issue a read request. ~Peter Williams
MSDN Visual Basic .NET General FAQ
Reply With Quote
  #3  
Old 10-29-2008, 07:56 AM
jtammyg jtammyg is offline
Freshman
 
Join Date: Jun 2008
Posts: 35
Default

Hi Roger!

Thank you for taking the time to answer.

Can you show me how to do that? I don't understand what you mean.

I am new to VB.

Thanks a lot!

Tammy
Reply With Quote
  #4  
Old 10-30-2008, 10:06 AM
Roger_Wgnr's Avatar
Roger_Wgnr Roger_Wgnr is offline
CodeASaurus Hex

Forum Leader
* Expert *
 
Join Date: Jul 2006
Location: San Antonio TX
Posts: 2,427
Default

since you are looping the files anyway just increment a counter.
Code:
filecount = 0 For Each filePath As FileInfo In dInfo.GetFiles("*.*", SearchOption.AllDirectories) sum += filePath.Length filecount += 1 Next
__________________
Code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. ~Martin Golding
The user is a peripheral that types when you issue a read request. ~Peter Williams
MSDN Visual Basic .NET General FAQ
Reply With Quote
  #5  
Old 10-30-2008, 02:11 PM
jtammyg jtammyg is offline
Freshman
 
Join Date: Jun 2008
Posts: 35
Default

okay..I did that...now how do I show the top level folders when selecting the network drive?

thanks a lot!

Tammy
Reply With Quote
  #6  
Old 10-31-2008, 07:34 AM
jtammyg jtammyg is offline
Freshman
 
Join Date: Jun 2008
Posts: 35
Default

anybody else has an idea on how i can achieve this?

Thanks!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->