Change the file extension according to your need.
datafolder=uigetdir();
% files=dir(datafolder); % if you don't have any subfolder or other type of files then you can use this line
files=dir([datafolder '\*.xlsx']); % if you have subfolders or different type of files
for kk=1:length(files)
oldname=fullfile(datafolder,files(kk).name);
[~,fname,ext]=fileparts(files(kk).name);
fname=fname(end-8:end); % taking only last 7 characters excluding extension
newname = fullfile(datafolder,[fname,ext]);
movefile(oldname, newname);
end
Or you can use the following code. You have to select any one file in your desired folder. The following code will rename all those files having with similar file extension. It is also applicable where the selected folder conatins mixed type of files or subfolders.
[file,datafolder]=uigetfile('*.*');
[~,~,ext]=fileparts(file);
files=dir([datafolder, '*', ext]);
for kk=1:length(files)
oldname=fullfile(datafolder,files(kk).name);
[~,fname,ext]=fileparts(files(kk).name);
fname=fname(end-8:end); % taking only last 7 characters excluding extension
newname = fullfile(datafolder,[fname,ext]);
movefile(oldname, newname);
end