フィルターのクリア

Copy a user selected folder

21 ビュー (過去 30 日間)
Ian Hogan
Ian Hogan 2024 年 6 月 26 日 11:15
コメント済み: Ian Hogan 2024 年 6 月 26 日 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?

採用された回答

Ashutosh Thakur
Ashutosh Thakur 2024 年 6 月 26 日 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 件のコメント
Ian Hogan
Ian Hogan 2024 年 6 月 26 日 12:06
Thanks this is what i was looking for.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by