PDA

View Full Version : How to detect folder contents changes


Freehold Fred
04-02-2007, 02:31 PM
Any ideas for detecting when a specific folder contents changes; i.e., a new file has been added to that folder.

Also, any idea for coding append to text file.

Prefer VB coding or link to good online source.

TIA,

kelly
04-02-2007, 04:02 PM
I don't have an XP machine running right now, but I think if you go to the folder Properties, it will give you a modified date. Don't know what triggers that date.

PeteF
04-02-2007, 07:27 PM
Any ideas for detecting when a specific folder contents changes; i.e., a new file has been added to that folder.


Hi Fred,
I haven't done any programming in some time so your request gives
me an opportunity to get some practice. I just put together a short
program for you using VBA code which should also work in VB6.


Sub GetDateAndTimeOfLastFileSavedInTargetFolder()

Dim ThisFileDateTime As Variant
Dim LastFileDateTime As Variant
Dim TargetFolder$
Dim FileName$

'Define the target folder
TargetFolder$ = "d:\temp"

'Get info on the oldest file in folder to initialize DIR
ThisFileDateTime = Dir(TargetFolder$ & "\*.*", 0)

Do

'Iterate through each file in folder oldest to newest
FileName$ = Dir()

'Capture the Time & Date of the last file which is the newest one
If FileName$ = "" Then
LastFileDateTime = ThisFileDateTime
Exit Do
End If

'Store the current file Date & Time
ThisFileDateTime = FileDateTime(TargetFolder$ & "\" & FileName$)

'For debug
'MsgBox ThisFileDateTime

Loop

'Display the Date & Time of the most recent file saved in the folder.
MsgBox "DATE & TIME OF LAST FILE SAVED IN FOLDER - " & LastFileDateTime


End Sub




Ok Fred,
But that only captures the DATE & TIME of the last file stored in the target folder.
You need to store that DATE & TIME and re-run the above routine to check if
the DATE & TIME have changed which indicates there has been a new file stored
to the folder.

Yeah, it's complicated but I don't think there is a single VB or VBA function
that will simply tell you when a folder's contents have changed.

---pete---

Freehold Fred
04-02-2007, 08:45 PM
Pete,
Thanks for this head start. I really appreciate it.
Fred

PeteF
04-02-2007, 08:57 PM
Pete,
Thanks for this head start. I really appreciate it.
Fred

Fred, what are you programming in?
VB6 or VB DOT NET or what?

---pete---

Freehold Fred
04-02-2007, 09:32 PM
I think I just found what I was looking for: Folder watcher (out of process folder watcher) (http://www.vbusers.com/downloads/download.asp#item18)

Scans folder for any file changes. Can set scan rate. Written in VB6 but has VBA module as well. Looks like programmer wrote his class and own library as there is an object type called FolderWatcher, which I do not think is native to VB. Let me know what you think.

My next step is:
'Pseudo code
IF FolderWatcher.watcher isChanged = True Then

'Append File
SomeFileContents = SomeFileContents + NewFile.name

EndIf


Fred, what are you programming in?
VB6 or VB DOT NET or what?

---pete---

PeteF
04-03-2007, 12:14 AM
Scans folder for any file changes. Can set scan rate. Written in VB6 but has VBA module as well. Looks like programmer wrote his class and own library as there is an object type called FolderWatcher, which I do not think is native to VB. Let me know what you think.


I tried running it but I get automation errors.

Good luck with your project.:)

---pete---

Freehold Fred
04-03-2007, 12:19 AM
I am told elsewhere that VB.NET provides a class called FileSystemWatcher.

Fred