Clear Filters
Clear Filters

Copy a user selected folder

21 views (last 30 days)
Ian Hogan
Ian Hogan on 26 Jun 2024 at 11:15
Commented: Ian Hogan on 26 Jun 2024 at 12:06
I am try to create a prompt where the user can select a folder and then this folder is copied to a new locationsuch as:
file = input('What folder would you like to copy?');
selected_dir = uigetdir();
and the copy the selected folder to a new location: i.e
test Folder source = selected_dir
copyfile('test Folder source','C:\TEMP\test Folder destination')
if there a clean way to make such a function?

Accepted Answer

Ashutosh Thakur
Ashutosh Thakur on 26 Jun 2024 at 11:56
Hello Ian,
Based on the description, I understand that you want to upload a user-selected folder to a new location, i.e., in a new folder. This can be achieved by leveraging the uigetdir function to get the selected directory as well as the destination directory. You can then construct the final path from these values and use the copyfile function to copy the contents from the source to the destination folder. The following links can be leveraged for using the uigetdir and copyfile functions in MATLAB:
The following sample code can be referred to implement the above-mentioned approach:
% Prompt user to select a folder
selected_dir = uigetdir('', 'Select the folder you want to copy');
% Check if the user canceled the folder selection
if selected_dir == 0
disp('No folder selected. Exiting.');
return;
end
% Prompt user to input the destination folder path
dest_dir = uigetdir('', 'Select the destination folder');
% Check if the user canceled the destination folder selection
if dest_dir == 0
disp('No destination folder selected. Exiting.');
return;
end
% Construct the full path for the destination folder
[~, folderName, ~] = fileparts(selected_dir);
dest_dir_full = fullfile(dest_dir, folderName);
% Copy the selected folder to the destination
try
copyfile(selected_dir, dest_dir_full);
fprintf('Folder "%s" successfully copied to "%s".\n', selected_dir, dest_dir_full);
catch ME
fprintf('Failed to copy folder "%s" to "%s".\n', selected_dir, dest_dir_full);
disp(ME.message);
end
Additionally these links would also be helpful:
I hope this information helps you!
  1 Comment
Ian Hogan
Ian Hogan on 26 Jun 2024 at 12:06
Thanks this is what i was looking for.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!