Home > data-utility > copymove_subject_file.m

copymove_subject_file

PURPOSE ^

COPYMOVE_SUBJECT_FILE Move a file from one place in a subject dir to another

SYNOPSIS ^

function copymove_subject_file(action, subject, source, dest, varargin)

DESCRIPTION ^

COPYMOVE_SUBJECT_FILE Move a file from one place in a subject dir to another

 Usage:
   copymove_subject_file('copy', SUBJECT, SOURCE, DESTINATION)
       Within the SUBJECT's directory, COPY a file from one place to
       another.

   copymove_subject_file('move', SUBJECT, SOURCE, DESTINATION)
       Within the SUBJECT's directory, move a file from one place to
       another.

 If the DESTINATION file already exists, or if the SOURCE file does not
 exist, an exception is thrown.  MOVEFILE or COPYFILE, which are used
 internally, may also throw exceptions of its own.  If the DESTINATION's
 directory is not there, we try to make it.

   copymove_subject_file(..., 'dry')
       A "dry run": show what would be done.  If an exception would be
       thrown, it is thrown in this case too (except for ones generated
       directly by MOVEFILE).

   copymove_subject_file(..., 'quiet')
       Don't say what's happening.

   copymove_subject_file(..., 'force')
       Don't give up if the destination file already exists.  Try to
       copy/move over it, if possible.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function copymove_subject_file(action, subject, source, dest, varargin)
0002 %COPYMOVE_SUBJECT_FILE Move a file from one place in a subject dir to another
0003 %
0004 % Usage:
0005 %   copymove_subject_file('copy', SUBJECT, SOURCE, DESTINATION)
0006 %       Within the SUBJECT's directory, COPY a file from one place to
0007 %       another.
0008 %
0009 %   copymove_subject_file('move', SUBJECT, SOURCE, DESTINATION)
0010 %       Within the SUBJECT's directory, move a file from one place to
0011 %       another.
0012 %
0013 % If the DESTINATION file already exists, or if the SOURCE file does not
0014 % exist, an exception is thrown.  MOVEFILE or COPYFILE, which are used
0015 % internally, may also throw exceptions of its own.  If the DESTINATION's
0016 % directory is not there, we try to make it.
0017 %
0018 %   copymove_subject_file(..., 'dry')
0019 %       A "dry run": show what would be done.  If an exception would be
0020 %       thrown, it is thrown in this case too (except for ones generated
0021 %       directly by MOVEFILE).
0022 %
0023 %   copymove_subject_file(..., 'quiet')
0024 %       Don't say what's happening.
0025 %
0026 %   copymove_subject_file(..., 'force')
0027 %       Don't give up if the destination file already exists.  Try to
0028 %       copy/move over it, if possible.
0029 %
0030 args = varargin;
0031 
0032 if strcmp(action, 'copy')
0033     action_func = @copyfile;
0034     action_word = 'Copying';
0035 elseif strcmp(action, 'move')
0036     action_func = @movefile;
0037     action_word = 'Moving';
0038 else
0039     error('copymove_subject_file:badaction', ...
0040         'ACTION must be one of ''copy'' and ''move'' (got %s)', action);
0041 end
0042 
0043 % Defaults:
0044 really = 1; % Yes, really perform the action
0045 quiet = 0;  % No, don't suppress status messages
0046 force = 0;  % No, don't do the action if the destination already exists
0047 
0048 if ismember('dry', args)
0049     really = 0;
0050     args = setdiff(args, 'dry');
0051 end
0052 if ismember('quiet', args)
0053     quiet = 1;
0054     args = setdiff(args, 'quiet');
0055 end
0056 if ismember('force', args)
0057     force = 1;
0058     args = setdiff(args, 'force');
0059 end
0060 
0061 if ~isempty(args)
0062     error('copymove_subject_file:arg', 'Unrecognized arguments %s', evalc('disp(args);'));
0063 end
0064 
0065 
0066 % idea: parameterize, to move, copy, or remove (i.e. move to extra_p)
0067 
0068 subject_dir = get_subject_dir(subject);
0069 full_source = fullfile(subject_dir, source);
0070 full_dest = fullfile(subject_dir, dest);
0071 
0072 % Ensure source exists
0073 if ~ exist(full_source, 'file')
0074     error('copymove_subject_file:nxsource', 'Source file does not exist');
0075 end
0076 % Ensure dest does not exist
0077 if exist(full_dest, 'file') && ~ force
0078     error('copymove_subject_file:xdest', 'Destination file already exists');
0079 end
0080 
0081 dest_dirname = fileparts(full_dest);
0082 need_to_make_dir = ~ exist(dest_dirname, 'dir');
0083 
0084 if force
0085     extra_args = {'f'};
0086 else
0087     extra_args = {};
0088 end
0089     
0090 
0091 
0092 %
0093 % Do the move, or not
0094 %
0095 
0096 if really
0097     if need_to_make_dir
0098         if ~ quiet
0099             fprintf('Making destination dir %s\n', dest_dirname);
0100         end
0101         mkdir(dest_dirname);
0102     end
0103     
0104     if ~ quiet
0105         % Say we're moving/copying the file
0106         fprintf('%s %s\n\tto %s\n', action_word, full_source, full_dest);
0107     end
0108     
0109     action_func(full_source, full_dest, extra_args{:});
0110     
0111 else
0112     if need_to_make_dir && ~ quiet
0113         fprintf('Would make destination dir %s\n', dest_dirname);
0114     end
0115     if ~ quiet
0116         fprintf('Would %s %s\n\tto %s\n', action, full_source, full_dest);
0117     end
0118 end
0119

Generated on Wed 24-May-2017 00:00:56 by m2html © 2005