필터 지우기
필터 지우기

question regarding 'copyfile' function from folder to folder

조회 수: 57 (최근 30일)
Yu Li
Yu Li 2019년 8월 4일
댓글: David Young 2023년 3월 7일
Hi:
I tried to use 'coypfile' function from folder to folder.
assuming I have file a.txt, b.txt in folder 'A', and a.txt, b.txt, c.txt in folder 'B', using command: copyfile('A','B'), looks like it has some behaviors below:
  • it will only not replace folder 'B' with 'A', it seems like copy files in to folder 'A' to folder 'B', i.e. the file 'c.txt' in folder 'B' will not be removed
  • copyfile will overwrite the files having the same name in folder 'B' by files from folder 'A', i.e. the file 'a.txt' and 'b.txt' in folder 'B' will be overwrite by 'a.txt' and 'b.txt' from folder 'A'
but I could not find any illustration regarding this behavior anywhere, am I correct with the observation described above?
Thanks!
Yu

채택된 답변

Jan
Jan 2019년 8월 4일
편집: Jan 2019년 8월 4일
This is exactly, what you find in the documentation. Please read:
help copyfile
doc copyfile
[SUCCESS,MESSAGE,MESSAGEID] = copyfile(SOURCE, DESTINATION) attempts to
copy SOURCE to DESTINATION. If SOURCE constitutes a directory or
multiple files and DESTINATION does not exist, copyfile attempts to
create DESTINATION as a directory and copy SOURCE to DESTINATION. If
SOURCE constitutes a directory or multiple files and DESTINATION exists
as a directory, copyfile attempts to copy SOURCE to DESTINATION.
This means, that copyfile copies the files from the source directory to the destination directory. This sounds intuitive for a function, which is called "copy file".
  댓글 수: 2
Yu Li
Yu Li 2019년 8월 4일
Hi Jan:
Thanks for your reply. I read the link below, but looks like it is not as detail as what shows in your answer.
Thanks!
Yu
David Young
David Young 2023년 3월 7일
I don't think the point is covered in the documentation. I can't see where it describes what happens when both source and destination are directories, both exist, and some files are present in both and some in only one or the other.
In general, the doc information is unclear and incomplete. The help information is a little better, but it should never be necessary to use both commands to get all the specification of the function.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

George Abrahams
George Abrahams 2023년 1월 3일
편집: George Abrahams 2023년 1월 3일
Agree with @Yu Li. Lots of problems in the documentation for copyfile and movefile.
No comment in documentation of overwriting. At least on Windows, both copyfile and movefile quietly overwrite files, i.e. without a warning, error, message, or confirmation. It's not that the functions need changing, but the behaviour should definitely be documented, as it could lead to loss of data, but currently is not. Experienced programmers would be aware of this danger; others would not.
The documentation for directory creation with these functions when destination does not exist is also both wrong and/or missing information:
  • The documentation for copyfile specifies that directory creation will occur if source specifies multiple files. In reality, the behaviour is similar to movefile, in that directory creation will occur if source is capable of specifying multiple files, i.e. it contains a wildcard, even it selects only one file.
  • The documentation specifies that: if source is a file, then destination can be a file or folder; and, if source is a folder, then destination must be a folder. However, if source is capable of specifying multiple files, then destination must also be a folder.
To give an example of why this can be so confusing, consider, for example, that a file name contains a version number which is updated over time, or a program outputs a file prepended with the date. In these cases a wildcard may be included in source with the intention to select only one file. In code, for copyfile("data_v*.txt","results\data.txt") the file destination would unexpectedly be "results\data.txt\data_v*.txt", i.e. it creates a "results" folder containing a folder named "data.txt" which contains the file with its original name.
Also while I'm here, both copyfile and movefile have what is, to me, a confusing directory creation behaviour, although it is at least documented. If destination does not exist, then these functions only create the folder structure to destination when source is either a folder or contains a wildcard, as described above. For example:
  1. copyfile("uniqueFile.txt","nonExistentFolder") unexpectedly copies the file to "nonExistentFolder", i.e., it creates a file named nonExistentFolder in the working directory.
  2. copyfile("uniqueFile.txt","nonExistentFolder\") fails and gives the following error: "The filename, directory name, or volume label syntax is incorrect." This is also a very unclear error message.
  3. copyfile("uniqueFile.txt","nonExistentFolder\uniqueFile.txt") fails and gives the following error: "The location 'nonExistentFolder\uniqueFile.txt' was not found."
  4. copyfile("uniqueFile.txt*","nonExistentFolder") and copyfile("uniqueFile.txt*","nonExistentFolder\") both successfully copy the file to "nonExistentFolder\uniqueFile.txt".
  5. copyfile("uniqueFile.txt*","nonExistentFolder\uniqueFile.txt") unexpectedly copies the file to "nonExistentFolder\uniqueFile.txt\uniqueFile.txt".
Possibly I'm missing some implementation difficulty, but I can't imagine why 4 would work but 1-3 don't. Only that it would either have to guess or you would have to specify whether destination should be a folder or file. It, of course, makes sense that 5 would not have the "expected" result - I just included it for completeness.
@Jan you're very experienced around these parts! Would this be considered a bug? Should I report it here or...?
  댓글 수: 1
Jan
Jan 2023년 1월 7일
Yes, it is worth to send an "enhancement request" to MathWorks. It is not a "bug", but inhertited from the more or less strange behavior of the corresponding DOS commands. In older Matlab versions, copyfile and movefile have been processed by system() calls and the behavior is kept.
It would be more useful to have 2 different commands: copyToFolder and copyToFile instead of relying on smart decisions based on the existence of the destination folder.
This is such confusing, that I consider it as "too ugly":
copyfile("uniqueFile.txt", "nonExistentFolder\uniqueFile.txt") % Fails
copyfile("uniqueFile.txt*", "nonExistentFolder\uniqueFile.txt") % Works
I'm using workarounds and do not use the built-in copyfile/movefile for these reasons.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Files and Folders에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by