By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,168 Members | 2,599 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,168 IT Pros & Developers. It's quick & easy.

File Aging Program

P: n/a
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

Nov 8 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
You need a top level that gets a count of those files, and then
goes through the subfolders and gets the count of files in each
subfolders&its subfolders. The second routine needs to be
recursive. Try something like the following. You'll probably
want to catch the exception that gets thrown when it hits the
SystemVolumeInformation because you won't have access to that
folder.

Public Sub LookThruFolders(ByVal path As String)
Dim TestCount As Integer
'First, get the number of files in this directory
TestCount = 0
For Each FileFound As String In Directory.GetFiles(path)
TestCount += 1
Next
Console.WriteLine(String.Format("In {0}, {1} files found.", _
path, TestCount))
For Each dirName As String In Directory.GetDirectories(path)
TestCount = 0
Call NextLevel(dirName, TestCount)
Console.WriteLine(String.Format("In {0}, {1} files found.", _
dirName, TestCount))
Next
End Sub

Public Sub NextLevel(ByVal path As String, ByRef CountOfFiles As Integer, _
Optional ByVal level As Integer = 0)
'count all the files in this folder and in all the folders below it
'get number of files in this folder, and then
' go through this guy's subfolders
level += 1 'shows how many levels down you are
'uncomment this to see all the folders
'Console.WriteLine(String.Format("level = {0}, dir = {1}", _
' level, path.ToString))
For Each FileFound As String In Directory.GetFiles(path)
CountOfFiles += 1
Next
'now process the subfolders by calling this routine for each folder
For Each dir As String In Directory.GetDirectories(path)
Call NextLevel(dir, CountOfFiles, level)
Next
level -= 1
End Sub

Robin S.
"Justin Fancy" <ju*********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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

Nov 9 '06 #2

P: n/a

So where do you suggest I put the select statements I have proposed.
Because i am using the FileInfo class to dig up the lastModified
attribute of each file in a particular. I want to loop through each
file in a root level directory (including all subfolders), and count up
the number of files that were last accessed in 1995, 96, and so on all
in seperate categories. Then, when all the files are looped through,
break and go to the next directory.

Justin

Nov 10 '06 #3

P: n/a
If you mean how do you get a count for each year?
First, I'd split out the routine that counts the files.

Then I would replace TestCount with an array of
counts, where the 1st entry was for the first year,
the 2nd was for the 2nd year, and so on.

In IncrementCountOfFiles, you check the year
and add it to the appropriate array entry.

I'm anal-retentive, so I would probably set up
an array that had the years in it that
corresponded to each entry in the array of
counts, so I wouldn't have the years hardcoded.

If I put this in production, I would look up
how to make the years date field; I'll leave
that for you to do if you so choose. If you
use an integer field, you'll have to convert
year to integer when you do the compare.

Dim myYears(10) as Integer
'Fill this somewhere with your seed values
For i as Integer = 1995 to 2006
myYears(i) = i
Next i
Dim myCounts(10) as integer

'**Change all places that use TestCount
'** to use myCounts instead. In fact, you
'** can put the definition of myCounts
'** inside this routine, or just change TestCount to TestCount(10).
'** The loop to fill myYears could go
'** inside LookThruFolders, too.
'**
Public Sub LookThruFolders(ByVal path As String)
Dim TestCount As Integer
'First, get the number of files in *this* directory
TestCount = 0
IncrementCountOfFiles(TestCount, path)
Console.WriteLine(String.Format("In {0}, {1} files found.", _
path, TestCount))
For Each dirName As String In Directory.GetDirectories(path)
TestCount = 0
Call NextLevel(dirName, TestCount)
Console.WriteLine(String.Format("In {0}, {1} files found.", _
dirName, TestCount))
Next
End Sub

'**Replace CountOfFiles with an array of integers.
Public Sub NextLevel(ByVal path As String, ByRef CountOfFiles As Integer, _
Optional ByVal level As Integer = 0)
'count all the files in this folder and in all the folders below it
'get number of files in this folder, and then
' go through this guy's subfolders
level += 1 'shows how many levels down you are
'uncomment this to see all the folders
'Console.WriteLine(String.Format("level = {0}, dir = {1}", _
' level, path.ToString))
IncrementCountOfFiles(CountOfFiles, path)
'now process the subfolders by calling this routine for each folder
For Each dir As String In Directory.GetDirectories(path)
Call NextLevel(dir, CountOfFiles, level)
Next
level -= 1
End Sub

'**Replace CountOfFiles with an array of integers.
Private Sub (ByRef CountOfFiles as Integer, ByVal path as String)
For Each FileFound As String In Directory.GetFiles(path)
'**Here, add logical to get the year.
'Then you can look thru the array to find the matching year
' and increment the appropriate pointer.
For i as Integer = 0 to CountOfFiles.Length
if myYears(i) = YearInteger Then
CountOfFiles(i) += 1
End If
Next
Next
End Sub

Does that make sense?
Robin S.
"Justin Fancy" <ju*********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
>
So where do you suggest I put the select statements I have proposed.
Because i am using the FileInfo class to dig up the lastModified
attribute of each file in a particular. I want to loop through each
file in a root level directory (including all subfolders), and count up
the number of files that were last accessed in 1995, 96, and so on all
in seperate categories. Then, when all the files are looped through,
break and go to the next directory.

Justin

Nov 10 '06 #4

P: n/a
Perfect Robin. I've figured it out and its working fine!

Thanks so much for your help.
Justin

Nov 14 '06 #5

P: n/a
Great; I'm glad I could help.
Robin S.

"Justin Fancy" <ju*********@gmail.comwrote in message
news:11*********************@f16g2000cwb.googlegro ups.com...
Perfect Robin. I've figured it out and its working fine!

Thanks so much for your help.
Justin

Nov 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.