Effacer les filtres
Effacer les filtres

Copy a user selected folder

21 vues (au cours des 30 derniers jours)
Ian Hogan
Ian Hogan le 26 Juin 2024 à 11:15
Commenté : Ian Hogan le 26 Juin 2024 à 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?

Réponse acceptée

Ashutosh Thakur
Ashutosh Thakur le 26 Juin 2024 à 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 commentaire
Ian Hogan
Ian Hogan le 26 Juin 2024 à 12:06
Thanks this is what i was looking for.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by