Home > SaliencyToolbox > ensureDirExists.m

ensureDirExists

PURPOSE ^

ensureDirExists - makes sure that directory exists.

SYNOPSIS ^

function ensureDirExists(directory)

DESCRIPTION ^

 ensureDirExists - makes sure that directory exists.

 ensureDirExists(directory) checks if directory exists
    and attempts to create it if it doesn't exist.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 % ensureDirExists - makes sure that directory exists.
0002 %
0003 % ensureDirExists(directory) checks if directory exists
0004 %    and attempts to create it if it doesn't exist.
0005   
0006 % This file is part of the SaliencyToolbox - Copyright (C) 2006-2013
0007 % by Dirk B. Walther and the California Institute of Technology.
0008 % See the enclosed LICENSE.TXT document for the license agreement.
0009 % More information about this project is available at:
0010 % http://www.saliencytoolbox.net
0011 
0012 function ensureDirExists(directory)
0013 
0014 % does directory alread exist?
0015 if ~isempty(dir(directory))
0016   return
0017 end
0018 
0019 % need to create directory - first figure out the base directory
0020 slash = find(directory == filesep);
0021 if isempty(slash)
0022   basedir = '.';
0023   cdir = directory;
0024 else
0025   if (slash(end) == length(directory))
0026     if (length(slash) == 1)
0027       basedir = '.';
0028       cdir = directory;
0029     else
0030       basedir = directory(1:slash(end-1));
0031       cdir = directory(slash(end-1)+1:end);
0032     end
0033   else
0034     basedir = directory(1:slash(end));
0035     cdir = directory(slash(end)+1:end);
0036   end
0037 end
0038 
0039 % does the base directory exist?
0040 if isempty(dir(basedir))
0041   error(['Could not create ' directory ...
0042   ', because the basedir ' basedir ' does not exist.']);
0043 end
0044 
0045 % last char of cdir is a slash? Need to remove it
0046 if (strcmp(cdir(end),filesep))
0047   cdir = cdir(1:end-1);
0048 end
0049 
0050 % now try making the directory
0051 [success, message] = mkdir(basedir,cdir);
0052 if ~success
0053   error(['Failed to create ' directory ' - error message: ' message]);
0054 end

Generated on Thu 18-Jul-2013 06:10:46 by m2html © 2005