MATLAB: Trouble with copyfile and rename

copyfiledirrename

Hello there, I need some help with an annoying issue that I can't fix by myself even though I've tried.
So I have 2 folders. In folder 1 there's a .txt file which I've downloaded (it has a random name), and I want to move it into folder 2, but I want to be able to rename it while I do that.
I've used dir in order to get all the files in folder 1, then I got the name of the file that's in it, but when I use copyfile in order to duplicate it into folder 2, I get this error "The filename, directory name, or volume label syntax is incorrect."
Param.folder1='C:\Users...';
Param.folder2='C:\Users...';
Files=dir(Param.folder1);
for i=1:length(Files)
if((~strcmp(Files(i).name,'.'))&&(~strcmp(Files(i).name,'..')))
NameF=Files(i).name;
copyfile(NameF,[Param.folder1,Param.folder2,'rez.txt']);
end
end
Thank you!

Best Answer

copyfile(NameF,[Param.folder1,Param.folder2,'rez.txt']);
expands to
copyfile(NameF,'C:\Users...C:\Users...rez.txt');
which isn't name of any file.
copyfile(fullfile(Param.folder1,NameF),fullfile(Param.folder2,'rez.txt'));
should do what you want.
HINTS: Use the debugger and stop on the error; pick pieces like the argument string out of the command to evaluate to see what actually is. Also, use the optional return values to get more expansive error messages that can be most helpful.