copying file to a folder makes a new folder instead

조회 수: 6 (최근 30일)
sesilia maidelin
sesilia maidelin 2021년 7월 14일
댓글: sesilia maidelin 2021년 7월 15일
hi, so i want my code to create a folder based on a list of file's names, but when i want to move the file to the folder i created, it made a new folder instead with the name of the variable i used. help please
I = the files
for gg = 1: numel(I)
bmp = {I.name}; %the files I want to move
bmp_n = char(bmp(gg));
for ii = 1: numel(Plist)
P = dir( fullfile( mainfolder,Plist{ii}, '*.dcm'));
dcm = {P(~[P.isdir]).name};
for jj = 1 : numel(dcm)
dicom = char(dcm(jj)); % converts cell to string
mkdir( parentfolder, dicom); % (dicom is the variable of the name of the new folders, the new
%folders' name are eg 00004, 00005)
ptt = digitsPattern + regexprep(dicom, '.dcm$', '') + "_";
matchedIdx = contains(bmp_n, ptt) % matching the name of the files to the name of the folder
if matchedIdx == true
copyfile input dicom % in this part it makes a new file dicom and move the files there instead of moving it to 00004, 00005, 00006 etc)
end
end
end
any help would be greatly appreciated!
  댓글 수: 1
dpb
dpb 2021년 7월 14일
copyfile input dicom %
is the command line form for copyfile -- "input" and "dicom" are interpreted as literal strings, not as variables. Also input is the builtin MATLAB function and should not be used as variable -- and it isn't defined in the above code otherwise.
copyfile(YOURWANTEDFILENAMEINVARIABLE,dicom)
where the first argument needs to be a variable that contains the matching file names in the matched pattern. You'll need to iterate over the number of elements that are TRUE in matchedIdx as copyfile isn't vectorized I do not believe.

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

채택된 답변

Simon Chan
Simon Chan 2021년 7월 14일
Note that "myFolder" is the current folder storing the DCM files and the destination folders.
If they are in two different paths, then you may need to change the path:
cd(myFolder);
nfiles = dir(myFolder);
folders = {nfiles([nfiles.isdir]).name};
folders(1:2)=[];
files = dir(fullfile(myFolder,'*.dcm'));
filename={files.name};
for i = 1:length(filename)
matchedIdx = find(cellfun(@(x) contains(filename{i},x),folders));
copyfile(filename{i}, fullfile(myFolder,'\',folders{matchedIdx}));
end
  댓글 수: 11
Walter Roberson
Walter Roberson 2021년 7월 15일
At some point I found (and posted) a reference indicating that NTFS reads the Locale information of the person who created the NTFS file system, and locks the sorting order appropriate for that Locale into the filesystem.
For example, many many PC users use Windows-1252, which is like a superset of ISO-8896-1 . It has associated sorting rules, and those sorting rules are different than the rules mandated by Unicode . I personally never use Windows-1252: I always go for en-CA (English, Canada) locale, which uses UTF-8 by default. Someone who was using an NTFS filesystem created under Windows-1252 could get a different sorting order than I would get for creating exactly the same files in exactly the same order under en-CA . Furthermore if I change locale to create a second filesystem, then if I were to copy the same files to the two different filesystems, then the sort order could end up different for the two filesystems.
Apple has not been perfect either. The sorting order they used for filesystems before OS-X changed when HFS was put into use. HFS's rules for canonicalization of names were broken, and were changed, with (if I recall correctly) the change going into effect for the HFS+ . Some people consider HFS+'s canonicalization to also be broken, so the newer APFS filesystem has slightly different rules yet, if I understand correctly. On the other hand, it is the case that two different people who create HFS+ filesystems should get the same sort order no matter what locale they were using.
sesilia maidelin
sesilia maidelin 2021년 7월 15일
it works nicely, thank you! :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by