I'm using matlab.unittest.fixtures for unit testing with matlab.unittest.TestCase. The operation involves file conversions, so I think it makes sense to use "setup" method for preparing a work folder and "teardown" for deleting the same folder.
The removal of the folder is done by rmdir function. It works fine, but 3 to 5 times per 10 trials, it fails to delete the folder with an error message 'MATLAB:RMDIR:SomeDirectoriesNotRemoved'. This is essentially about the same issue, but putting rehash() after rmdir did not help. It's just unpredictable at the moment. I checked the folder's write permission with fileattrib function. Write permission is surely granted, but it still fails. Only to illustrate the point, I wrote much simpler code. Interestingly, I successfully reproduced the problem.
clc;
str = 'TEMP_FOLDER';
for i = 1:100
if isdir(str)
rmdir(str,'s')
rehash
end
mkdir(str);
A = rand(10);
save(fullfile(str,'A'),'A')
[~,s] = fileattrib(str);
fprintf('UserWrite %d\n',s.UserWrite);
try
rmdir(str,'s');
rehash
catch mex
disp(mex)
throw(mex)
end
fprintf('OK %d\n',i);
end
Although it's quite random, usually it fails within 10 trials.
UserWrite 1
OK 1
UserWrite 1
OK 2
UserWrite 1
MException with properties:
identifier: 'MATLAB:RMDIR:SomeDirectoriesNotRemoved'
message: 'D:\xxxxx\TEMP_FOLDER could not be removed.'
cause: {0x1 cell}
stack: [0x1 struct]
D:\xxxxx\TEMP_FOLDER could not be removed.
Does anyone know how to solve this? I tested on Windows 7 (R2016a); the same code worked on OS X (R2016a).
2 Comments
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/286871-rmdir-frequently-fails-with-the-matlab-rmdir-somedirectoriesnotremoved-error#comment_370369
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/286871-rmdir-frequently-fails-with-the-matlab-rmdir-somedirectoriesnotremoved-error#comment_370369
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/286871-rmdir-frequently-fails-with-the-matlab-rmdir-somedirectoriesnotremoved-error#comment_370394
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/286871-rmdir-frequently-fails-with-the-matlab-rmdir-somedirectoriesnotremoved-error#comment_370394
Sign in to comment.