Borrar filtros
Borrar filtros

Copy a user selected folder

21 visualizaciones (últimos 30 días)
Ian Hogan
Ian Hogan el 26 de Jun. de 2024 a las 11:15
Comentada: Ian Hogan el 26 de Jun. de 2024 a las 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?

Respuesta aceptada

Ashutosh Thakur
Ashutosh Thakur el 26 de Jun. de 2024 a las 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 comentario
Ian Hogan
Ian Hogan el 26 de Jun. de 2024 a las 12:06
Thanks this is what i was looking for.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by