how to rename a bunch of csv files in a folder

20 views (last 30 days)
Sara
Sara on 7 Jan 2019
Commented: Sara on 7 Jan 2019
Hello,
I have a folder containing several subfolders in which there are bunch of csv files.
I want to rename the csv files in this format. X_Y_subfolder's number_0file's number00. For example, the name of the files in first subfolder and second subfolders should be as following:
Subfolder1) X_Y_1_0100, X_Y_1_0200,...
Subfolder2) X_Y_2_0100, X_Y_2_0200,....
I appreciate any help.
  4 Comments
Sara
Sara on 7 Jan 2019
Yes. I've tried dir and movefile but I've got error. I did not use sprintf. I will try this command.
Thanks

Sign in to comment.

Answers (3)

Image Analyst
Image Analyst on 7 Jan 2019
Code is in the FAQ: click here
put a call to movefile() to rename the files.
ALso see attached code to recursivley go through all subfolders.

Image Analyst
Image Analyst on 7 Jan 2019
Another way is to simply use the fileDatastore() function to get a list of all files in all subfolders. In fact, this may be the easiest way, then just loop over the filenames calling movefile().

Sara
Sara on 7 Jan 2019
I rename all files with this code. It is not the best solution nor the professional one, but it works for my case .
Thanks.
S = dir('mymainfolderpath');
a=sum([S(~ismember({S.name},{'.','..'})).isdir]); %%numbers of subfolders
for i = 1:a
n=[(num2str(i))]
cd(fullfile('mymainfolderpath',n)) %% the names of my subfolders are 1,2,...
files=dir('*.csv')
for j = 1:length(files)
name= ['X_Y_',(num2str(i)),'_0',(num2str(j)),'00','.csv']
movefile (files(j).name,name);
end
end
  2 Comments
Sara
Sara on 7 Jan 2019
Dear Stephen,
Thanks for your comments. Actually, I am beginner but, whenever I ask a question I learn a lot from the experts.
thanks again for your useful comments

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!