MATLAB Answers

Is there a way to copy files with a name specified by a link contained within an array to a new folder?

1 ビュー (過去 30 日間)
Jack Robertson
Jack Robertson 2020 年 1 月 30 日
コメント済み: Geoff Hayes 2020 年 1 月 31 日
I am new to MatLab and am trying to copy multiple files from one folder, which the link is defined in a csv table, to a new location. I tried useing the following but it didnt work. ANy help would be much appreciated.
clear
close all
clc
%%
% Data import
Data = readtable('C:\Users\data.csv')
%%
% Select column with the file names
File_location = Data{:,5}
%%
mkdir C:\Users\Desktop\copied
status = copyfile(File_location, 'C:\Users\Desktop')

  0 件のコメント

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

採用された回答

Geoff Hayes
Geoff Hayes 2020 年 1 月 30 日
Jack - without seeing the error message from your above code, I suspect that File_location is a cell array of paths to files and you are trying to use copyfile which copies a single file or folder from one location to another. I think that you will want to loop over every file (string) in File_location and call copyfile on that file.

  5 件のコメント

表示 2 件の古いコメント
Jack Robertson
Jack Robertson 2020 年 1 月 30 日
thank you for the responce but i still get the following error and no files have moved:
Capture.PNG
Jack Robertson
Jack Robertson 2020 年 1 月 31 日
This code works fine if i stop it and manually enter the fileAndPath output into replace the 'FileAndPath' in status = copyfile('FileAndPath', 'C:\Users\Desktop\copied')
but when i run it it gives output as follows with the status logical showing status = logical 0 (if i manually enter the FileandPath output it gives status = logical 1)
2×1 cell array
{'C:\Users\Desktop\619834.svs'}
{'C:\Users\Desktop\619839.svs'}
fileAndPath =
'C:\Users\Desktop\619834.svs'
status =
logical
0
Copying file C:\Users\Desktop\619834.svs with status 0
fileAndPath =
'C:\Users\Desktop\619839.svs'
status =
logical
0
Copying file C:\Users\Desktop\619839.svs with status 0>>
Geoff Hayes
Geoff Hayes 2020 年 1 月 31 日
The error message is a type in my code (in the fprintf) where it is trying to reference a variable (fileAnPath) that doesn't exist. Try changing it to
for k = 1:length(File_location)
fileAndPath = File_location{k};
status = copyfile(fileAndPath, 'C:\Users\Desktop');
fprintf('Copying file %s with status %d', fileAndPath, status);
end

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

その他の回答 (0 件)

タグ


Translated by