copying file to a folder makes a new folder instead
3 views (last 30 days)
Show older comments
sesilia maidelin
on 14 Jul 2021
Commented: sesilia maidelin
on 15 Jul 2021
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 Comment
dpb
on 14 Jul 2021
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.
Accepted Answer
Simon Chan
on 14 Jul 2021
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 Comments
Walter Roberson
on 15 Jul 2021
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.
More Answers (0)
See Also
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!