how to rename a bunch of csv files in a folder
Mostra commenti meno recenti
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 Commenti
Jan
il 7 Gen 2019
What have you tried so far? What is the problem you have?
Sara
il 7 Gen 2019
Jan
il 7 Gen 2019
Did you try to solve the problem by your own at first? Then please post the existing code and ask a specific question.
You need a dir command to obtain a list of subfolders, then a loop over the subfolders and another dir command to get the file names. Then sprintf will create the new file names and movefile renames the files.
Sara
il 7 Gen 2019
Risposte (3)
Image Analyst
il 7 Gen 2019
0 voti
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
il 7 Gen 2019
0 voti
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
il 7 Gen 2019
0 voti
2 Commenti
Stephen23
il 7 Gen 2019
It is better to avoid using cd, e.g.
D = 'mymainfolderpath';
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('X_Y_%d_0%d00.csv',ii,jj);
movefile(old,new)
end
end
Note that with sprintf you can easily change the format of the subfolder/file numbers, e.g. how many digits, leading zeros, etc.
Sara
il 7 Gen 2019
Categorie
Scopri di più su File Operations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!