473,378 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

loop through folders in mapped drive

TG
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("EXC EL")
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
Oct 31 '08 #1
0 2138

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: giloosh99 | last post by:
Hello, Im grabbing tables via VB code using visual foxpro ODBC drives. The tables directory is in a mapped network drive. The code works fine and does the job, however if the computer is idle for...
1
by: Z0gS | last post by:
I got this problem for the web application I try to access files on a remote server. string dirs = Directory.GetDirectories(@"E:\vehicles") E drive is a map to a network drive. I get the...
3
by: Bonj | last post by:
I have a service which is a Windows Service, but it is running under a User account - I enter my own credentials on installing. It can access network files by specifying the full UNC path, i.e....
5
by: Nirosh | last post by:
Hi All, Can any one suggest me a best way to do this .. I have a thrid party tool "EXE" that we need to use with our web service to manipulate some complex XML files, which reside in a...
5
by: Marc | last post by:
I am trying to run a web service that has pre-compiled dll's that reference dll's that are on a mapped drive. The web service can not load because it does not see that mapped drive and returns an...
0
by: Manu | last post by:
Hi, To enumerate the Mapped drive in Windows 2000 box, I am using the "Win32_NetworkConnection". I can see the instance of the Mapped drive in local system, when I try to do the same with...
1
by: Jack | last post by:
Hi, My OS is Windows XP. I mapped a drive on another machine running Windows 2000. I set the permission of the mapped drive on the Windows 2000 machine to be "accessible to everyone". On the...
5
by: =?Utf-8?B?SmVycnkgQw==?= | last post by:
I am trying to accesss a Mapped Drive from my asp.net code running on IIS 6.0. I am using a FileUpload control and fileUpload.SaveAs() to save the file to the server. I want to save the file to...
3
Chrisjc
by: Chrisjc | last post by:
So here is the issue at hand. I have several domains across a WAN environment and just recently I started noticing something odd between all my Domain Controllers. Every Site in my company has a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.