Fastest way to copy files on Windows
20 views (last 30 days)
Show older comments
Joe_Z on 22 May 2020
Commented: Rik on 22 May 2020
I am interested in the fastest way to copy files from one directory to another in MATLAB on Windows platform. I have two questions, one about the quickest way and the second about how memory is used in MATLAB which is why I did not just go ahead and test this myself.
A file structure driveA: main_folder > subfolders > files.ext could be copied by
% 1) Copy entire main folder
% 2) parallel copy the subfolders
subfolders = dir('driveA:\main_folder\*'); % ignoring . and .. for now
parfor i = 1:length(subfolders)
copyfile(fullfile(subfolders(i).folder, subfolder(i).name), ...
% 3) parallel copy every individual file
files = dir('driveA:\main_folder\*\*.ext')
parfor i = 1:length(files)
path_split = strsplit(files(i).folder, filesep);
path_no_drive = path_split(2:end);
copyfile(fullfile(files(i).folder, files(i).name), ...
fullfile('driveB', path_no_drive, files(i).name));
Q1: is there likely to be much difference in performance, particularly between methods 2 and 3?
Q2: the reason I did not test this myself is I have a feeling that when repeating copying files, if repeating a copyfile procedure the files copy much faster if they have been copied recently, as if they were still held in memory. Is this is the case?
Thanks for your help
Rik on 22 May 2020
About your second question: that depends on the storage medium you're using. Most drives will use some form of caching. If the files are still in the cache, the writing will be much faster.
I doubt there will be much difference between these three methods, although the second and third might trigger multiple threads of the file transfer, which would speed up the copying.
Find more on Environment and Settings 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!