Within a script, can I close or redirect a Windows File Explorer window opened with 'winopen'?

14 vues (au cours des 30 derniers jours)
I have a script that processes iteratively through a number of directories, spending a few hours per directory. It would be very convenient to be able to view the directory currently being processed at each moment while the script is running.
MATLAB's 'winopen' command can be used to open a file explorer window at the start of processing each new directory, which is helpful. But these windows proliferate if the script issues a new 'winopen' each time it starts processing a new directory. With a lot of such windows open, it is tough to see which directory is currently being processed.
Hence my question: Is there a way to either:
- close an existing Windows Explorer window on a directory when I am done processing that directory, or
- point the existing Windows Explorer window to the new directory when the script changes directories?
Or maybe there is some entirely different approach?
Thanks for any suggestions,

Réponse acceptée

Jan
Jan le 19 Mai 2022
You could open a tree view to the folder inside Matlab:
[mtree, container] = uitree('v0', 'Root','C:\') % Still working?
This wa supported in R2018b, but maybe newer Matlab versions limit this.
  3 commentaires
Jan
Jan le 20 Mai 2022
In R2018b the shown code does allow to open subfolders.
The live-updates would need some additional effort, e.g. a FileSystemViewer or timer. You might be able to expand the code even to show the wanted details. But I agree, that a standard file system view window is most useful, because it is the look&feel, that a user expects.
I've experimented with:
runtime = java.lang.Runtime.getRuntime();
process = runtime.exec('EXPLORER.EXE /n,/e,"C:\"'); % non-blocking
pause(5)
process.destroy(); % force-kill the process (rc will be 1)
without success. Using !tasklist and !taskkill would be fragile and you could kill the wrong application.
You can use the WindowAPI to close a window with a specific name. But as soon as you click around in the Explorer window, the name changes.
So what about opening one Explorer window, which points to a virtual link created by SUBST and to modify this link dynamically? Ugly, so ugly, that I hesitate to try it.
Nicer: Create a GUI, which displays a button: "Open currently processed folder view". Then the user can close this view and opens it again dynamically. This is more user-friendly then brutally closing this Explorer window: imagine, the user has open such a window, gets an email concerning a specific file, navigates to this folder using the file explorer currently visible and - whoops, Matlab closes it. Evil.
So let the user use a Matlab GUI button to open the currently processed folder and let him close this window manually.
Jeff Miller
Jeff Miller le 21 Mai 2022
Hi @Jan,
Thanks for your additional comment. It provided several useful hints, and I'm accepting your answer, even though it isn't quite the version that works best for me (so far), as shown below:
sTargetDir = 'C:\MATLAB'; % Change to any desired File Explorer target directory
% Start a new, separate, file explorer process, based on an answer from Walter Roberson at
% https://au.mathworks.com/matlabcentral/answers/418173-run-programm-in-background-without-using-start#answer_335999
proc = System.Diagnostics.Process();
proc.StartInfo.FileName = 'explorer.exe';
proc.StartInfo.Arguments = ['/n,/separate,' sTargetDir];
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.Start();
pause(5)
% Get list of active File Explorer processes using getProcess from FileExchange
procList1 = getProcess('explorer');
% Assume the new file explorer process just opened is the last one in this list
pid = procList1{end,1};
% kill that process. based on an answer from Walter Roberson at
% https://au.mathworks.com/matlabcentral/answers/500196-kill-a-processb-from-matlab
if ispc
cmd = sprintf('taskkill /PID %d > nul', pid); % > nul suppresses taskkill output string
else
cmd = sprintf('kill %d', pid);
end
s = system(cmd);
I'm not sure how robust this approach is, but it works in some simple cases that I've tried so far.

Connectez-vous pour commenter.

Plus de réponses (1)

Paul
Paul le 21 Mai 2022
Maybe using an actxserver is a viable approach. I've used it for interacting with Power Point. It's not so bad once you get the hang of it and can find the good documentation on the methods, properties, objects, etc.
I did a quick search and found this link that seems to be on point and might be a good place to start. No idea what capabilities are actually available, but I'd be surprised if you can't make it do what you want.

Produits


Version

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by