Get files and count based on modified date

I have pdf files in my Print folder "c:\Process\Print"

The PDF files show below

File name                               Modified date

ABC10-10-20130613              9/13/2013
ABC11-10-20130613              9/13/2013
ABC12-10-20130613              9/13/2013
ABC13-10-20130613              9/13/2013
ABC14-10-20130613              9/13/2013
ABC15-10-20130613              9/13/2013
ABC10-10-20130613              713/2013
ABC11-10-20130613              7/01/2013
ABC12-10-20130613              7/01/2013
ABC13-10-20130613              7/01/2013
ABC14-10-20130613              7/01/2013
ABC15-10-20130613              7/01/2013

I'd like to get the count of the items dated 9/13/2013
although the files have same name.
What file manipulation code is needed to get it?
zachvaldezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zachvaldezAuthor Commented:
It will be the latest date in the folder for the items that the count will be done.
0
Jacques Bourgeois (James Burger)PresidentCommented:
A question of my own.

How can you have files with the same name in the same folder?

This being said:

Dim count As Integer
Dim lastDate As Date
Dim folder As New IO.DirectoryInfo("c:\Process\Print")

For Each file As IO.FileInfo In folder.GetFiles
	If file.LastWriteTime.Date = lastDate Then
		count += 1
	Else
		If file.LastWriteTime.Date > lastDate Then
			lastDate = file.LastWriteTime.Date
			count = 1
		End If
	End If
Next
MessageBox.Show(count.ToString & " files")

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zachvaldezAuthor Commented:
YOur right!
should be
ABC10-10-20130613              9/13/2013
ABC11-10-20130613              9/13/2013
ABC12-10-20130613              9/13/2013
ABC13-10-20130613              9/13/2013
ABC14-10-20130613              9/13/2013
ABC15-10-20130613              9/13/2013
ABC10-10-20130513              713/2013
ABC11-10-20130513              7/01/2013
ABC12-10-20130513              7/01/2013
ABC13-10-20130513              7/01/2013
ABC14-10-20130513              7/01/2013
ABC15-10-20130513              7/01/2013
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Jacques Bourgeois (James Burger)PresidentCommented:
And my code will work anyway...
0
Mel-Leo RosalCommented:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim sFolder As String = "c:\Process\Print" 'CHANGE THIS TO YOUR FOLDER
        Dim sDate As String = "9/13/2013" 'CHANGE THIS TO WHATEVER DATE YOU WANT

        Dim iCount As Integer = 0
        Dim DI As New IO.DirectoryInfo(sFolder)
        For Each FI As IO.FileInfo In DI.GetFiles

            Dim strTemp() As String = FI.LastWriteTime.ToString.Split(" ")
            If strTemp(0) = sDate Then iCount += 1
        Next

        MsgBox(iCount & " files found edited on " & sDate)
End Sub

Open in new window


Try this. Just edit sDate and sFolder to your liking. xD
0
Jacques Bourgeois (James Burger)PresidentCommented:
@mleo1

It will be the latest date in the folder. Simply setting the sdate variable is not sufficient. That is why my code does some tricks with the lastDate variable. It must count only the files that were on the last date in the folder, not on a specific hardcoded date.

Also, working with date As String is almost always a bad idea. The format of the date can change from one computer to another. If you set the date as "9/13/2013", it won't work on my computer, because my Control Panel returns "2013/9/13" when you call LastWriteTime.ToString.

In code, dates should always be treated as Date, not as String. This is not only more precise, because the Date type is independent of the Control Panel, but also faster, because converting from Date to String all the time is time consuming.  If you have a specific date to specify in code, you should do it in a Date variable with #9/13/2013#. A date between ## is always month-day-year, while a date between "" will be interpreted by the Control Panel settings and will end up being different things on different computers.

Also, creating an array to split the date and time is overkill. The framework has almost all the methods and properties that you might need. They are almost always faster than anything you might write. LastWriteTime.Date does it without having to create an array in which you use only one element.
0
Mel-Leo RosalCommented:
@JamesBurger

I just wanted to share maybe he needs specifics.

I thought it always is like that format, lol. I googled and found out you can change the format thru Region and Language options (Never tried until now, I always used the default), xD. New at .NET still migrating from VB6. Thanks, I learned alot.

