I get an error with copyfile loop
9 ビュー (過去 30 日間)
古いコメントを表示
I am trying to copy files from a list and past them in a new folder, but the loop does not work
%copy files in list from one folder to another
%%read text file with file names from previous step
FileList=textread('E:\Songmeter_2016-2017\Clean_data\Archivos_168h.txt','%s')
%%set the source directory as current directory
cd('E:\Songmeter_2016-2017\Clean_data\\')
%%create a destinaiton directory
%mkdir('E:\Songmeter_2016-2017\Clean_data\subsample_168h')
%%copyfcopycile loop
for n=1:numel(FileList)
source=FileList{n}
if (FileList{n})==0
continue
end
destination='E:\Songmeter_2016-2017\Clean_data\subsample_168h'
copyfile(source,destination)
end
2 件のコメント
Asvin Kumar
2021 年 5 月 10 日
編集済み: Asvin Kumar
2021 年 5 月 10 日
What error are you seeing? What behaviour are you seeing when you say it doesn't work?
What are some sample values in 'FileList'? It's hard to provide solutions when the problem isn't clear.
Please provide more details so that the community can help you.
回答 (1 件)
Asvin Kumar
2021 年 5 月 11 日
Thanks for providing additional info. I'm not entirely sure why you're seeing this error. There are some pieces of information which are unknown to me like the version of MATLAB you're using and the directory structure that you're working with. Here are a few suggestions:
1. In R2019b and earlier, the error message "The system cannot find the path specified" occured when the destination directory did not exist. So, that's one thing you could check.
2. You said your script works when you run it from within a subfolder that contains the files that you are trying to copy. You can check whether your Archivos_168h.txt file includes subfolder information. Refer to listOfFiles.txt in the example below.
3. Since I'm not too sure of your setup, I can share what works for me. You can adapt this to see if it works for you too. I'm using R2021a.
This is a screenshot of my C:\Users\Asvin\TestCopyFile directory
file1-file6 are dummy files for testing. I want to move all 6 files to a folder called 'movehere' in the same directory.
This is what scriptToCopyFiles.m looks like (very similar to yours):
%copy files in list from one folder to another
%%read text file with file names from previous step
FileList=textread('C:\Users\Asvin\TestCopyFile\listOfFiles.txt','%s');
%%set the source directory as current directory
cd('C:\Users\Asvin\TestCopyFile\\')
%%create a destinaiton directory
mkdir('C:\Users\Asvin\TestCopyFile\movehere\')
%%copyfcopycile loop
for n=1:numel(FileList)
source=FileList{n}
if (FileList{n})==0
continue
end
detination='C:\Users\Asvin\TestCopyFile\movehere'
copyfile(source,destination)
end
Here's what listOfFiles.txt looks like:
file1.m
file2.m
subfolder1\file3.m
subfolder1\file4.m
subfolder2\file5.m
subfolder2\file6.m
Note that I mention the path to file3-file6 relative to my current directory which is TestCopyFile.
And running the script with this setup works for me. Here's what the directory looks like after running the script:
If none of these suggestions work for you, I would recommend two approaches:
- Contact tech support via phone/email so that they can address your concerns quicker. Those channels are recommended for back-and-forth communication. Refer the contact us page. [Recommended]
- You could get back to me with details of your matlab version and preferably attach a zip file with a few test files that will help me reproduce this error that you're seeing.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!