The actual command I'm using is rmdir(<directory>, 's') to remove everything in the temporary directory. I just did it again, and paused the program before it executed the rmdir command. I did "ls -al" and there were no hidden files, just some text files and subdirectories. I then stepped to execute the rmdir command, and it removed everything in the directory, but did not remove the temporary directory. I did "ls -al" again, and suddenly there was a .nfs* file in the directory.
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
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.
回答(2 个)
Guillaume
2015-7-1
Are all handles to files in the temp directory closed?
Does
fclose('all')
help?
0 个评论
Walter Roberson
2015-6-30
Make sure that you cd out of the directory before removing it.
4 个评论
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
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 Center 和 File Exchange 中查找有关 Search Path 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!