Then
        Dim sFolder As String = "c:\Process\Print" 'CHANGE THIS TO YOUR FOLDER
        Dim dDate As Date = #9/13/2013# 'CHANGE THIS TO WHATEVER DATE YOU WANT

        Dim iCount As Integer = 0
        Dim DI As New IO.DirectoryInfo(sFolder)
        For Each FI As IO.FileInfo In DI.GetFiles
            If FI.LastWriteTime.Date = dDate Then iCount += 1
        Next

        MessageBox.Show(iCount.tostring & " files found edited on " & dDate.tostring)

Open in new window

0
zachvaldezAuthor Commented:
It's looping but not counting the items.
Let me add the time element in the data which I missed.

ABC10-10-20130613              9/13/2013 4:30 PM
ABC11-10-20130613              9/13/2013 4:30 PM
ABC12-10-20130613              9/13/2013  4:30 PM
ABC13-10-20130613              9/13/2013  4:30 PM
ABC14-10-20130613              9/13/2013  4:30 PM
ABC15-10-20130613              9/13/2013  4:30 PM
ABC10-10-20130513              7/01/2013    9:21 AM
ABC11-10-20130513              7/01/2013  9:21 AM
ABC12-10-20130513              7/01/2013  9:21 AM
ABC13-10-20130513              7/01/2013 9:21 AM
ABC14-10-20130513              7/01/2013 9:21 AM
ABC15-10-20130513              7/01/2013 9:21 AM
0
zachvaldezAuthor Commented:
I tested this code..
Dim count As Integer
Dim lastDate As Date
Dim folder As New IO.DirectoryInfo("c:\Process\Print")

For Each file As IO.FileInfo In folder.GetFiles
      If file.LastWriteTime.Date = lastDate Then
            count += 1
      Else
            If file.LastWriteTime.Date > lastDate Then
                  lastDate = file.LastWriteTime.Date
                  count = 1
            End If
      End If
Next

It loops thru the items but after the last item, the next loop goes to the Else
statement , therefore setting the count back to 1....

all the data it loop thru has the datetime to 9/13/2013 4:30 PM
but it adds another loop and it goes to the Else statement and sets the count value = 1
0
zachvaldezAuthor Commented:
There is a hidden file Thumbs.db that should not be counted. It should only count pdf files.
How can I limit it to pdf files?
0
Jacques Bourgeois (James Burger)PresidentCommented:
After the last item, it should go out of the loop. How can it go to the Else?

The way the routine is built is that whenever it finds a file that has a date that is later than the highest date if encountered, it resets the counter to 1 and then starts counting only the files that are at that given date. But it if finds an even newer date, it resets again and starts counting only that newer date.

Is the code in your last post a copy and paste that comes strait from my post, or a copy of the code that you actually used for your testing?
0
Jacques Bourgeois (James Burger)PresidentCommented:
@mleo1

Moving from VB6 to VB.NET is quite an adventure. Try to forget your old tricks. The framework is so full of features that there is almost always something that can do the basic jobs for you. While it took 60 lines of code to get the format of the date in the Control panel in VB6, it takes only 1 in VB.NET.

Take the habit of exploring the properties and methods of the variables you create. They are a throve of wonderful things. Try not to use the old VB6 commands and use the methods of the objects instead. You will be able to do a lot more with less code.

For instance, instead of DateAdd("m",3,yourDate), where you never remember if "m" is month or minutes, use yourDate.AddMonths(3) or yourDate.AddMinutes(3).

Look at MessageBox.Show, that replaces the old MsgBox. It has extra features that let you align the text that the older MsgBox do not have.

You will be having fun for years to come discovering these little things.
0
Jacques Bourgeois (James Burger)PresidentCommented:
For Each file As IO.FileInfo In folder.GetFiles("*.pdf")
0
zachvaldezAuthor Commented:
yeah, one liner is good!

I used more lines
   extension = file.Extension
     If extension = ".pdf" Then
0
Jacques Bourgeois (James Burger)PresidentCommented:
The one liner will also be faster in directories where you have a mix-up of file types, because it will bring less stuff in memory and you will have less loops to perform.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.