newdir.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This file is part of NS2DDV. %
% %
% Copyright(C) 2011-2018 C. Calgaro (caterina.calgaro@math.univ-lille1.fr) %
% E. Creusé (emmanuel.creuse@math.univ-lille1.fr) %
% T. Goudon (thierry.goudon@inria.fr) %
% A. Mouton (alexandre.mouton@math.univ-lille1.fr) %
% %
% NS2DDV is free software: you can redistribute it and/or modify it under the terms %
% of the GNU General Public License as published by the Free Software Foundation, %
% either version 3 of the License, or (at your option) any later version. %
% %
% NS2DDV is distributed in the hope that it will be useful, but WITHOUT ANY %
% WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A %
% PARTICULAR PURPOSE. See the GNU General Public License for more details. %
% %
% You should have received a copy of the GNU General Public License along with %
% NS2DDV. If not, see <http://www.gnu.org/licenses/>. %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[respath] = newdir(newpath, defaultpath)
% INPUT
% newpath Path to be created
% defaultpath Default path if the creation of new path fails
%
% OUTPUT
% respath Either newpath or defaultpath according to the success of the creation of newpath
% Verify if the directory exists
if (exist(newpath, 'dir') == 7)
% The directory exists --> verify if it is writable
namefile = strcat(newpath, '/test');
[fid,err] = fopen(namefile, 'a');
if strcmp(err, '')
% The directory is writable
fclose(fid);
delete(namefile);
respath = newpath;
else
disp(err)
% The directory is not writable
message = sprintf('WARNING : The directory %s is not writable. The directory %s will be used instead.\n', ...
newpath, defaultpath);
disp(message);
respath = defaultpath;
end
else
% The directory does not exist --> try to create it
[s,mess,messid] = mkdir(newpath);
if ~(max(strcmp(messid, {'', 'MATLAB:MKDIR:DirectoryExists'})) == 1)
% The directory cannot be created
message = sprintf('WARNING : The directory %s cannot be created. The directory %s will be used instead.\n', ...
newpath, defaultpath);
disp(message);
respath = defaultpath;
else
% The directory has been created
respath = newpath;
end
end
end