how to rename multiple file with sequent custom number?
Show older comments
dear matlab expert, I have a folder contain multiple txt file as following
1.txt
5.txt
9.txt
13.txt
17.txt
21.txt
25.txt
29.txt
33.txt
37.txt
I want to rename these files and add an underline.So my output should look like this:
2015_001.txt
2015_002.txt
2015_003.txt
2015_004.txt
2015_005.txt
2015_006.txt
2015_007.txt
2015_008.txt
2015_009.txt
2015_010.txt
I tried this code but nothing happened
% Directory of the files
d = 'C:\ZTD\pwv2015';
% Retrieve the name of the files only
names = dir(d);
names = {names(~[names.isdir]).name};
% Calculate the length of each name and the max length
len = cellfun('length',names);
mLen = max(len);
% Exclude from renaming the files long as the max
idx = len < mLen;
len = len(idx);
names = names(idx);
% Rename in a LOOP
for n = 1:numel(names)
oldname = [d names{n}];
newname = sprintf('2015_%s%0*s',d,mLen, names{n});
dos(['rename "' oldname '" "' newname '"']); % (1)
end
Accepted Answer
More Answers (0)
Categories
Find more on Time Series Events in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!