changing the name of multiple csv files in a folder

I have a folder containing 3000 csv files. All of these are named in the format: cor0.1 sc10 fc0.1_0.csv.
I want to change the name of all of these files to c0_1f0_1_0.csv.
Is there a way i can write a loop to do this for me?

 Accepted Answer

hello
this is a first attempt
I prefered to create the renamed copies in a separate folder
also my code will probably need some upgrade as I don't know how the numberring inside your filenames can evolve among your 3000 files
d = dir('*.csv')
dir_out = [cd '\out']
for ci = 1:numel(d)
filename = d(ci).name;
ind_und = strfind(filename,'_'); % search underscores
ind_dot = strfind(filename,'.'); % search dots
ind_f = strfind(filename,'f'); % search "f"
new_filename = [filename(1) filename(ind_dot(1)-1) '_' filename(ind_dot(1)+1) filename(ind_f ),...
filename(ind_dot(2)-1) '_' filename(ind_dot(2)+1) '_' filename(end-4:end)];
copyfile(filename,fullfile(dir_out,new_filename));
end

9 Comments

Thank you for your response.
I have inputted your code into my script, and I can see what you are doing in this.
however, I get the following error:
Error using copyfile
The system cannot find the path specified.
Is this to do with the line below, and do I have to create a new folder for these to start with?
dir_out = [cd '\out']
hello
yes , you have to create that subfolder first
C.G.
C.G. on 15 Jan 2021
Edited: C.G. on 15 Jan 2021
Thanks, I understand now.
Ive created the folder and run the code, butit is only identifying 'ci = 1', when 'd' is a 3001x1 structure. It has successfully done the first 10 items, but stops there.
so it has reached ci = 10
what is the error message ?
can you copy paste the list of the csv files filenames in a txt file ?
ci only ever goes to 1, but in the folder 'out', It has converted 10/3000 files.
I have tried to copy them inot a text file and have attached it but i dont know if this is done right
ok , i understand now how your file name structure operates. simly the last number is changing
so I am not saying that it's the best code , but it's seems to work (created a few dummy files)
tx to matt to remind me of strrep that makes my code more readable
d = dir('*.csv')
dir_out = [cd '\out']
for ci = 1:numel(d)
filename = d(ci).name;
new_filename = filename(1:length(filename)-4); % without the extension !!
extension = filename(length(filename)-3:end);
new_filename = strrep(new_filename,' ',''); % remove single spaces
new_filename = strrep(new_filename,'.','_'); % replace dot by underscore
new_filename = strrep(new_filename,'sc10',''); % remove sc10
new_filename = strrep(new_filename,'cor','c'); % replace
new_filename = strrep(new_filename,'fc','f'); % replace
new_filename = [new_filename extension];
copyfile(filename,fullfile(dir_out,new_filename)); %
end
great thank you that works now!
Rather than fragile indexing, it better to use fileparts to split the filename and file extension:

Sign in to comment.

More Answers (1)

csvPath = '/path/to/files';
filelist = dir(fullfile(csvPath,'*.csv'));
for iFile = 1:numel(fileList)
thisFile = fullfile(csvPath,fileList(iFile).name);
movefile(thisFile, strrep(thisFile,'old','new'));
end
This is just psuedocode. I don't quite see how you're mapping the old filename to the new one, so I just placed a string replace function in the loop. If you need help on that piece, please post more details.
See:

6 Comments

Thank you for your repsonse.
I have currently got something like this. I understand that the old file name needs to be mapped to the new one, but I am unsure how to change this code to reflect my file names.
D = 'E:\Rice piles\Numerical\Rice Pile\Sensitivity Analysis\New folder\COR.1 SC10 FC0.1';
S = dir(D);
N = setdiff({S([S.isdir]).name},{'.','..'});
for ii = 1:numel(N)
T = dir(fullfile(D,N{ii},'*.csv'));
for jj = 1:numel(T)
old = fullfile(D,N{ii},T(jj).name);
new = sprintf('c0_1f0_1.csv',ii,jj);
movefile(old,new)
end
end
That looks like it's traversing multiple directories? You wouldn't need an embedded loop if you have all your files in one directory (no need for the ii loop). If you need to look in multiple subfolders, I recommend dir2 to simplify things.
Can you provide some info on how the current filename relates to the one you wish to overwrite it with? Give some examples.
I currenlty have 3000 csv files in one folder named: 'cor0.1 sc10 fc0.1_0.csv', 'cor0.1 sc10 fc0.1_1.csv', 'cor0.1 sc10 fc0.1_2.csv', etc, up to 3000.
I want to rename this to 'c0_1f_0.1_0.csv' etc, so I want to remove the '.', spaces and the phrase 'sc10'.
Mathieu gave a suggestion below. I find strrep to the most readable. Sprintf would also work if you are using the iteration variable.
thisFile = strrep(thisFile,' ',''); % remove single spaces
thisFile = strrep(thisFile,'.',''); % remove period
thisFile = strrep(thisFile,'sc10',''); % remove sc10
If you have multiple spaces, perhaps see here.
This SPRINTF call does not use its inputs for anything:
new = sprintf('c0_1f0_1.csv',ii,jj)
C.G.
C.G. on 15 Jan 2021
Edited: C.G. on 15 Jan 2021
I understand sprintf, but i am struggling to write a loop to do this.
I have started using Mathieu's code below, but this reaches 10 iterations and stops even though it identifies 3001 csv's

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 15 Jan 2021

Commented:

on 15 Jan 2021

Community Treasure Hunt

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

Start Hunting!