Error using rmdir

112 views (last 30 days)
Jason
Jason on 16 Nov 2011
Commented: Justin Marrott on 1 Dec 2021
Guys -
I'm writing a GUI in which I create a directory with the following code:
SessionName = get(handles.SessionName,'String');
%later on...
mkdir(SessionName);
Later on, the user has an option to remove that folder and all its contents by using rmdir:
rmdir(SessionName,'s');
But, every time I call rmdir in my GUI I get the following error:
??? Error using ==> mkdir
Access is denied.
Error in ==> ClusterGUI2>createsession_Callback at 95
mkdir(SessionName);
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> ClusterGUI2 at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)ClusterGUI2('createsession_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
It's as if MATLAB (R2011a) doesn't recognize the presence of the folder I just created. When I call 'dir', the folder is listed in the directory, but at the command line when I try
rmdir(SessionName,'s');
??? Error using ==> rmdir
No directories were removed.
Any thoughts? This is driving me nucking futs.
  1 Comment
Jason
Jason on 16 Nov 2011
The problem appears to be a permissions issue when I create the folder, but I am the administrator on my machine and there is no documentation for setting/changing folder permissions in Matlab that I can find.

Sign in to comment.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 16 Nov 2011
Maybe you need to run rehash() after executing rmdir().
  1 Comment
Walter Roberson
Walter Roberson on 16 Nov 2011
rehash() should only have an effect if you were executing files in that directory. If that is the case, that you were executing files there, then MATLAB might have internal file pointers due to the JIT process, and the solution would be to "clear" the name of each function that was called upon from the directory.

Sign in to comment.

More Answers (6)

ewnetu bee
ewnetu bee on 26 Jan 2015
Note: You may need administrator privileges to complete the installation. open ur matlab as run as administrator it works for me simple .

Sergio Santos
Sergio Santos on 11 Jun 2015
Also, I know it might sound silly but check that inside the directory there is no file that is currently open with another program. I had this problem once and I just had just opened a text with notepad. When I closed the file my problems disappeared also.

Walter Roberson
Walter Roberson on 16 Nov 2011
According to the traceback message, you are calling mkdir instead of rmdir. In which case creating the directory again is being denied because the directory already exists.

Jason
Jason on 16 Nov 2011
Well, in my code I call rmdir first, then mkdir immediately after that. mkdir reports an error because the directory does already exist, but it exists because rmdir didn't delete the folder.

Jason Ross
Jason Ross on 16 Nov 2011
I've seen issues where operations will return as "complete" from the OS, but there are still some things that are going on or caches that need to finish flushing. As a diagnostic, try pausing for a few seconds before executing the mkdir. Or put a breakpoint in the debugger and see how long the rmdir takes to delete.
Also, are all the processes using the directory terminated before you end it? Most OS's will not remove a directory if it's still in use by a process. If you change into the directory, make sure you change out of it before you delete it.
  1 Comment
Justin Marrott
Justin Marrott on 1 Dec 2021
Really good point tucked into your answer. If your current directory (cd) is in the folder, then it wont delete.
I have been trying to figure what was the problem with some parallel workers not clearing their working folder with rmdir. Your answer finally gave me the answer, I needed to move their working folder out of the folder to be deleted.

Sign in to comment.


Jason
Jason on 16 Nov 2011
Thanks everybody, the rehash suggestion appears to have done the trick.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!