📄 renamefile.m
字号:
function [varargout] = renamefile(varargin)
% renamefile - renames a file to another filename
%
% FORMAT: renamefile(FromFile, ToFileOrDir);
%
% Input fields:
%
% FromFile filename(s) of files being renamed
% either a char array for single file or
% cell array with multiple char arrays
% ToFileOrDir one of the following:
% - single filename as target filename
% - single foldername as target for one or
% multiple files
% - cell array with multiple targetnames
% (dims must match with input array)
%
% Note: there is a special usage you can use to replace a pattern
% in one or multiple filenames:
%
% renamefile(FileOrListOfFiles, FromPattern, ToPattern);
% Version: v0.6f
% Build: 7062711
% Date: Jun-27 2007, 11:05 AM CEST
% Author: Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools
% enough arguments ?
if nargin < 2
error( ...
'BVQXtools:BadArgument', ...
'Too few arguments. Try ''help %s''.', ...
mfilename ...
);
end
% act on special case
if nargin > 2 && ...
ischar(varargin{2}) && ...
~isempty(varargin{2}) && ...
ischar(varargin{3})
if ischar(varargin{1}) || ...
(iscell(varargin{1}) && ...
~isempty(varargin{1}) && ...
ischar(varargin{1}{1}))
try
rval = renamefile(varargin{1}, ...
strrep(varargin{1}, varargin{2}, varargin{3}));
catch
rval = -1;
end
else
error( ...
'BVQXtools:BadArgument', ...
'Bad argument given.' ...
);
end
if nargout > 0
varargout{1} = rval;
end
return;
end
FromFile = varargin{1};
ToFileOrFolder = varargin{2};
if nargout > 0
varargout{1}=-1;
end
if iscell(FromFile) && ...
numel(FromFile) == 1
FromFile = FromFile{1};
end
if iscell(ToFileOrFolder) && ...
numel(ToFileOrFolder) == 1
ToFileOrFolder = ToFileOrFolder{1};
end
if (~ischar(FromFile) && ...
~iscell(FromFile)) || ...
(~ischar(ToFileOrFolder) && ...
~iscell(ToFileOrFolder))
error( ...
'BVQXtools:BadArgument', ...
'Bad argument.' ...
);
end
if ischar(FromFile) && ...
~ischar(ToFileOrFolder)
error( ...
'BVQXtools:BadArgument', ...
'Bad argument.' ...
);
end
if ischar(FromFile)
if exist(FromFile, 'file') < 2
if nargout > 0
varargout{1} = 8;
end
warning( ...
'BVQXtools:RenameFailed', ...
'Source file ''%s'' not found.', ...
FromFile ...
);
return;
end
if exist(ToFileOrFolder, 'dir') == 7
TargetFile = i_movetodir(FromFile, ToFileOrFolder);
if exist(TargetFile, 'file') < 2
if nargout > 0
varargout{1} = 16;
end
warning( ...
'BVQXtools:RenameFailed', ...
'Couldn''t move source to destination folder.' ...
);
return;
end
else
TargetFile = i_movetofile(FromFile, ToFileOrFolder);
if exist(TargetFile, 'file') < 2
if nargout > 0
varargout{1} = 16;
end
warning( ...
'BVQXtools:RenameFailed', ...
'Couldn''t move source to destination file.' ...
);
return;
end
end
else
% initialize counters
NumOfFiles = numel(FromFile);
CompleteSuccess = 1;
if (ischar(ToFileOrFolder) && ...
exist(ToFileOrFolder, 'dir') ~= 7) || ...
(iscell(ToFileOrFolder) && ...
numel(ToFileOrFolder) ~= NumOfFiles)
error( ...
'BVQXtools:BadArgument', ...
'Bad sized argument.' ...
);
end
if iscell(ToFileOrFolder)
for FileNumber = 1:NumOfFiles
if exist(ToFileOrFolder{FileNumber}, 'dir') == 7
TargetFile = ...
i_movetodir(FromFile{FileNumber}, ToFileOrFolder{FileNumber});
else
TargetFile = ...
i_movetofile(FromFile{FileNumber}, ToFileOrFolder{FileNumber});
end
if exist(TargetFile, 'file') < 2
CompleteSuccess = 0;
warning( ...
'BVQXtools:RenameFailed', ...
'Couldn''t rename ''%s'' to ''%s''.', ...
FromFile{FileNumber}, TargetFile ...
);
end
end
else
for FileNumber = 1:NumOfFiles
TargetFile = i_movetodir(FromFile{FileNumber}, ToFileOrFolder);
if exist(TargetFile, 'file') < 2
CompleteSuccess = 0;
warning( ...
'BVQXtools:RenameFailed', ...
'Couldn''t rename ''%s'' to ''%s''.', ...
FromFile{FileNumber}, TargetFile ...
);
end
end
end
if nargout > 0
if CompleteSuccess ~= 1
varargout{1} = 1;
else
varargout{1} = 0;
end
end
end
if nargout > 0 && ...
varargout{1} == -1
varargout{1} = 0;
end
% sub functions
function tgf = i_movetodir(ff, tf)
if any(ff == filesep)
[sp{1:3}] = fileparts(ff);
sn = [sp{2} sp{3}];
else
sn = ff;
end
tgf = [tf filesep sn];
if ispc
system(['move "' ff '" "' tgf '"']);
else
system(['mv "' ff '" "' tgf '"']);
end
% end of function tgf = i_movetodir(ff, tf)
function tgf = i_movetofile(ff, tf)
if ~any(tf == filesep) && ...
any(ff == filesep)
sp = fileparts(ff);
tgf = [sp filesep tf];
else
tgf = tf;
end
if ispc
ff = strrep(ff, '/', '\');
tgf = strrep(tgf, '/', '\');
system(['move "' ff '" "' tgf '"']);
else
system(['mv "' ff '" "' tgf '"']);
end
% end of function tgf = i_movetofile(ff, tf)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -