Copy a user selected folder

조회 수: 2 (최근 30일)
Ian Hogan
Ian Hogan 2024년 6월 26일
댓글: Ian Hogan 2024년 6월 26일
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?

채택된 답변

Ashutosh Thakur
Ashutosh Thakur 2024년 6월 26일
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
Ian Hogan
Ian Hogan 2024년 6월 26일
Thanks this is what i was looking for.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by