Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

filename manipulation_numbering

1 Ansicht (letzte 30 Tage)
Yachao Chen
Yachao Chen am 4 Nov. 2017
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
I have 44 files with the filenames like '200_R1_20171102_1938_0001.AVG', ‘200_R1_20171102_2025_0002.AVG’,etc. I would like to change all the filenames starting with '200_R1_', and delete the rest of the parts, also ending with a number starts from 0000 to 0043. So the final filenames would be like '200_R1_0000','200_R1_0001'....'200_R1_0043'.
How should I do this?
  2 Kommentare
Walter Roberson
Walter Roberson am 4 Nov. 2017
Should the ending number be the same as the input ending number? Or should it be assigned sequentially? If it should be assigned sequentially then you need to define what order the file names need to be sorted into.
Yachao Chen
Yachao Chen am 4 Nov. 2017
It would be sequentially, not the one as input. Do you know how to do it?

Antworten (1)

Walter Roberson
Walter Roberson am 4 Nov. 2017
Yes. Does the output need to go into the same directory or can an individual new directory be created for it?
If it needs to go into the same directory or any directory that might already have such files, then a pass needs to be made to detect the existing 200_R1_xxxx.AVG files to find the last such file so as to continue the numbering for the 200_R1_2*_xxxx.AVG files. Or you could just assume that everything is going to go right.
dinfo = dir('200_R1_*.AVG');
filenames = {dinfo.name};
numfiles = length(filename);
for K = 1 : numfiles
outfile = sprintf('200_R1_%04d.AVG', K);
if exist(outfile, 'file')
error('Detected existing file "%s", not renaming anything to avoid accidentally clobbering it', outfile);
end
end
for K = 1 : numfiles
infile = filenames{K};
outfile = sprintf('200_R1_%04d.AVG', K);
try
movefile(infile, outfile);
catch ME
error('Move failed on file "%s", giving up to prevent further problems', infile);
end
end

Community Treasure Hunt

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

Start Hunting!

Translated by