Rename folders using excel key

1 view (last 30 days)
Hi everyone,
I am brand new to using MATLAB, so please excuse if this task is very easy.
I have ~300 folders that I have to rename based on an excel spreadsheet key. So the spreadsheet would be with Column A with complete path for the old foldername and Column B will be the new folder name (very easy to change it to path). How can I do that?
Thank you everyone!
  1 Comment
Mehmed Saad
Mehmed Saad on 16 May 2020
Edited: Mehmed Saad on 16 May 2020
What are the names of your folders? Show us your code. Also read this

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 16 May 2020
%assuming no header on the file
t = readtable('YourExcelKey.xlsx', 'readvariablenames', false);
for K = 1 : height(t)
oldfile = t.Var1{K};
if ~exist(oldfile, 'dir') && ~exist(oldfile, 'file')
fprintf('no source #%d: "%s"\n', K, oldfile);
continue
end
newfile = t.Var2{K};
if strcmp(oldfile, newfile)
fprintf('old and new are identical names, #%d: "%s"\n', K, oldfile);
continue;
end
if exist(newfile, 'dir') || exist(newfile, 'file')
fprintf('did not move #%d "%s" to "%s" because destination exists\n', K, oldfile, newfile);
continue
end
[newdir, lastpart] = fileparts(newfile);
if ~exist(newdir, 'dir')
fprintf('target directory #%d does not exist: "%s"\n', K, newdir);
continue;
end
try
movefile(oldfile, newfile);
catch ME
fprintf('Problem moving #%d "%s" to "%s"\n', K, oldfile, newfile);
end
end
You might want to think more about the target directory not existing case. For example if you asked to move /A/B/C/ to /A/D/E/ and /A/D does not exist, then trying ot move /A/B/C/ directly to /A/D/E/ would fail. The code would test that /A/D exists to move E into and refuse to try if it is not there. What you might want to think about is trying to create all missing levels of the destination path.
Also, I have the code check to see whether the destination exists already. If you were moving files, then it might be reasonable to move a file over-top of another file. But if you are moving directories and you try to move a directory to another directory, then it would not replace the existing directory, it would move the source into the destination directory, which is probably not what is wanted. Then there is the case where the source is a file but the destination is a directory: would you want the destination directory deleted and replaced with the source file, or would you want the file to be moved into the destination directory? These are usage decisions that have to be made, and in this code I deal with them by not permitting the operation if the destination already exists.
  3 Comments
Walter Roberson
Walter Roberson on 17 May 2020
Please get into the habit of using fullfile() instead of concatenating strings to add directory names. There are places in MATLAB that are not consistent about whether they include the directory seperator or not, so it is best to get used to using fullfile() to be sure that the code will work.
Ashitha Pathrose
Ashitha Pathrose on 17 May 2020
I will do that! Thank you!

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations 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!