J
Justin Fancy
Hi Everyone,
I am developing a file aging program that will eventually report on
every folder that is in the root directory. I have a list of requested
years, and counters set up to count every file within every subfolder
to add up all the files from the specified year. I have the
functionality working, but not quite. With the following code, the end
result includes folders within subfolders. For example, instead of
having root/folder1/ and the next line root/folder1/subfolder, i need
root/folder1, and count including all the folders inside of folder1. So
subfolder will be added in to the count of folder1.
Here's the code:
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Dim fmtStr As String = "{0,3} {1,-7} {2,-7} {3, -7} {4, -7} {5, -7}
{6,-7} {7,-7}"
Private Sub btnSearch_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSearch.Click
lstFilesFound.Items.Clear()
lstFilesFound.Items.Add(String.Format(fmtStr, "DIRECTORY",
"1995", "1996", "1997", "1998", "1999", "2000", "Total"))
txtFile.Enabled = False
cboDirectory.Enabled = False
btnSearch.Text = "Searching..."
Me.Cursor = Cursors.WaitCursor
Application.DoEvents()
DirSearch(cboDirectory.Text)
btnSearch.Text = "Search"
Me.Cursor = Cursors.Default
txtFile.Enabled = True
cboDirectory.Enabled = True
End Sub
Sub DirSearch(ByVal sDir As String)
Dim rootLevelFolder(), rootFolder, rootLevelFolderNew,
rootLevelFolderOld, bottomLevel(), bottomLevel2() As String
Dim c95, c96, c97, c98, c99, c00, totcount As Integer
Dim d As String
Dim f As String
Dim lastModify As String
Try
For Each d In Directory.GetDirectories(sDir)
MsgBox(d)
c95 = 0
c96 = 0
c97 = 0
c98 = 0
c99 = 0
c00 = 0
totcount = 0
For Each f In Directory.GetFiles(d)
Dim sb As New System.Text.StringBuilder(d)
Dim file As New FileInfo(f)
sb.Replace("C:\", "/")
sb.Replace("\", "/")
d = sb.ToString
lastModify = file.LastWriteTime
Select Case True
Case InStr(lastModify, "1995")
c95 += 1
totcount += 1
Case InStr(lastModify, "1996")
c96 += 1
totcount += 1
Case InStr(lastModify, "1997")
c97 += 1
totcount += 1
Case InStr(lastModify, "1998")
c98 += 1
totcount += 1
Case InStr(lastModify, "1999")
c99 += 1
totcount += 1
Case InStr(lastModify, "2000")
c00 += 1
totcount += 1
End Select
Next
lstFilesFound.Items.Add(String.Format(fmtStr, d, c95,
c96, c97, c98, c99, c00, totcount))
DirSearch(d)
Next
Catch excpt As System.Exception
Debug.WriteLine(excpt.Message)
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim s As String
cboDirectory.Items.Clear()
For Each s In Directory.GetLogicalDrives()
cboDirectory.Items.Add(s)
Next
cboDirectory.Text = "C:\"
End Sub
End Class
----------------------------------------------------------------------------------------------------------
The output is something like this:
DIRECTORY 1995 1996 1997
1998 1999 2000 total
C:/My Documents 0 1 3
4 6 7 21
C:/My Documents/My Music 89 5 0 7
8 10 119
C:/My Documents.My Videos 0 5 6 7
8 4 30
C:/Windows 90 6 7
9 7 5 124
------------------------------------------------------------------------------------------------------------
What I would prefer is that My Music and My Videos get added into the
My Documents category, instead being its only seperate count. The same
goes with everything else under My Documents. Its very hard to explain,
but I think you may understand.
Any Suggestions??
Justin
I am developing a file aging program that will eventually report on
every folder that is in the root directory. I have a list of requested
years, and counters set up to count every file within every subfolder
to add up all the files from the specified year. I have the
functionality working, but not quite. With the following code, the end
result includes folders within subfolders. For example, instead of
having root/folder1/ and the next line root/folder1/subfolder, i need
root/folder1, and count including all the folders inside of folder1. So
subfolder will be added in to the count of folder1.
Here's the code:
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Dim fmtStr As String = "{0,3} {1,-7} {2,-7} {3, -7} {4, -7} {5, -7}
{6,-7} {7,-7}"
Private Sub btnSearch_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSearch.Click
lstFilesFound.Items.Clear()
lstFilesFound.Items.Add(String.Format(fmtStr, "DIRECTORY",
"1995", "1996", "1997", "1998", "1999", "2000", "Total"))
txtFile.Enabled = False
cboDirectory.Enabled = False
btnSearch.Text = "Searching..."
Me.Cursor = Cursors.WaitCursor
Application.DoEvents()
DirSearch(cboDirectory.Text)
btnSearch.Text = "Search"
Me.Cursor = Cursors.Default
txtFile.Enabled = True
cboDirectory.Enabled = True
End Sub
Sub DirSearch(ByVal sDir As String)
Dim rootLevelFolder(), rootFolder, rootLevelFolderNew,
rootLevelFolderOld, bottomLevel(), bottomLevel2() As String
Dim c95, c96, c97, c98, c99, c00, totcount As Integer
Dim d As String
Dim f As String
Dim lastModify As String
Try
For Each d In Directory.GetDirectories(sDir)
MsgBox(d)
c95 = 0
c96 = 0
c97 = 0
c98 = 0
c99 = 0
c00 = 0
totcount = 0
For Each f In Directory.GetFiles(d)
Dim sb As New System.Text.StringBuilder(d)
Dim file As New FileInfo(f)
sb.Replace("C:\", "/")
sb.Replace("\", "/")
d = sb.ToString
lastModify = file.LastWriteTime
Select Case True
Case InStr(lastModify, "1995")
c95 += 1
totcount += 1
Case InStr(lastModify, "1996")
c96 += 1
totcount += 1
Case InStr(lastModify, "1997")
c97 += 1
totcount += 1
Case InStr(lastModify, "1998")
c98 += 1
totcount += 1
Case InStr(lastModify, "1999")
c99 += 1
totcount += 1
Case InStr(lastModify, "2000")
c00 += 1
totcount += 1
End Select
Next
lstFilesFound.Items.Add(String.Format(fmtStr, d, c95,
c96, c97, c98, c99, c00, totcount))
DirSearch(d)
Next
Catch excpt As System.Exception
Debug.WriteLine(excpt.Message)
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim s As String
cboDirectory.Items.Clear()
For Each s In Directory.GetLogicalDrives()
cboDirectory.Items.Add(s)
Next
cboDirectory.Text = "C:\"
End Sub
End Class
----------------------------------------------------------------------------------------------------------
The output is something like this:
DIRECTORY 1995 1996 1997
1998 1999 2000 total
C:/My Documents 0 1 3
4 6 7 21
C:/My Documents/My Music 89 5 0 7
8 10 119
C:/My Documents.My Videos 0 5 6 7
8 4 30
C:/Windows 90 6 7
9 7 5 124
------------------------------------------------------------------------------------------------------------
What I would prefer is that My Music and My Videos get added into the
My Documents category, instead being its only seperate count. The same
goes with everything else under My Documents. Its very hard to explain,
but I think you may understand.
Any Suggestions??
Justin