Files getting moved to another folder while renaming using regexp and movefile command

3 vues (au cours des 30 derniers jours)
This is with reference changing file name using matlab code
(1) I have bunch of pdf files with tiles “AuthorA_2005_ First Paper”, “AuthorB_1955_ Second Paper”, “AuthorC_2018_ Third Paper” and so on. I would like to change the name to such a way that there is no space between year and title of paper, i.e. “AuthorA_2005_First Paper”, “AuthorB_1955_Second Paper”, “AuthorC_2018_Third Paper”. For this, I have written the following code:
clear; clc;
folder_name = 'C:\Users\SREERAJ\Desktop\1stone';
dir_infrmatn = dir( fullfile(folder_name, '*.pdf') );
prvs_name = {dir_infrmatn.name};
fun1 = @(x) regexprep(x, '^(.+\d{4})_ (.+)$', '$1_$2');
newfilenames1 = cellfun(fun1, prvs_name, 'UniformOutput', false);
for k = 1 : length(prvs_name)
movefile( fullfile(folder_name, prvs_name{k}), fullfile(folder_name, newfilenames1{k}) );
end
When I run this command, the names of file names are changed as I wanted in the prescribed folder only.
(2) There are another set of files which has the format “2019-06-20 First paper by Author A”, “2019-07-18 Second paper by Author B”, “2019-08-01 3rd Paper by AuthorC”, “19800925FourthPaper_Author D”, “19900329Fifth paper(NiceOne) of great interest”, “20181011Sixth paper_Author E”. I want to convert it into format like “20190620First paper by Author A”, i.e. “yearmonthdayName.pdf” (name can have spaces between them). The code that I have written for them is
clear; clc;
folder_name = 'C:\Users\SREERAJ\Desktop\2ndone';
dir_infrmatn = dir( fullfile(folder_name, '*.pdf') );
prvs_name = {dir_infrmatn.name};
fun2 = @(x) regexprep(x, '^(\d{4})-(\d{2})-(\d{2}) (.+)$', '$1$2$3$4');
newfilenames2 = cellfun(fun2, prvs_name, 'UniformOutput', false);
for k = 1 : length(prvs_name)
movefile( fullfile(folder_name, prvs_name{k}), fullfile(folder_name, newfilenames2{k}) );
end
When I run this code, I am getting an error message “Error using movefile Cannot copy or move a file or directory onto itself.
Now, if I inserted a command folder_name5=' C:\Users\SREERAJ\Desktop\Done'; before “dir_infrmatn” and modified the command inside the for loop to “movefile( fullfile(folder_name, prvs_name{k}), fullfile(folder_name5, newfilenames2{k}) );”, the program runs fine, but the contents are moved (not copied) from folder_name to folder_name5 and renamed there.
My question is why is this (moving the file and renaming) happening for the second case, not for the first one, even though both of the code have similar structure
Thanks in advance.
P.S. (i). The codes that I have used is from Stackoverflow
(ii). I am not uploading the files, Anybody can try to name a pdf file in this fashion and try it.

Réponse acceptée

Mehmed Saad
Mehmed Saad le 17 Mai 2020
Modifié(e) : Mehmed Saad le 18 Mai 2020
Because all the file names are not changing in your code i.e. why it is giving error
clear; clc;
folder_name = 'C:\Users\SREERAJ\Desktop\2ndone';
dir_infrmatn = dir( fullfile(folder_name, '*.pdf') );
prvs_name = {dir_infrmatn.name};
fun2 = @(x) regexprep(x, '^(\d{4})-(\d{2})-(\d{2}) (.+)$', '$1$2$3$4');
newfilenames2 = cellfun(fun2, prvs_name, 'UniformOutput', false);
Now add a new code which compare newfilenames with prvs_name
ind=strcmp(prvs_name,newfilenames2)
ind =
1×5 logical array
1 1 0 0 0
which means that the first two files name have no change in it, the files in my directory are
19800925FourthPaper_Author D.pdf
19900329Fifth paper(NiceOne) of great interest.pdf
2019-06-20 First paper by Author A.pdf
2019-07-18 Second paper by Author B.pdf
2019-08-01 3rd Paper by AuthorC.pdf
The first two file names are not changing and you are trying to overwrite those files creating error
Change only name of those files whose new name is not matching the old name
for k = 1 : length(prvs_name)
if(~ind(k))
movefile( fullfile(folder_name, prvs_name{k}), fullfile(folder_name, newfilenames2{k}) );
end
end
Now i have following pdf files in my directory
19800925FourthPaper_Author D.pdf
19900329Fifth paper(NiceOne) of great interest.pdf
20190620First paper by Author A.pdf
20190718Second paper by Author B.pdf
201908013rd Paper by AuthorC.pdf
PS: to copy the content, use copyfile as movefile is for moving the file
  2 commentaires
Sreeraj T
Sreeraj T le 18 Mai 2020
Modifié(e) : Sreeraj T le 18 Mai 2020
Thank you for the answer.
"Because all the file names are not changing in you code i.e. why it is giving error" - I realised that its true after your comments. But if the files names are "AuthorA_2005_ First Paper", "AuthorB_1955_Second Paper", "AuthorC_2018_ Third Paper" and run the first command, then first article's name is getting changed, but third article is not. It seems that a break apperars when matlab encounters the second file.
As far as the 2nd case is concerned, if the only files are “2019-06-20 First paper by Author A”, “2019-07-18 Second paper by Author B”, “2019-08-01 3rd Paper by AuthorC”, the the program runs and renames in existing folder only.
So the moral of the story (😀 ) is the code that I pasted runs if and only if the files have the SAME name format.
But with your code, no matter whatever be the file name format be, it will be changed to the name format we require. Thanks for that...
One query, if i change folder_name to folder_name5 in fullfile(folder_name, newfilenames2{k}) ) , then the only files the files whose names are changing are moved to the folder_name5. How to move all the files once the renaming is completed?
Mehmed Saad
Mehmed Saad le 18 Mai 2020
You are encoutering error in your initial code because when you try to rename a file in similar folder with the same name as of original file, you are overwritting which creates error i.e. why we put that if(~ind(k)) condition. But when you want to move these files to another folder, that if condition is not required because you are moving them to another folder (so overwritting is not possible).
You can also automate current code by defining two variables and modifying if condition for filepath of previous and new files
prvs_filepath = folder_name;
new_filepath = folder_name; % change it to folder_name5 for testing
ind=strcmp(prvs_name,newfilenames2);
ind2 = strcmp(prvs_filepath,new_filepath);
for k = 1 : length(prvs_name)
if(~ind(k) || ~ind2)
movefile( fullfile(prvs_filepath, prvs_name{k}), fullfile(new_filepath, newfilenames2{k}) );
end
end

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Search Path dans Help Center et File Exchange

Produits


Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by