473,799 Members | 3,006 Online
Bytes | Software Development & Data Engineering Community
+ 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.EventArg s) Handles btnSearch.Click
lstFilesFound.I tems.Clear()

lstFilesFound.I tems.Add(String .Format(fmtStr, "DIRECTORY" ,
"1995", "1996", "1997", "1998", "1999", "2000", "Total"))
txtFile.Enabled = False
cboDirectory.En abled = False
btnSearch.Text = "Searching. .."
Me.Cursor = Cursors.WaitCur sor
Application.DoE vents()
DirSearch(cboDi rectory.Text)
btnSearch.Text = "Search"
Me.Cursor = Cursors.Default
txtFile.Enabled = True
cboDirectory.En abled = True
End Sub

Sub DirSearch(ByVal sDir As String)
Dim rootLevelFolder (), rootFolder, rootLevelFolder New,
rootLevelFolder Old, 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.GetDi rectories(sDir)
MsgBox(d)
c95 = 0
c96 = 0
c97 = 0
c98 = 0
c99 = 0
c00 = 0
totcount = 0
For Each f In Directory.GetFi les(d)

Dim sb As New System.Text.Str ingBuilder(d)
Dim file As New FileInfo(f)
sb.Replace("C:\ ", "/")
sb.Replace("\", "/")
d = sb.ToString
lastModify = file.LastWriteT ime
Select Case True
Case InStr(lastModif y, "1995")
c95 += 1
totcount += 1

Case InStr(lastModif y, "1996")
c96 += 1
totcount += 1

Case InStr(lastModif y, "1997")
c97 += 1
totcount += 1

Case InStr(lastModif y, "1998")
c98 += 1
totcount += 1

Case InStr(lastModif y, "1999")
c99 += 1
totcount += 1

Case InStr(lastModif y, "2000")
c00 += 1
totcount += 1

End Select

Next
lstFilesFound.I tems.Add(String .Format(fmtStr, d, c95,
c96, c97, c98, c99, c00, totcount))
DirSearch(d)
Next
Catch excpt As System.Exceptio n
Debug.WriteLine (excpt.Message)
End Try
End Sub

Private Sub Form1_Load(ByVa l sender As System.Object, _
ByVal e As System.EventArg s) Handles MyBase.Load
Dim s As String
cboDirectory.It ems.Clear()
For Each s In Directory.GetLo gicalDrives()
cboDirectory.It ems.Add(s)
Next
cboDirectory.Te xt = "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 1577
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
SystemVolumeInf ormation 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.GetFi les(path)
TestCount += 1
Next
Console.WriteLi ne(String.Forma t("In {0}, {1} files found.", _
path, TestCount))
For Each dirName As String In Directory.GetDi rectories(path)
TestCount = 0
Call NextLevel(dirNa me, TestCount)
Console.WriteLi ne(String.Forma t("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.WriteL ine(String.Form at("level = {0}, dir = {1}", _
' level, path.ToString))
For Each FileFound As String In Directory.GetFi les(path)
CountOfFiles += 1
Next
'now process the subfolders by calling this routine for each folder
For Each dir As String In Directory.GetDi rectories(path)
Call NextLevel(dir, CountOfFiles, level)
Next
level -= 1
End Sub

Robin S.
"Justin Fancy" <ju*********@gm ail.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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.EventArg s) Handles btnSearch.Click
lstFilesFound.I tems.Clear()

lstFilesFound.I tems.Add(String .Format(fmtStr, "DIRECTORY" ,
"1995", "1996", "1997", "1998", "1999", "2000", "Total"))
txtFile.Enabled = False
cboDirectory.En abled = False
btnSearch.Text = "Searching. .."
Me.Cursor = Cursors.WaitCur sor
Application.DoE vents()
DirSearch(cboDi rectory.Text)
btnSearch.Text = "Search"
Me.Cursor = Cursors.Default
txtFile.Enabled = True
cboDirectory.En abled = True
End Sub

Sub DirSearch(ByVal sDir As String)
Dim rootLevelFolder (), rootFolder, rootLevelFolder New,
rootLevelFolder Old, 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.GetDi rectories(sDir)
MsgBox(d)
c95 = 0
c96 = 0
c97 = 0
c98 = 0
c99 = 0
c00 = 0
totcount = 0
For Each f In Directory.GetFi les(d)

Dim sb As New System.Text.Str ingBuilder(d)
Dim file As New FileInfo(f)
sb.Replace("C:\ ", "/")
sb.Replace("\", "/")
d = sb.ToString
lastModify = file.LastWriteT ime
Select Case True
Case InStr(lastModif y, "1995")
c95 += 1
totcount += 1

Case InStr(lastModif y, "1996")
c96 += 1
totcount += 1

Case InStr(lastModif y, "1997")
c97 += 1
totcount += 1

Case InStr(lastModif y, "1998")
c98 += 1
totcount += 1

Case InStr(lastModif y, "1999")
c99 += 1
totcount += 1

Case InStr(lastModif y, "2000")
c00 += 1
totcount += 1

End Select

Next
lstFilesFound.I tems.Add(String .Format(fmtStr, d, c95,
c96, c97, c98, c99, c00, totcount))
DirSearch(d)
Next
Catch excpt As System.Exceptio n
Debug.WriteLine (excpt.Message)
End Try
End Sub

Private Sub Form1_Load(ByVa l sender As System.Object, _
ByVal e As System.EventArg s) Handles MyBase.Load
Dim s As String
cboDirectory.It ems.Clear()
For Each s In Directory.GetLo gicalDrives()
cboDirectory.It ems.Add(s)
Next
cboDirectory.Te xt = "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 IncrementCountO fFiles, 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
IncrementCountO fFiles(TestCoun t, path)
Console.WriteLi ne(String.Forma t("In {0}, {1} files found.", _
path, TestCount))
For Each dirName As String In Directory.GetDi rectories(path)
TestCount = 0
Call NextLevel(dirNa me, TestCount)
Console.WriteLi ne(String.Forma t("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.WriteL ine(String.Form at("level = {0}, dir = {1}", _
' level, path.ToString))
IncrementCountO fFiles(CountOfF iles, path)
'now process the subfolders by calling this routine for each folder
For Each dir As String In Directory.GetDi rectories(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.GetFi les(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.Le ngth
if myYears(i) = YearInteger Then
CountOfFiles(i) += 1
End If
Next
Next
End Sub

Does that make sense?
Robin S.
"Justin Fancy" <ju*********@gm ail.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.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*********@gm ail.comwrote in message
news:11******** *************@f 16g2000cwb.goog legroups.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
3208
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 indeed also true for our language of choice, Python. Its file type allows some extraordinary convenient access like: for line in open("blah"): handle_line(line)
6
5305
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 report any error while writing to the open file handle. On Windows, file shows-up again in explorer and automatically deleted finally when program ends. On Unix, same thing happens if file is on nfs mounted drive, but in this case, actual file is...
1
1823
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 out there. Thanks. -- to reply, remove .s.p.a.m. from email
6
1748
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 observed how binary files are buffered in the past. But I have never tested against a fragmented file. Perhaps I am paranoid or whatever. Maybe fragmentation is no worse than changing tracks on the drive?
0
1200
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? ---------------------------------- http://community.ihostasp.net ASP.NET Developer Community
4
2146
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 contains all transactions from day one (includes any purchases, labor charges, payments, adjustments) The aging report I developed works fine, except... For any client that owes money, it lists ALL the transactions for that client. Or, it lists...
1
1824
by: robospy | last post by:
Hi What is variable aging? I was asked this question by some friend related to C discussions. Robo
0
1977
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 correctly so that i can allow certain indexes to be removed without completely deleting everything else. This is what i have so far. Code is below It's in the e part of the code and I have //remed the location of the for loop containg the...
1
2164
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 formulas but it either pulls all the records or I get error messages. It's all probably simple to do and I may have been close but just frustrated now. Need for report ... please help. Thanks.
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10490
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10260
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10243
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9078
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4146
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 we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.