Home > applications > common > gotodirectory.m

gotodirectory

PURPOSE ^

GOTODIRECTORY - Goto a directory creating the path if necessary

SYNOPSIS ^

function [FinalDir, ErrorFlag] = gotodirectory(GotoDir)

DESCRIPTION ^

GOTODIRECTORY - Goto a directory creating the path if necessary
  [FinalDir, ErrorFlag] = gotodirectory(DirName)

  Go to directory DirName.  Creates all the necessary directories along the way. 
  DirName must start from the root of the tree or the present directory.  '.' does nothing.

  Written by Greg Portmann

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [FinalDir, ErrorFlag] = gotodirectory(GotoDir)
0002 %GOTODIRECTORY - Goto a directory creating the path if necessary
0003 %  [FinalDir, ErrorFlag] = gotodirectory(DirName)
0004 %
0005 %  Go to directory DirName.  Creates all the necessary directories along the way.
0006 %  DirName must start from the root of the tree or the present directory.  '.' does nothing.
0007 %
0008 %  Written by Greg Portmann
0009 
0010 
0011 ErrorFlag = 0;
0012 
0013 if nargin == 0
0014     FinalDir = [pwd filesep];
0015     return
0016 elseif isempty(GotoDir)
0017     FinalDir = [pwd filesep];
0018     return
0019 elseif strcmp(GotoDir,'.')
0020     FinalDir = [pwd filesep];
0021     return
0022 end
0023 
0024 
0025 %%%%%%%%%%%%%%%%%%%
0026 % Go to directory %
0027 %%%%%%%%%%%%%%%%%%%
0028 
0029 % Find the file separators
0030 k = findstr(GotoDir, filesep);
0031 
0032 if isempty(k)
0033     % Add a filesep to the end
0034     GotoDir(end+1) = filesep;
0035     k(1) = length(GotoDir);
0036 else
0037     % If k starts with a filesep, then remove the index to it
0038     if k(1) == 1
0039         k(1) = [];
0040     end
0041     % Try again
0042     if length(k) >= 1
0043         if k(1) == 2
0044             k(1) = [];
0045             if ispc && length(k) >= 1
0046                 k(1) = [];
0047             end
0048         end
0049     end
0050     % And again
0051     if length(k) >= 1
0052         if k(1) == 3
0053             k(1) = [];
0054         end
0055     end
0056 end
0057 
0058 % If doesnot end with a filesep, then add one
0059 if ~strcmp(GotoDir(end), filesep)
0060     GotoDir(end+1) = filesep; 
0061     k(end+1) = length(GotoDir);
0062 end
0063 
0064 
0065 % Try to cd as far as possible
0066 j = 1;
0067 for i = 1:length(k)
0068     %GotoDir(j:k(i))
0069     try
0070         cd(GotoDir(j:k(i)));
0071     catch
0072         % Create that directory
0073         [Success, msg] = mkdir(GotoDir(j:k(i)));
0074         if Success
0075             cd(GotoDir(j:k(i)));
0076         else
0077             if ispc
0078                 ErrorFlag = 1;
0079                 fprintf('   Problem creating directory: %s\n', msg);
0080                 break
0081             else
0082                 % I'm getting a tcsh error that's not really can error so I have a test another way
0083                 try
0084                     cd(GotoDir(j:k(i)));
0085                 catch
0086                     ErrorFlag = 1;
0087                     fprintf('   Problem creating directory: %s\n', msg);
0088                     break
0089                 end
0090             end
0091         end
0092     end
0093     j = k(i) + 1;
0094 end
0095 
0096 
0097 % Return
0098 FinalDir = pwd;
0099 FinalDir = [FinalDir filesep];
0100

Generated on Mon 21-May-2007 15:32:41 by m2html © 2003