If you are on Windows and you are runnning a recent release of matlab that supports for .NET (more recent that r2009a as far as i rememeber), here is a quick solution to let some FileSystemWatcher object monitor changes for you and send proper notifications when required:
%% --- Watch for file events in some directory
function [watchers] = WatchFolder(name)
%[
% Get folder's name
if (exist(name, 'dir'))
folder = name;
elseif (exist(name, 'file'))
fullFilename = which(name);
[folder, ~, ~] = fileparts(fullFilename);
else
error('Argument must be a file or folder');
end
% Create file system watcher object
fileObj = System.IO.FileSystemWatcher(folder);
fileObj.EnableRaisingEvents = true;
% Select events to monitor for
filter = bitor(System.IO.NotifyFilters.FileName, System.IO.NotifyFilters.DirectoryName);
filter = bitor(filter, System.IO.NotifyFilters.Attributes);
filter = bitor(filter, System.IO.NotifyFilters.Size);
filter = bitor(filter, System.IO.NotifyFilters.LastWrite);
filter = bitor(filter, System.IO.NotifyFilters.LastAccess);
filter = bitor(filter, System.IO.NotifyFilters.CreationTime);
filter = bitor(filter, System.IO.NotifyFilters.Security);
fileObj.NotifyFilter = filter;
% Add listeners (Need to be kept alive in scope)
watchers.Changed = addlistener(fileObj, 'Changed', @onFolderModification);
watchers.Created = addlistener(fileObj, 'Created', @onFolderModification);
watchers.Deleted = addlistener(fileObj, 'Deleted', @onFolderModification);
watchers.Renamed = addlistener(fileObj, 'Renamed', @onFolderModification);
%]
%% --- This event is raise on any folder modification and as long as listeners are alive
function [] = onFolderModification(source, args) %#ok<INUSL>
%[
fprintf('%s: %s\n', char(args.ChangeType.ToString()), char(args.Name));
%]
Careful, this is only test code and you need to keep listeners alive to continue on receiving events ... up to you to adapt to your situation (insert in gui, watch only some specific file(s), check if file(s) are in use or not from received notifications, etc...).
Here is some logs from my Command Window using current test code:
>> wf= WatchFolder('C:\Users\CitizenInsane\Desktop');
Created: Nouveau Microsoft Word Document.docx
Renamed: MyDocument.docx
Changed: MyDocument.docx
Created: ~$Document.docx
Changed: ~$Document.docx
Created: ~WRD0000.tmp
Changed: ~WRD0000.tmp
Changed: ~WRD0000.tmp
Changed: ~WRD0000.tmp
Changed: ~WRD0000.tmp
Changed: ~WRD0000.tmp
Changed: ~WRD0000.tmp
Renamed: ~WRL0001.tmp
Renamed: MyDocument.docx
Changed: ~WRL0001.tmp
Changed: MyDocument.docx
Changed: MyDocument.docx
>> clear wf; % Stops receiving events (i.e. listeners no longer referenced)