Change file names which meet certain criteria to a new name

4 vues (au cours des 30 derniers jours)
Peyman Obeidy
Peyman Obeidy le 26 Oct 2017
Commenté : Peyman Obeidy le 27 Oct 2017
I need to change 80 file names, which meet certain criteria and copy it into a new folder. I listed the relationship between the current file name and the file name to be below. The first 10 doesn’t follow any chronological order (or at least I couldn’t find one). However, this order gets repeated 8 times (after every 10 files) – see example attached.
Here is the code that I have written. Is there a better way to do that?
Any help would be appreciated
path ='D:\MATLAB\2017\Phil_changefileName\';
Stru =dir( fullfile(path, '*.vsi') );
fileNames = { Stru.name };
for iFile = 1: numel( fileNames )
if strcmp(fileNames{iFile}, '01.vsi') % (fileNames{1}=='01.vsi')
iFileN=9;
newName = fullfile(outputPath, sprintf( '%02d.vsi', iFileN ) );
%To rename a file use movefile (yes, really!): movefile('oldname.csv','newname.csv')
copyfile(fullfile(path, fileNames{iFile}), newName );
elseif(strcmp(fileNames{iFile}, '02.vsi')) % (fileNames{1}=='02.vsi')
iFileN=7;
newName = fullfile(outputPath, sprintf( '%02d.vsi', iFileN ) );
%To rename a file use movefile (yes, really!): movefile('oldname.csv','newname.csv')
copyfile(fullfile(path, fileNames{iFile}), newName );
end
end
  6 commentaires
Peyman Obeidy
Peyman Obeidy le 27 Oct 2017
Thank you for your comprehensive explanations. I have truly enjoyed reading this.
Best wishes

Connectez-vous pour commenter.

Réponse acceptée

Peyman Obeidy
Peyman Obeidy le 27 Oct 2017
Modifié(e) : Peyman Obeidy le 27 Oct 2017
Answered by Looky. see above.
% code clear clc path =[pwd '\']; stru =dir( fullfile(path, '*.vsi') ); fileNames = { stru.name }; outputPath = [path 'NewImage\'];%function_GenerateNumber(j,2) mkdir(outputPath); % sequenceNum = {9,7,5,3,1,10,8,6,4,2}; indexLookUp=[-8,9,7,5,3,1,10,8,6,4]; % derives from your sequenceNum, % corresponds to oldIndex = [10,1,2,3,4,5,6,7,8,9] for iFile = 1: numel(fileNames) fileName=fileNames{iFile}; numofName=str2double(fileName(7:end-4)); % cut out all but the number newFileNum=indexLookUp(mod(numofName,10)+1)+numofName-mod(numofName,10); % this calculates the new number from the old, repeats after 10 newName = fullfile(outputPath, sprintf('%02d.vsi', newFileNum) ); copyfile(fullfile(path, fileNames{iFile}), newName ); end

Plus de réponses (0)

Catégories

En savoir plus sur Standard File Formats dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by