rename files using matlab

1,801 views (last 30 days)
Hello all,
I have a folder on my desktop that has 10 text files as following:
A.txt
Abgd.txt
B.txt
Bbgd.txt
C.txt
Cbgd.txt
S.txt
Sbgd.txt
std.txt
stdbgd.txt
I want to write a code that the changes the names of the first 8 files. I want to rename these files and add an underline (i.e. '_') after the first letterof each file. So my output should look like this:
A_.txt
A_bgd.txt
B_.txt
B_bgd.txt
C_.txt
C_bgd.txt
S_.txt
S_bgd.txt
Here is what I have done so far:
clear all; clc;
d = 'C:\Users\krraz\Desktop\Day13\*.txt';
oldnames = dir(d);
oldnames = {oldnames(~[oldnames.isdir]).name};
L = length(oldnames);
oldnames = oldnames(1:L-2);
points = oldnames(1:2:end);
points = cellfun(@(x) x(1:1), points, 'UniformOutput', false);
strr = '_.txt'; strr = string(strr);
for n = 1:numel(points)
formatSpec = '%1$s%2$s';
points{n} = sprintf(formatSpec,points{n},strr);
end
bgds = oldnames(2:2:end);
bgds = cellfun(@(x) x(1:1), bgds, 'UniformOutput', false);
strr = '_bgd.txt'; strr = string(strr);
for n = 1:numel(bgds)
formatSpec = '%1$s%2$s';
bgds{n} = sprintf(formatSpec,bgds{n},strr);
end
newnames = [points bgds];
newnames = sort(newnames);
for j=1:numel(oldnames)
movefile(d,newnames{j},'f');
end
But when I run my code, It generates a new folder on my desktop and doesn't change the names of the files. What am I doing wrong?
Any insight will be appreciated.
  10 Comments
Malwandla Laurel
Malwandla Laurel on 21 Mar 2022
How to rename the data file using matlab

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 5 May 2017
Edited: Walter Roberson on 15 Sep 2018
You have
d = 'C:\Users\krraz\Desktop\Day13\*.txt';
which includes the *.txt . You do not change d in your code. Later you have
movefile(d,newnames{j},'f');
but d still has the *.txt part, so it is going to move all the text files instead of moving one at a time.
projectdir = 'C:\Users\krraz\Desktop\Day13';
dinfo = dir( fullfile(projectdir, '*.txt') );
oldnames = {dinfo.name};
unwanted = cellfun(@isempty, regexp(oldnames, '^[A-Z][^_].*') );
oldnames(unwanted) = [];
newnames = regexprep(oldnames, '^(.)', '$1_');
for K = 1 : length(oldnames)
movefile( fullfile(projectdir, oldnames{K}), fullfile(projectdir, newnames{K}) );
end
  2 Comments
Walter Roberson
Walter Roberson on 27 May 2019
newnames = regexprep(oldnames, '^\w+\.(\w+)\.\...(\w)\.\w\.\d{4}\.(\d{3}).*$', '$1_$2_$3.txt');

Sign in to comment.

More Answers (2)

KSSV
KSSV on 5 May 2017
Edited: KSSV on 5 May 2017
% Get all text files in the current folder
files = dir('*.txt');
% Loop through each file
for id = 1:length(files)
% Get the file name
[~, f,ext] = fileparts(files(id).name);
rename = strcat(f,'_',ext) ;
movefile(files(id).name, rename);
end
Run this code the folder which has your text files.
  1 Comment
Changoleon
Changoleon on 5 May 2017
This code does NOT exactly do what I wanted. It adds _ to every file. I wanted to add _ after the first letter of the name of each file. But it helped, thank you KSSV!

Sign in to comment.


Solene Frileux
Solene Frileux on 13 Sep 2018
Edited: Walter Roberson on 15 Sep 2018
Hello,
I Have a question about the same subject.
I have several files:
FoodS01HealthSession1.mat
FoodS01PracticeSession1.mat
FoodS01TasteSession1.mat
FoodS01TestSession1.mat
That go from S01 to S021 and I would like to all rename them as following
FoodSub110HealthSession1.mat
FoodSub110PracticeSession1.mat
FoodSub110TasteSession1.mat
FoodSub110TestSession1.mat
from 110 to 130
I cannot manage doing it using the function rename, as I donnnot know how to correctly code the loop nor using the function.
Could anyone help me?
Thanks a lot
  1 Comment
Walter Roberson
Walter Roberson on 15 Sep 2018
Untested
projectdir = '.';
dinfo = dir( fullfile(projectdir, '*.txt') );
oldnames = {dinfo.name};
lookfor = 'FoodS0';
wanted = strncmp(oldnames, lookfor, length(lookfor));
oldnames(~wanted) = [];
for K = 1 : length(oldnames)
this_name = oldnames{K};
old_info = regexp(this_name, '(?<prefix>\D+)(?<id>\d+)(?<suffix>.+)', 'names';
old_id = str2double(old_info.id);
new_name = [old_info.prefix 'ub' sprintf('%03d', old_id+109) old_info.suffix];
movefile( fullfile(projectdir, this_name), fullfile(projectdir, new_name) );
end

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!