Trouble with copyfile and rename
Ältere Kommentare anzeigen
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 Kommentare
Adam
am 11 Apr. 2018
You need to show the code you used, otherwise we are just taking your word for it that you did everything right and then suddenly it can't find the file!
Vlad State
am 11 Apr. 2018
Adam
am 11 Apr. 2018
What are you expecting this
[Param.folder1,Param.folder2,'rez.txt']
to evaluate to? It doesn't seem like anything sensible would result from it.
Zhenren Zhou
am 3 Dez. 2021
Bearbeitet: dpb
am 4 Dez. 2021
Files = dir(currentpath);
for i = 1:length(Files)
if ~ismember(Files(i).name, {'.', '..'})
NameF = Files(i).name;
disp(NameF);
copyfile(fullfile(currentpath, NameF), fullfile(remotepath));
m = m +1;
end
end
Running the above code got the below error. If comment out if condition and its end, there is not the error, but an extra sub folder with everything under currentpath got copied. How to avoid the sub folder generating? or How to get rid of the error? Thanks.
Error using copyfile
The filename, directory name, or volume label syntax is incorrect.
dpb
am 4 Dez. 2021
if ~ismember(Files(i).name, {'.', '..'})
is not robust and is ugly even if were up to doing what is needed. The dir() structure returned contains the logical field isdir, use it as is intended and you'll then also not get other directory entries confused as files...
Use
if Files(i).isdir, continue, end
instead to skip over folders.
Jan
am 5 Dez. 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?
Akzeptierte Antwort
Weitere Antworten (1)
Jan
am 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 Kommentare
dpb
am 11 Apr. 2018
To amplify somewhat on the style discussion...
"ismember looks nicer here."
Or use the isdir logical returned by dir...
if ~Files(i).isdir
My personal preference if at all possible is to use an appropriate wildcard on the dir pattern so that only files are returned; I never use an extension on directory names for that amongst other reasons so
d=dir(fullfile(Param.folder1,'*.*'));
would be guaranteed to only return files, not directory entries.
Jan
am 11 Apr. 2018
@dpb: Does this exclude folders if their names contain a dot?
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.
Kategorien
Mehr zu File Operations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!