Home > mfiles > 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-2007
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 global PD;
0015 
0016 % does directory alread exist?
0017 if ~isempty(dir(directory))
0018   return
0019 end
0020 
0021 % need to create directory - first figure out the base directory
0022 slash = find(directory == PD);
0023 if isempty(slash)
0024   basedir = '.';
0025   cdir = directory;
0026 else
0027   if (slash(end) == length(directory))
0028     if (length(slash) == 1)
0029       basedir = '.';
0030       cdir = directory;
0031     else
0032       basedir = directory(1:slash(end-1));
0033       cdir = directory(slash(end-1)+1:end);
0034     end
0035   else
0036     basedir = directory(1:slash(end));
0037     cdir = directory(slash(end)+1:end);
0038   end
0039 end
0040 
0041 % does the base directory exist?
0042 if isempty(dir(basedir))
0043   fatal(['Could not create ' directory ...
0044   ', because the basedir ' basedir ' does not exist.']);
0045 end
0046 
0047 % last char of cdir is a slash? Need to remove it
0048 if (strcmp(cdir(end),PD))
0049   cdir = cdir(1:end-1);
0050 end
0051 
0052 % now try making the directory
0053 [success, message] = mkdir(basedir,cdir);
0054 if ~success
0055   fatal(['Failed to create ' directory ' - error message: ' message]);
0056 end

Generated on Fri 07-Sep-2007 14:42:18 by m2html © 2003