copying file to a folder makes a new folder instead

3 次查看(过去 30 天)
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.

请先登录,再进行评论。

更多回答(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