• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

Watching a folder for file population

Hello all, I have a rather tricky VB problem I have not been able to solve so far.
I need to write a function that will "watch" an error folder for file population.  If a file jumps into the error folder, I need to perform a series of operations (i.e. copy to another directory, rename, strip extension, ect. ) on the said file and delete it from the error folder in the end.  The problem with this is the file that appears in the error folder is not always the same name.  depending on the nature of the error, a range of different files can potentially appear.  In addition, it is possible for more than one file to populate in this location at a time, it is not uncommon to have seven or more such files appear at once.  I need some sort of way to constantly scan the folder to detect for errors even though my operations will only handle one at a time.

I tried doing this with a simple timer control but am having difficutly opening a folder and scanning for files without having the exact name.  Any suggestions?
1 Solution
Maybe this example can help:

Steve SiricaSenior Web Application EngineerCommented:
That looks pretty cool.  I think I'll download that for future use.  Thanks!

You can achieve this using FilesySystemObject

Add a reference to "microsoft scripting runtime"  to your project

then use this sample code:

Private Sub Form_Load()

    Dim fs As New FileSystemObject
    Dim fo As Folder
    Set fo = fs.GetFolder("C:\Myfloder")    '<===Change this to the specific folder name
    Dim Fi As File
    For Each Fi In fo.Files
         MsgBox " Name=" & Fi.Name & " " & vbCrLf & "Date created=" & Fi.DateCreated

End Sub

In the above code Fi(file object) has a lots ofother useful properties whcih you can use them.

More questions, please  Ask,

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Steve SiricaSenior Web Application EngineerCommented:
It's also alot easier now in VB.Net seeing that MS now sends a canned filewatch control along with.
gwosgoodIT MgrAuthor Commented:
Comment for "unknown_routine"  your solution appears to be very handy.  My only question is how to use.  Would I just tie it into a timer routine and check for files once every X seconds?

As far as your timer interval is bigger that 100 (1 tenth of a second)

it is healthy( usage of CPU is negligible).

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now