Home > utils > check_dir.m

check_dir

PURPOSE ^

Checks for existence of outdir, and creates it if necessary

SYNOPSIS ^

function status = check_dir(outdir, verbose, parents, group, perm)

DESCRIPTION ^

 Checks for existence of outdir, and creates it if necessary

 status = check_dir(outdir, verbose, parents, group, perm);

 status returns 0 on success and 1 on failure

 INPUT
 outdir:   a string that specifies the directory to check.
 verbose:  0 or 1. 1 means that some basic status messages will
           output to the command line
 parents:  0 or 1. 1 means that check_dir will execute mkdir with the '-p'
           argument, if outdir does not exist. This will create parent
           directories as needed. execute 'man mkdir' from the unix command
           line for more information
 groupname: if set, mkdir will change the group of the new directory to
           this value
 perm:     if set, mkdir will change the permissions of the new directory
           to this value

 Copyright (c) 2005-2012 The Regents of the University of California
 All Rights Reserved

 Author(s):
 10/18/05 Petr Janata
 08/17/09 Fred Barrett - added recursive option
 04/04/11 Fred Barrett - added group and permission setting. Permission
 setting can be used to set the gid stick bit, if needed (perm: 'g+s')

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function status = check_dir(outdir, verbose, parents, group, perm)
0002 % Checks for existence of outdir, and creates it if necessary
0003 %
0004 % status = check_dir(outdir, verbose, parents, group, perm);
0005 %
0006 % status returns 0 on success and 1 on failure
0007 %
0008 % INPUT
0009 % outdir:   a string that specifies the directory to check.
0010 % verbose:  0 or 1. 1 means that some basic status messages will
0011 %           output to the command line
0012 % parents:  0 or 1. 1 means that check_dir will execute mkdir with the '-p'
0013 %           argument, if outdir does not exist. This will create parent
0014 %           directories as needed. execute 'man mkdir' from the unix command
0015 %           line for more information
0016 % groupname: if set, mkdir will change the group of the new directory to
0017 %           this value
0018 % perm:     if set, mkdir will change the permissions of the new directory
0019 %           to this value
0020 %
0021 % Copyright (c) 2005-2012 The Regents of the University of California
0022 % All Rights Reserved
0023 %
0024 % Author(s):
0025 % 10/18/05 Petr Janata
0026 % 08/17/09 Fred Barrett - added recursive option
0027 % 04/04/11 Fred Barrett - added group and permission setting. Permission
0028 % setting can be used to set the gid stick bit, if needed (perm: 'g+s')
0029 
0030 if ~exist('verbose','var')
0031   verbose = 0;
0032 end
0033 if exist('parents','var') && parents
0034   mkdiropts = '-p ';
0035 else
0036   mkdiropts = '';  
0037 end
0038 status = -1;
0039 
0040 if ~exist(outdir,'dir')
0041   if verbose
0042     fprintf('Making directory: %s\n', outdir);
0043   end
0044   %escape spaces and other strange characters for mkdir
0045   outdir = regexprep(outdir,'[()'',& ]','\\$0');
0046     
0047   unix_str = sprintf('mkdir %s%s',mkdiropts,outdir);
0048   status = unix(unix_str);
0049 
0050 else
0051   status = 0;
0052 end
0053 
0054 if ~status
0055   if exist('group','var') && ~isempty(group)
0056     unix_str = sprintf('chgrp %s %s',group,outdir);
0057     status = unix(unix_str);
0058   end
0059   if ~status && exist('perm','var') && ~isempty(perm)
0060     unix_str = sprintf('chmod %s %s',perm,outdir);
0061     status = unix(unix_str);
0062   end
0063 end % if ~status

Generated on Sat 24-Aug-2019 04:00:39 by m2html © 2003