Copyfile/ movefile when filenames are not directly available

2 ビュー (過去 30 日間)
Afzal Mohammed
Afzal Mohammed 2020 年 3 月 28 日
コメント済み: Afzal Mohammed 2020 年 3 月 29 日
Hello Everyone,
I have two .xlsx files in a folder, I want to copy/ move those .xlsx files to a different folder but the user is not aware of file names which are stored in a workspace variable of the same Matlab program.
As shown below: 'flagged' is a workspace variable of my program which has file names. I want to copy/ move those specific files to a separate folder.
I tried following but it's not working
copyfile flagged(i).name newFolder
Please advise.
Thanks!
  1 件のコメント
Afzal Mohammed
Afzal Mohammed 2020 年 3 月 29 日
Following lines helped.
mkdir('newFolder');
copyfile(flagged{i}, './newFolder');

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

採用された回答

Peng Li
Peng Li 2020 年 3 月 28 日
flagged is a cell so you couldn’t access it using parentheses and dot motion.
flagged{i} is your ith element in flagged. For example flagged{1} will be myFile.xlsx. If you want to move it somewhere you may also need the full directory as well unless it’s in the current directory.
  3 件のコメント
Afzal Mohammed
Afzal Mohammed 2020 年 3 月 29 日
Peng Li, appreciate your tips, thanks.
Peng Li
Peng Li 2020 年 3 月 29 日
If you need to create a folder called newFolder, use mkdir('newFolder'); first to create a folder named newFolder at the current directory.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 3 月 28 日
Assuming that newFolder is the name of a variable that stores the destination folder name:
copyfile(flagged(i).name, newFolder)
However I would recommend that you should be using fullfile() and should be using exist() to test to be sure that the file exists before you attempt to copy it.
  5 件のコメント
Afzal Mohammed
Afzal Mohammed 2020 年 3 月 29 日
編集済み: Afzal Mohammed 2020 年 3 月 29 日
Walter,
ohh my bad, I apologize for creating the confusion, I was trying something and forgot to delete the last line. I know the last line from my code as shown below doesn't make any sense.
newFolder {i} = flagged{i}
Afzal Mohammed
Afzal Mohammed 2020 年 3 月 29 日
Peng Li,
Correct! that is what I was trying to. Your code workd :) Yeyyy!! Much appreciated, thanks!

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

カテゴリ

Help Center および File ExchangeDatabase Toolbox についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by