bugmagnet
bugmagnet

Reputation: 7769

Detect if the contents of a folder have changed?

Conditions:

I have code that looks like this, written using my steroidal wrapping of VBScript using MSScript.

do
    a = files.collectfiles( "c:\userver", "" )
    for i = 0 to ubound( a )
        f = a(i)
        if strings.endswith( f, ".usv" ) then
            d = files.readfilee( f )
            on error resume next
            executeglobal d
            nErr = err.number
            sErr = err.description
            on error goto 0
            if nErr <> 0 then
                trace "*** Error " & nErr & ", " & sErr
            end if
            files.deletefile f
        end if
    next
    system.sleep 10
    system.cooperate
loop

There's a lot of disk activity with that call to files.collectfiles. Is there some way of detecting a change in the contents of a folder without actually scanning the folder for files?

Upvotes: 0

Views: 2561

Answers (3)

jitter
jitter

Reputation: 54605

There is a sample which claims to work on all versions from Win95 up to at leas WinXP. Developed under Win98 with VB5. Using the (then? provided links to the docu below) undocumented SHChangeNotify* Functions.

SHChangeNotifyRegister: Receive Shell Change Notifications

SHChangeNotifyRegister Function SHChangeNotifyDeregister Function

There is another solution using ReadDirectoryChangesW here:

VB6 WinAPI ReadDirectoryChangesW (check the 5th post from Yang Kok Wah)

Upvotes: 2

merkuro
merkuro

Reputation: 6177

You specifically asked for something in VB and running on win98 and I have no answer for this, but MS has a c/win32 example on how to achieve this on Windows2000+ with FindFirstChangeNotification. Another thing is that apparently "FileSystemWatcher" in .NET is not working/supported on Win98. What is my point? There maybe is no easy solution for this and you have to come up with something on your own.

Upvotes: 0

Yuliy
Yuliy

Reputation: 17718

Define "change in the contents of a folder".

If it means that a file was added, deleted, or renamed, then the modified timestamp of the folder is updated whenever such an event occurs.

If you're instead wanting to know when files are modified, then you'll need to read them.

That said, looking at what you're trying to do (scan a folder for new .usv files, and process them and delete them), then just keeping track of the timestamp on the folder and updating it right before you call collectfiles is best (note that the correct time to log is just BEFORE calling collectfiles, otherwise you run the risk of not waking up if a file gets added during the collectfiles call or immediately afterward).

Upvotes: 1

Related Questions