1

I have a doubt about if there is any way to know automatically if a specific file is not used by other program. Let me explain this idea better.

I have a file which is opened and utiliced by other software, and I want to know with MATLAB if that file is already unused by the software and then make whatever I want. By now I have a provisional solution and is checking every period if the file isn't changing the weight, but this solution makes a unnecesary searching.

Thanks by the way !

lisandrojim
  • 509
  • 5
  • 18
  • So, if I understand you correctly, you just want to know [whether or not a file is already open in another program](http://stackoverflow.com/questions/15708/how-can-i-determine-whether-a-specific-file-is-open-in-windows)? Because, if so, you'd use `lsof` in *nix and any of the tools mentioned in that link for windows. And, if you wanted to know how you could write what `lsof` is doing into your program (rather than actually calling `lsof`), you'd run `strace` on `lsof` to determine which syscalls it was making. – Parthian Shot Jun 03 '14 at 20:55
  • Yeah! But the thing is for example, I have a .doc opened with Microsoft Word and I'm working on it; in the moment that I close the document, it becomes unused, then I want to know if MATLAB automatically reconize that the file is unused. I apreciate your quickly answer. – lisandrojim Jun 03 '14 at 21:02
  • I honestly don't see why you would want to use MATLAB when Linux is so much better. If you want, you can cheat and use `system()` to call `lsof` from MATLAB and you can check the output yourself. – rayryeng Jun 03 '14 at 21:12

1 Answers1

0

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)
CitizenInsane
  • 4,755
  • 1
  • 25
  • 56