I get an error with copyfile loop

22 次查看(过去 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
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.
Susannah Buchan
Susannah Buchan 2021-5-10
Many thanks Asvin. I get this error:
Error using copyfile
The system cannot find the path specified.
Error in Copyfiles_from_txt (line 31)
copyfile(source,destination)
Two issues that I have found by playing with things:
1) The script is not finding the files listed in the txt file (Archivos_168h.txt); these files are in folders and subfolders of cd('E:\Songmeter_2016-2017\Clean_data\\') but for some reason it is not finding them, even though I have specifed the double back slash. I have also tried cd(fullfile('F:\Songmeter_2016-2017\Clean_data')) This is my first issue.
2) The second issue is that the script should go through all the files listed in the txt file (Archivos_168h.txt), however when it does not find the first one, it stops and does not continue.
I know both of the above because when I specify a specific subfolder (working around issue 1) and I make sure that the first file in the Archivos_168h.txt file is definitely present in the specfic subfolder (working around issue 2), the script works. However, it is not fesible for me to use this work around for the over 300 files I want to copy.
Here are some sample values in FileList:
>> FileList=textread('F:\Songmeter_2016-2017\Clean_data\Archivos_168h.txt','%s')
FileList =
336×1 cell array
{'SM-01-MEL__0__20160108_144927.wav'}
{'SM-01-MEL__0__20160108_161927.wav'}
{'SM-01-MEL__0__20160108_171927.wav'}
{'SM-01-MEL__0__20160108_174927.wav'}
Many thanks!

请先登录,再进行评论。

回答(1 个)

Asvin Kumar
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:
  1. 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]
  2. 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.

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by