Author Topic: Detect file changed by an external program?  (Read 415 times)

SoundMike

  • Posts: 364
Detect file changed by an external program?
« on: 11 Nov '22 - 22:14 »
Using functions like BASS_StreamCreateFile etc in a Windows environment, is there a way my program can be notified if the selected file has been edited and saved by an external program while my program has the file open? I've had requests from users who want to be able to edit an audio file in an external program and have my program detect the changed file without the need to close and re-open my program. Something like an appropriate SetSync feature would be useful.

David_AVD

  • Posts: 64
Re: Detect file changed by an external program?
« Reply #1 on: 13 Nov '22 - 01:15 »
Perhaps look into the Directory Change Notification in Windows? https://learn.microsoft.com/en-us/windows/win32/fileio/obtaining-directory-change-notifications

SoundMike

  • Posts: 364
Re: Detect file changed by an external program?
« Reply #2 on: 13 Nov '22 - 04:54 »
Thanks, David_AVD, that looks like it could help.

David_AVD

  • Posts: 64
Re: Detect file changed by an external program?
« Reply #3 on: 13 Nov '22 - 05:21 »
It's something I've been looking into as well.

How to use the minimum number of monitor threads will depend on how spread out the files are.

One way could be to build a list of media files with all the duplicates removed and use one monitor per item. Seems wasteful if most are within the same directory.

Alternatively find the top most directory that covers them all and have just one monitor. That could be tricky if they are spread out on multiple drives though.

SoundMike

  • Posts: 364
Re: Detect file changed by an external program?
« Reply #4 on: 13 Nov '22 - 06:31 »
Fortunately for my requirements it will be just a single audio file at any time. The user would have the file open in the editor component of my program (which allows the user to set levels, start and end points, and any looping requirements) but then decides to edit the file itself using an external editor such as GoldWave or Sound Forge. So while the file is open in my program's editor, I will run a thread using the Windows functions you suggest to raise an event if a change is detected on that file.

David_AVD

  • Posts: 64
Re: Detect file changed by an external program?
« Reply #5 on: 13 Nov '22 - 19:42 »
If it's only one file, you could also use a timer to get the last modified stamp of the file at a fairly short interval. I think it's part of the file attributes record.

SoundMike

  • Posts: 364
Re: Detect file changed by an external program?
« Reply #6 on: 13 Nov '22 - 21:48 »
Yes, I had considered that, but was hoping for an automated notification.