How do I rename sound files in a folder?

3 views (last 30 days)
ayela
ayela on 3 Aug 2018
Answered: OCDER on 3 Aug 2018
I have 100 sounds in a folder under bad names and I want rename each sound to "stim1", "stim2", ect... How do rename these sounds in matlab?

Answers (1)

OCDER
OCDER on 3 Aug 2018
Use movefile to rename a file from BAD_NAME --> GOOD_NAME. But, BACKUP YOUR FILES before doing this for the first time! Bugs can be costly x.x
Use dir, fullfile, and maybe regexprep to find and replace filename string, before using movefile. Here's an example to get you started.
A = dir('**/*.wav');
for j = 1:length(A)
% If your bad file name has a sequence ID #, like Bad_121_blah.wav, extract "121" first via regexp.
% Ex: Bad_121 -> stim121.wav, figure out how to extract "121" via regexp(BadName, '\d+', 'match')
BadNameNum = regexp(A(j).name, '\d+', 'match');
GoodName = sprintf('stim%s.wav', BadNameNum{1});
movefile(fullfile(A(j).folder, A(j).name)), fullfile(A(j).folder, GoodName));
end

Community Treasure Hunt

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

Start Hunting!