spm_copy.m 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. function spm_copy(source, dest, varargin)
  2. % Copy file(s)
  3. % FORMAT spm_copy(source, dest [,opts])
  4. %__________________________________________________________________________
  5. % Copyright (C) 2017 Wellcome Trust Centre for Neuroimaging
  6. % Guillaume Flandin
  7. % $Id: spm_copy.m 7354 2018-06-22 10:44:22Z guillaume $
  8. %-Source and destination
  9. %--------------------------------------------------------------------------
  10. source = cellstr(source);
  11. if nargin < 2
  12. dest = pwd;
  13. end
  14. dest = cellstr(dest);
  15. if numel(source) == 1
  16. source = repmat(source,numel(dest),1);
  17. elseif numel(dest) == 1
  18. dest = repmat(dest,numel(source),1);
  19. elseif numel(source) ~= numel(dest)
  20. error('Number of elements in source and dest must be one or equal.');
  21. end
  22. %-Options (struct array or key/value pairs)
  23. %--------------------------------------------------------------------------
  24. opts = struct('gzip', false, 'gunzip', false, 'nifti', false, 'mode', {{}});
  25. if nargin > 2
  26. if isstruct(varargin{1})
  27. opt = varargin{1};
  28. else
  29. opt = struct;
  30. for i=1:2:numel(varargin)
  31. opt.(varargin{i}) = varargin{i+1};
  32. end
  33. end
  34. else
  35. opt = struct([]);
  36. end
  37. fn = fieldnames(opt);
  38. for i=1:numel(fn)
  39. if ~isfield(opts,lower(fn{i}))
  40. warning('Unknown option "%s".',fn{i});
  41. end
  42. opts.(lower(fn{i})) = opt.(fn{i});
  43. end
  44. %-Actual copy
  45. %--------------------------------------------------------------------------
  46. for i=1:numel(source)
  47. protocol = source{i}(1:find(source{i}==':',1)-1);
  48. if ismember(protocol,{'file','http','https','ftp'})
  49. urlwrite(source{i}, dest{i}); % dest{i} has to be a filename...
  50. else
  51. sts = copyfile(source{i}, dest{i}, opts.mode{:});
  52. end
  53. if opts.nifti
  54. if strcmp(spm_file(source{i},'ext'),'img')
  55. sts = copyfile(spm_file(source{i},'ext','hdr'), dest{i}, opts.mode{:});
  56. sts = copyfile(spm_file(source{i},'ext','mat'), dest{i}, opts.mode{:});
  57. sts = copyfile(spm_file(source{i},'ext','json'), dest{i}, opts.mode{:});
  58. elseif strcmp(spm_file(source{i},'ext'),'hdr')
  59. sts = copyfile(spm_file(source{i},'ext','img'), dest{i}, opts.mode{:});
  60. sts = copyfile(spm_file(source{i},'ext','mat'), dest{i}, opts.mode{:});
  61. sts = copyfile(spm_file(source{i},'ext','json'), dest{i}, opts.mode{:});
  62. elseif strcmp(spm_file(source{i},'ext'),'nii')
  63. sts = copyfile(spm_file(source{i},'ext','mat'), dest{i}, opts.mode{:});
  64. sts = copyfile(spm_file(source{i},'ext','json'), dest{i}, opts.mode{:});
  65. end
  66. end
  67. if opts.gzip && ~strcmp(spm_file(source{i},'ext'),'gz')
  68. gzip(spm_file(source{i},'path',dest{i}));
  69. spm_unlink(spm_file(source{i},'path',dest{i}));
  70. end
  71. if opts.gunzip && strcmp(spm_file(source{i},'ext'),'gz')
  72. gunzip(spm_file(source{i},'path',dest{i}));
  73. spm_unlink(spm_file(source{i},'path',dest{i}));
  74. end
  75. end