Using movefile function in existing and unexisting files

The following use of the "movefile" function works only if the requested files exist. But it yields error messages and stops the iteration when the files do not exist:
DistPath_MT = SourceFolder;
DistFolder_MT = fullfile(DistPath_MT,'MT');
if ~exist(DistFolder_MT )
mkdir(DistFolder_MT )
end
DistPath_T1w = SourceFolder;
DistFolder_T1w = fullfile(DistPath_T1w,'T1w');
if ~exist(DistFolder_T1w)
mkdir(DistFolder_T1w)
end
DistPath_PD = SourceFolder;
DistFolder_PD = fullfile(DistPath_T1w,'PD');
if ~exist(DistFolder_PD)
mkdir(DistFolder_PD)
end
movefile(fullfile(SourceFolder,'*_MT*'),DistFolder_MT);
movefile(fullfile(SourceFolder,'*_T1w*'),DistFolder_T1w);
movefile(fullfile(SourceFolder,'*_PD*'),DistFolder_PD );
How can I identify the target files in the SourceFolder , and move them to DistFolder when some of them exist while others don't? I unsuccesfully tried out something like:
if ~exist(DistFolder_MT)
movefile(fullfile(SourceFolder,'*_MT*'),DistFolder_MT)
end
%...the same for '*_T1w*' and '*_PD*'
Many thanks in advance

댓글 수: 1

Stephen23
Stephen23 2022년 12월 7일
편집: Stephen23 2022년 12월 7일
Note that the MOVEFILE documentation states "If source is a folder or is capable of specifying multiple files and destination does not exist, then movefile creates destination", so MKDIR is probably superfluous.

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2022년 12월 7일
편집: Stephen23 2022년 12월 7일
Capture the error as an output, and the error will not be thrown when you call the function:
[X,Y,Z] = movefile('doesnotexist*.txt','subdir')
X = logical
0
Y = 'No matching files named '/users/mss.system.Chrpre/doesnotexist*.txt' were found.'
Z = 'MATLAB:MOVEFILE:FileNotFound'

댓글 수: 3

Thanks a lot @Stephen23,
The code is way more efficient now thanks to your suggestion about "mkdir". However, I still do not manage to integrate the mentioned line to avoid interruption in the iteration when the "movefile" function does not find one of the requested files.
SourcePath ='/f1/f2/f3/f4/f5/f6/';
index = 01 : 1005;
%missing_idx = [07, 08, 15, 17,20, 21, 25, 26, 27, 30]; %
index(missing_idx) = [] %
%iteration for the 7th subfolder of the source path
for i = 1 : length(index)
if index(i) >= 1 & index(i) < 10
newParticipant = ['sub-s00' , num2str(index(i))];
SourceFolder = fullfile(SourcePath,newParticipant,'ses-01','anat')
elseif index(i) >= 10 & index(i) < 100
newParticipant = ['sub-s0' , num2str(index(i))];
SourceFolder = fullfile(SourcePath,newParticipant,'ses-01','anat')
elseif index(i) >= 100
newParticipant = ['sub-s', num2str(index(i))];
SourceFolder = fullfile(SourcePath,newParticipant,'ses-01','anat')
end
%Moving files to their new subfolders respectively
SourceFolder = fullfile([SourcePath,newParticipant],'ses-01/anat')
movefile(fullfile(SourceFolder,'*_MT*'),fullfile(SourceFolder,[newParticipant,'_MT']));
movefile(fullfile(SourceFolder,'*_T1w*'),fullfile(SourceFolder,[newParticipant,'_T1w']));
movefile(fullfile(SourceFolder,'*_PD*'),fullfile(SourceFolder,[newParticipant,'_PD']));
end
For instance, I've got the follwing error message due to inexistent "MT" files in S038. It stopped the script, of course:
Error using movefile
No matching files named '/f1/f2/f3/f4/f5/f6//sub-s038/f8/f9*_MT*' were found.
Error in moving_files_final (line 28)
movefile(fullfile(SourceFolder,'*_MT*'),fullfile(SourceFolder,[newParticipant,'_MT']));
" However, I still do not manage to integrate the mentioned line to avoid interruption in the iteration when the "movefile" function does not find one of the requested files."
Because your code still does not return any output arguments from MOVEFILE.
Here are the modified lines of code:
[X,Y,Z] = movefile(fullfile(SourceFolder,'*_MT*'),fullfile(SourceFolder,[newParticipant,'_MT']));
[X,Y,Z] = movefile(fullfile(SourceFolder,'*_T1w*'),fullfile(SourceFolder,[newParticipant,'_T1w']));
[X,Y,Z] = movefile(fullfile(SourceFolder,'*_PD*'),fullfile(SourceFolder,[newParticipant,'_PD']));
% ^^^^^^^^^^ this is exactly what I showed in my answer.
Oops....
A huge thanks @Stephen23
Best regards,

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

제품

릴리스

R2021b

태그

질문:

2022년 12월 7일

댓글:

2022년 12월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by