efficient way to rename files while preserving first 5 letters and removing the rest

4 views (last 30 days)
How can I efficiently code a MATLAB program to rename a list of files, such that first 5 letters do not change and all the letters from the letter number 6 until the end of the filename removes, without changing suffix? file names do have different lengths.

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 21 Mar 2012
Hi,
get the list of files using e.g. dir:
files = dir('*.txt');
Then a simple function to use is fileparts
for i=1:length(files)
[pathname,filename,extension] = fileparts(files(i).name);
% the new name, e.g.
newFilename = filename(1:5);
% rename the file
movefile([filename extension], [newFilename extension]);
end
This assumes the script to be in the same folder as the files. Otherwise use fullfile to combine the folder and the filename into absolute file paths.
Titus

More Answers (1)

David Young
David Young on 21 Mar 2012
Here is a function that will do what you need. Be very careful, though, as renaming is irreversible and you need to be certain that you are operating on the correct directory and that you will not lose any important information in the file names. It's probably sensible to backup your files before carrying out any operation like this one.
This function renames all the files in a given directory. If you only want to rename some files, you should make a new directory and move the files you want to rename to that directory, then move them back again afterwards.
function truncateFilenames(d, nchars)
%TRUNCATEFILENAMES truncates filenames
% TRUNCATEFILENAMES(D, NCHARS) truncates the file names of all files in
% directory D so that the filename (not including the extension) has
% NCHARS characters. Shorter filenames are left unchanged. If the
% resulting names will not be unique, an error is thrown and no changes
% are made.
% NCHARS must be a positive integer.
%
% Caution! The operation is irreversible.
validateattributes(nchars, {'numeric'}, {'scalar' 'real' 'positive' 'integer'});
if ~exist(d, 'dir')
error('matlabAnswers:truncateFilenames:noDir', ...
'Directory not found');
end
fstruct = dir(d); % examine directory
fnames = {fstruct.name}; % extract full names
% decompose names
[~, fnms, fexts] = cellfun(@fileparts, fnames, 'UniformOutput', false);
% retain only those that are too long initially
needTrunc = cellfun(@length, fnms) > nchars;
fnames = fnames(needTrunc);
fnms = fnms(needTrunc);
fexts = fexts(needTrunc);
% do the truncation, and replace the extension
fnmsout = cellfun(@(sn, se) [sn(1:nchars) se], fnms, fexts, 'UniformOutput', false);
% check for uniqueness
if ~isequal(length(fnmsout), length(unique(fnmsout)))
error('matlabAnswers:truncateFilenames:nonunique', ...
'Resulting filenames will not be unique');
end
% do the renames
for k = 1:length(fnms)
movefile(fullfile(d, fnames{k}), fullfile(d, fnmsout{k}));
end
end
  2 Comments
Ahmed Harun-Al-Rashid
Ahmed Harun-Al-Rashid on 3 Nov 2016
Hi, this is helpful. However, this can only truncate from the end. But, I want to truncate first 11 characters of my files.
Please suggest how can I do this.
Sincerely, Rashid
Michael Rowlands
Michael Rowlands on 4 Jul 2017
I also have many files that I need to rename in various ways. I wrote a function to rename files using lists and wildcards. It's called "easyrename" and it is available on File Exchange. Check it out and let me know what you think !
Here's how it works in the situations detailed above:
1. Keep the first 5 characters only: This matches any filenames with at least 5 characters. It renames those files to just the first 5 characters. (The * in the destination expression just grabs a copy of the string that matches the * in the source expression.)
easyrename('c:\USERN\desktop\files\?????*.*','c:\USERN\desktop\files\copy2\?????.*')
Moving FILES .....
Moving c:\USERN\desktop\copy2\files\port\AkelPadPortable\Fonts-Eng.txt
To c:\USERN\desktop\files\copy3\files\port\AkelPadPortable\Fonts.txt
Moving c:\USERN\desktop\copy2\files\port\AkelPadPortable\Fonts-Rus.txt
To c:\USERN\desktop\files\copy3\files\port\AkelPadPortable\Fonts.txt
DONE !
2. Drop the first 11 characters, keep the rest of the filename: This matches any filenames with at least 11 characters. It renames those files with the first 11 characters removed. (The * in the destination expression just grabs a copy of the string that matches the * in the source expression.)
easyrename('c:\USERN\desktop\copy2\???????????*.txt','c:\USERN\desktop\files\copy3\*.t2t')
Moving FILES .....
Moving c:\USERN\desktop\copy2\ACTIVE_mlxtools\0044\files2cascade.txt
To c:\USERN\desktop\files\copy3\ACTIVE_mlxtools\0044\de.t2t
Moving c:\USERN\desktop\copy2\ACTIVE_mlxtools\mlxn\files2cascade18inch.txt
To c:\USERN\desktop\files\copy3\ACTIVE_mlxtools\mlxn\de18inch.t2t
Moving c:\USERN\desktop\copy2\files\port\Docs\FullScreen-Eng.txt
To c:\USERN\desktop\files\copy3\files\port\Docs\Eng.t2t
DONE !

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!