loop through folders in mapped drive

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TG

    loop through folders in mapped drive

    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.Collecti ons
    Imports System.Windows. Forms
    Imports System.IO
    Imports System.Threadin g
    Imports Excel = Microsoft.Offic e.Interop.Excel
    Imports System.Text

    Public Class Form1
    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click

    ' Declare a variable named theFolderBrowse r of type
    FolderBrowserDi alog.
    Dim theFolderBrowse r As New FolderBrowserDi alog

    ' Set theFolderBrowse r object's Description property to give the user
    instructions.
    theFolderBrowse r.Description = "Please select a folder to get info
    on."

    ' Set theFolderBrowse r object's ShowNewFolder property to false when
    ' the a FolderBrowserDi alog is to be used only for selecting an
    existing folder.
    theFolderBrowse r.ShowNewFolder Button = 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.
    theFolderBrowse r.RootFolder = System.Environm ent.SpecialFold er.Desktop

    ' Default theFolderBrowse rDialog object's SelectedPath property to the
    path to the Desktop folder.
    theFolderBrowse r.SelectedPath =
    My.Computer.Fil eSystem.Special Directories.Des ktop

    ' If the user clicks theFolderBrowse r's OK button..
    If theFolderBrowse r.ShowDialog = Windows.Forms.D ialogResult.OK Then
    ' Set the FolderChoiceTex tBox's Text to theFolderBrowse rDialog's
    ' SelectedPath property.
    TextBox1.Text = theFolderBrowse r.SelectedPath
    End If

    End Sub

    Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button2.Click

    Cursor.Current = System.Windows. Forms.Cursors.W aitCursor

    Dim sum As Long = 0
    Dim filecount As Integer
    Dim folderpath As String
    folderpath = TextBox1.Text
    Dim dInfo As New DirectoryInfo(f olderpath)



    For Each filePath As FileInfo In dInfo.GetFiles( "*.*",
    SearchOption.Al lDirectories)

    sum += filePath.Length
    filecount = dInfo.GetFiles( "*.*", SearchOption.Al lDirectories).L ength

    Next




    End Sub


    Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button3.Click


    '============== =============== =============== ==========
    'export to excel
    '============== =============== =============== ==========

    Cursor.Current = System.Windows. Forms.Cursors.W aitCursor

    Dim xlapp As New Excel.Applicati on
    Dim xlbooks As Excel.Workbooks = xlapp.Workbooks
    Dim xlbook As Excel.Workbook = xlapp.Workbooks .Add(5)

    Dim xlsheet As Excel.Worksheet = xlapp.ActiveShe et

    xlsheet.Columns ("A").AutoFi t()

    '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.Bol d = True


    Dim sum As Long = 0
    Dim filecount As Integer
    Dim folderpath As String
    folderpath = TextBox1.Text
    Dim dInfo As New DirectoryInfo(f olderpath)
    Dim r As Long



    For Each filePath As FileInfo In dInfo.GetFiles( "*.*",
    SearchOption.Al lDirectories)

    sum += filePath.Length
    filecount = dInfo.GetFiles( "*.*", SearchOption.Al lDirectories).L ength

    Next



    r = xlsheet.Range(" A65536").End(Ex cel.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").Colum ns("A").AutoFit ()

    '============== ===============
    'saves spreadsheet in server
    '============== ===============

    xlsheet.Activat e()

    xlapp.Goto(xlsh eet.Range("D1") )

    Dim strFileName As String = "C:\Project s Info Reporting Tool\Projects
    Info Report" & " " & Now().Month & "-" & Now().Day & "-" & Now().Year
    & " " & Date.Now.ToStri ng("hh mm ss") & ".xls"
    Dim blnFileOpen As Boolean = False
    Try
    Dim fileTemp As System.IO.FileS tream =
    System.IO.File. OpenWrite(strFi leName)
    fileTemp.Close( )
    Catch ex As Exception
    blnFileOpen = False
    End Try

    If System.IO.File. Exists(strFileN ame) Then
    System.IO.File. Delete(strFileN ame)
    End If

    xlbook.SaveAs(s trFileName)


    ' The excel is created and opened for insert value. We most close this
    excel using this system
    Dim pro() As Process =
    System.Diagnost ics.Process.Get ProcessesByName ("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.Diagnost ics.Process.Sta rt("C:\Project s Info Reporting Tool\")

    Cursor.Current = System.Windows. Forms.Cursors.D efault


    End Sub

    Private Sub Button4_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button4.Click

    TextBox1.Text = String.Empty
    ListBox1.Text = String.Empty

    End Sub


    End Class
Working...