Change file names which meet certain criteria to a new name

1 view (last 30 days)
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 Comments
Looky
Looky on 27 Oct 2017
Edited: Looky on 27 Oct 2017
Since you ask, let me explain it in detail.
The code above basically is an adaption of your first try. I just added some math to calculate the newFileNum instead of using your if . From your table in the first post, you can nicely see that the numbers repeat every 10 and last digit of your newFileNum entirely depends on the last digit of your oldNum. You can express this with math by using the modulo function. Modulo 10 gives you the last digit of every number (Modulo 100 would gives you the last 2 digits, and so on...) So the following line computes the newFileNum
newFileNum=indexLookUp(mod(numofName,10)+1)+numofName-mod(numofName,10);
First of all, indexLookUp(mod(numofName,10)+1).
mod(numofName,10)+1 computes the last digit of your oldNum and adds 1. So, if your last digit is 1, it gives 2. last digit 5 gives 6 and the important part, last digit 0 gives 1 (this corresponds to your 10).
If we now arrange your newLastDigits in an array, where the array index follows the same n+1 dependence, we can use this to translate the oldLastDigit to the newLastDigit. Thats were indexLookUp=[-8,9,7,5,3,1,10,8,6,4]; comes in, but ignore the first index for now.
Let's say your oldNum is 21, than the last digit plus one gives, 1+1=2. If you use this as your array index, you get indexLookUp[2], what gives you 9, just like you worked out in your first post. Last digit 6 would give you indexLookUp[6+1]=10 and last digit 0 gives you indexLookUp[0+1]=-8;
Now comes the rest of the line into play, +numofName-mod(numofName,10). This expression gives you the multiplies of 10 that are in your oldNum.
For example numofName=8 would compute to 0. numofName=18 gives you 10, numofName=28 gives you 20 and so on. Adding this to the newLastDigit gives you the newFileNum.
There is only one exception, if your oldNum is 10, than +numofName-mod(numofName,10) computes to 10. Since you wanted 10 to become a 2, you know have to subtract 8. That's how the -8 in the indexLookUp comes in. It's basically 2-10 because of the way the modulo function works.
I hope this makes sense to you and I didn't confuse you ^^ If you have any question just ask.
Have a good one
Peyman Obeidy
Peyman Obeidy on 27 Oct 2017
Thank you for your comprehensive explanations. I have truly enjoyed reading this.
Best wishes

Sign in to comment.

Accepted Answer

Peyman Obeidy
Peyman Obeidy on 27 Oct 2017
Edited: Peyman Obeidy on 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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!