Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Message for when files change within a folder?

Status
Not open for further replies.

AndyGroom

Programmer
May 23, 2001
972
0
0
GB
Explorer refreshes itself when files change or are created and deleted. If VB is showing a list of files using the FileList box and one of them is changed by another application or another user, can VB hook into a system message (or some other notification) that would tell it that it needs to refresh the file list?

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Yes, and I've provided examples of exactly how to do it in several past posts ...

Do a keyword search for ...erm ... FolderWatch, which should find thread222-719707 (a non-blocking version) and thread222-576673 (a blocking version). You'll proibably also want thread222-1317846, which rectifies a possible permissions issue. These are the all singing, all dancing solution, in that not only do they tell you something has change din a folder, but alsop what the change was and which file it happened to.

Alternatively you might want to searchj on FindFirstChangeNotification. My solution using this sadly seems to have been deleted from the site, but I am sure there are alteratives. This is less flexible, but if all you want to know is whetehr a file has been added/removed it should be more than sufficient.

Of course you could leverage the face that you can show an explorer window inyour application, which does all the hard work for you ...
 
One way to do this would be to use an asynchronous call to the ReadDirectoryChangesW API.

Hope this helps

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before post
 
As strongm's quicker post indicates... [banghead]

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before post
 
Too late a second time... [tongue]

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before post
 
Thanks, there's some good stuff in there.

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Soumds a bit as though someone is trying to save videos from video sharing sites?
 
Really?

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before post
 

I have Cut and Pasted Both of strongm solutions and No way can I get a peep out of either when a file is Created in the watched Directory

I have used exactly the code provided

Used Temp and text.txt

I run the solution ... pushing the Command Button to start

Then I run a program that creates file c:\Temp\test.txt

I DO NOT get the Message Box UP saying ... File created .....

Peter
 
Works fine here ... and I know a fair number of forum users have also used it successfully ...

Can you make the following highlighted addition to the non-blocking version of the FileIOCompletionRoutine:
Code:
[blue]Public Sub FileIOCompletionRoutine(ByVal dwErrorCode As Long, ByVal dwNumberofBytes As Long, lpOverlapped As OVERLAPPED)
    Dim wombat As FILE_NOTIFY_INFORMATION ' the infamous wombat!
    Dim strFilename As String
    
    If dwNumberofBytes Then  ' did we get anything?
        CopyMemory wombat, cBuffer(0), dwNumberofBytes
        strFilename = Left(CStr(wombat.Filename), wombat.FileNameLength / 2)
    
	    [b][red]Msgbox "File: " & strFilename & vbcrlf & "Action: " & wombat.Action[/red][/b]
        Select Case wombat.Action
            Case FILE_ACTION_ADDED
                If strFilename = strFileWatch Then
                    MsgBox strFilename & " added to monitored folder"
                    Cancelled = True
                End If
            Case FILE_ACTION_MODIFIED
            Case FILE_ACTION_REMOVED
            Case FILE_ACTION_RENAMED_NEW_NAME
            Case FILE_ACTION_RENAMED_OLD_NAME
            Case Else
        End Select
    End If


End Sub[/blue]

And then try running again, and report back the result
 
So Far Nothing happens


Am Going to try with Messages to check if it gets into
Sub FileIOCompletionRoutine

Peter
 
WOMBAT Program Started
COMMAND1 CLICK

SUB FileWatch Accessed


hFolder = -1
nFilter = 81

WaitResult 258


loop Continues


Then FileCreateProgram Started

Fname = "C:\Temp\test.txt"

Simple Routine
chLen = 10
Ch = Freefile
Open Fname For Random As Ch
Chmax = LOF(Ch)
Cmax = cmax \ chLen
recNo = Cmax + 1
Put #AgCh, recNo, "TEST IT"
Close #Ch


Temp\test.txt is Created
Temp\test.txt is ADDED TO
Temp\test.txt is OPENED

Temp\test.txt is CLOSED
Temp\test.txt is DELETED
Temp\test.txt is Created again

even Temp\test.txt is Printed




WOMBAT program Never EXITS SUB FileWatch
(Until Cancelled = True FROM Command2 Button )

STAYS in Loop


WaitResult 258 NEVER changes .....

Peter
 
>So Far Nothing happens

Which indicates that the FileIOCompletionRoutine callback is not being set up properly. This generally happens if ReadAsync (our alias in this example for ReadDirectoryChangesW) is passed a bad handle ...

>hFolder = -1

And here's the problem. CreateFile is failing. Please be aware that most of my examples in this forum do not include error handling.

And CreateFile in this example will fail for a number of reasons

1) Insufficient permissions on the folder
2) Given bad path (unlikely, though, if you've simply cut'n'pasted my code)
3) AV software may block this sort of programmatic access to temp folder

You might try adding, after your line of code that prints out hFolder:

If hFolder=-1 then debug.? err.LastDllError

to provide more info.
 
strongm


Have added diagnostics

result = Err.LastDllError = 87


Result is same if compile and run ... or Run From Inside VB6


Hevent returns varios values e.g 272 , 276 , 284 and 292

Peter
 
hEvent is irrelevant if CreateFile has failed, which we have established is the case.

Error 87 means that an incorrect parameter is being passed to the CreateFile function.

Now, given that you say that you have cut'n'pasted my exact code that shouldn't be possible, so right now I'm at a loss. Indeed, the only way I can reliably duplicate your problem is by changing the value to the Const OPEN_EXISTING from its correct value of 3 to something (anything) else.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top