Trouble with copyfile and rename

23 views (last 30 days)
Vlad State
Vlad State on 11 Apr 2018
Commented: Jan on 5 Dec 2021
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!
  6 Comments
Jan
Jan on 5 Dec 2021
@Zhenren Zhou: To understand, whyt the problem of your code and the best solution is, we ned to know the contents of currentpath. Actually your code shoudl work. To analyse, what's going on, use the debugger:
dbstop if error
Then run the code again. If Matlab stops at the error, check the values of the arguments:
disp(fullfile(currentpath, NameF))
disp(remotepath)
By the way, fullfile(filename) replies the contents of the variable filename without modifications. fullfile() is useful only, if it gets mutliple inputs.
if ~Files(i).isdir
...
end
This excludes all folders from the list of file, while
if ~ismember(Files(i).name, {'.', '..'})
...
end
excludes the current and the parent folder only. They solve different problems, so I do not see a reasons to decide, which one is "uglier".
What do you want to do? Copy all files and folder inside a specific folder to another folder? Or do you want to copy the files only?

Sign in to comment.

Accepted Answer

dpb
dpb on 11 Apr 2018
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.

More Answers (1)

Jan
Jan on 11 Apr 2018
Param.folder1 = 'C:\Users...';
Param.folder2 = 'C:\Users...';
Files = dir(Param.folder1);
for k = 1:length(Files)
if ~ismember(Files(i).name, {'.', '..'})
NameF = Files(i).name;
copyfile(fullfile(Param.folder1, NameF), fullfile(Param.folder2, 'rez.txt'));
end
end
Some changes:
  • fullfile is smarter than the string concatenation by []
  • As Adam has written already, [Param.folder1,Param.folder2,'rez.txt'] is not meaningful. The folder1 must be attached to the first file in NameF.
  • ismember looks nicer here.
  3 Comments
dpb
dpb on 11 Apr 2018
Actually, I spoke/typed too quickly; one needs a matching character in the wildcard expression; '*.*' returns everything irregardless so it sometimes isn't possible to make it work that way altho generally can find a matching pattern that will work for a given case.
It isn't a general panacea, though, should have put a few more caveats on the use. The isdir field is the real ticket for general code, though, don't understand why so few ever seem to use it. Excepting, of course if one is actually interested in directories as well then may still need to screen for the two (basically useless) returned directories as opposed to "real" ones. I never for the life of me could figure why Bill chose to implement it the way they did and return the nuisance values.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by