I get an error with copyfile loop
6 次查看(过去 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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/614070/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/614075/image.png)
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!