question regarding 'copyfile' function from folder to folder
103 views (last 30 days)
Show older comments
Yu Li on 4 Aug 2019
Commented: David Young on 7 Mar 2023 at 15:07
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?
Jan on 4 Aug 2019
Edited: Jan on 4 Aug 2019
This is exactly, what you find in the documentation. Please read:
[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".
David Young on 7 Mar 2023 at 15:07
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.
More Answers (1)
George Abrahams on 3 Jan 2023
Edited: George Abrahams on 3 Jan 2023
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:
- copyfile("uniqueFile.txt","nonExistentFolder") unexpectedly copies the file to "nonExistentFolder", i.e., it creates a file named nonExistentFolder in the working directory.
- 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.
- copyfile("uniqueFile.txt","nonExistentFolder\uniqueFile.txt") fails and gives the following error: "The location 'nonExistentFolder\uniqueFile.txt' was not found."
- copyfile("uniqueFile.txt*","nonExistentFolder") and copyfile("uniqueFile.txt*","nonExistentFolder\") both successfully copy the file to "nonExistentFolder\uniqueFile.txt".
- 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 on 7 Jan 2023
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.
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!