Clear Filters
Clear Filters

I need to rename bulk files in windows by adding some text in present files name and retaining other part as it is.

1 view (last 30 days)
Passanger_last_ordercut_X
Passanger_last_ordercut_Y
Passanger_last_ordercut_Z
Passanger_Middle_ordercut_X
Passanger_Middle_ordercut_Y
Passanger_Middle_ordercut_Z
should be renamed to
Passanger_last_ordercut_X_AC
Passanger_last_ordercut_Y_AC
Passanger_last_ordercut_Z_AC
Passanger_Middle_ordercut_X_AC
Passanger_Middle_ordercut_Y_AC
Passanger_Middle_ordercut_Z_AC
I need matlab to import name of files present in given path, rename accordingly and save those files with new name. As in Passanger_last_ordercut_X.cur becomes Passanger_last_ordercut_X_AC.cur

Accepted Answer

Walter Roberson
Walter Roberson on 31 Oct 2016
dinfo = [dir('*_X.cur'); dir('*_Y.cur'); dir('*_Z.cur')];
for K = 1 : length(dinfo)
oldname = dinfo(K).name;
[folder, basename, ext] = fileparts(oldname);
newname = fullfile(folder, [basename '_AC' ext]);
movefile(oldname, newname);
end

More Answers (1)

KSSV
KSSV on 31 Oct 2016
Edited: KSSV on 31 Oct 2016
clc; clear all ;
str1 = 'Passanger_last_ordercut_X Passanger_last_ordercut_Y Passanger_last_ordercut_Z Passanger_Middle_ordercut_X Passanger_Middle_ordercut_Y Passanger_Middle_ordercut_Z' ;
rep0 = [{'_X'} {'_Y'} {'_Z'}] ; % replace these characters
rep1 = [{'_X_AC'} {'_Y_AC'} {'_Z_AC'}] ; % with these
for i = 1:length(rep0)
str1 = strrep(str1, rep0{i}, rep1{i}) ;
end
  5 Comments
Image Analyst
Image Analyst on 1 Nov 2016
Aditya, since you asked, you must not know about the FAQ. Both answers are essentially in the FAQ, with the addition of a movefile() in the middle of the loop to do the renaming. Here is the FAQ (there's other good stuff in there too so you should look at it all): http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!