473,466 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

File Aging Program

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
5 1560
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

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
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
Perfect Robin. I've figured it out and its working fine!

Thanks so much for your help.
Justin

Nov 14 '06 #5
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
6
by: Kiran | last post by:
Hi, I have program, which opens file at the startup and logs error messages to the file, file handle is closed at the end of the program. However if file is deleted in-between, program do not...
1
by: Ben Bloom | last post by:
I want to send some files to the recycle bin after I'm done using them. I'd rather not delete them or make my own aging scheme. How do I go about this? I haven't been able to find a C# example...
6
by: Tom | last post by:
Given: A binary data file of records. Task: Random access using seekg(). I've never found any documentation stating that the offset from begin file marker is safe on a fragmented file. I've...
0
by: Treas4ever | last post by:
Hi, dudes. Do you use any anti wrinkle solutions? I've bought revitol anti aging, but don't know if it is safe or not. Maybe someone has used it? ----------------------------------...
4
by: Ron | last post by:
Hi All, I've got a client/transaction type of database where tblClient is linked to tblTransactions via ClientID. The client table contains all clients from day one. The transaction table...
1
by: robospy | last post by:
Hi What is variable aging? I was asked this question by some friend related to C discussions. Robo
0
by: Filemaxor | last post by:
I have gotten my code to be able to allow people to add new text to a .txt document and able to call up files so the user can see whats in it. The problem i'm having is getting the for loop to work...
1
by: CandK | last post by:
Need help please to count cases from entered date which fall in aging range: <= 0 to 5 days, = 6 to 7 days, = 7 to 10 days and = 10+ days. Worked hard to use combination of DateAdd and DateDiff...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.