How to get Matlab to unlock or release a directory?

44 次查看(过去 30 天)
I have scripts that create a temporary directory to store intermediate files created during processing. At the end of the scripts I want to clean this temporary directory out and remove the directory. I can remove the files that I create, but when I run the rmdir() command, it will usually fail with the message "MATLAB:RMDIR:SomeDirectoriesNotRemoved". If I go into the file system and do a "ls -al" command, I will see that there is a Matlab lock file or something in the directory. It will go away if I shut down Matlab. Unfortunately it's not easy to recreate, because I've tried creating a directory from the command line, adding, modifying and removing a few files, and then removing the directory and it always seems to work fine. Something about running my scripts as a whole is causing Matlab to lock the temporary directory, and I don't know a way to unlock it.
  3 个评论
Walter Roberson
Walter Roberson 2015-6-30
<<If a file delete request is sent to a file on an NFS server while an NFS client still has the file open, NFS will create a file with the ".nfsX" extension, where "X" can be any number of characters. This allows the NFS server to maintain the file until all dependent processes on the file have been terminated.>>
This suggests that there is an open file in the directory. Perhaps something you did not fclose() ?
I think the inode of the .nfs* file should be the same as that of the file that is thought to be open, so if you record the ls -i before the rmdir and then look at the ls -i of the .nfs* file, I suspect you should find a correspondence.
Scott
Scott 2015-7-1
That was it. I found a function call that didn't fclose its file ID, and the inode helped me to quickly figure out which one it was. Thank you!

请先登录,再进行评论。

回答(2 个)

Guillaume
Guillaume 2015-7-1
Are all handles to files in the temp directory closed?
Does
fclose('all')
help?

Walter Roberson
Walter Roberson 2015-6-30
Make sure that you cd out of the directory before removing it.
  4 个评论
Image Analyst
Image Analyst 2015-6-30
"I do cd to the directory" - that's exactly what he said not to do. That probably IS the problem. For one thing, there's no need to cd to any directory - I almost never do. You can use fullfile() and save things to the folder using the full file name without actually having the folder be the current directory. So, if you did cd into it, then cd out of it before you call rmdir().
If that still does not work, you can find out what has a hold on it with the shareware program called "unlocker" (Google it). It will pry off whatever program's grubby hands are on it.
Walter Roberson
Walter Roberson 2015-6-30
编辑:Walter Roberson 2015-6-30
Scott cd'd to the containing directory, not the directory itself.
unlocker appears to be an MS Windows program, but "ls -al" is a Unix command so unlocker would not be applicable.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Search Path 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by