Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Copyfile can't seem to create new destination

301 views
Skip to first unread message

KJ

unread,
May 17, 2017, 5:56:08 AM5/17/17
to
Hi all, I'm trying to use copyfile() in R2016b to copy a large number of files from one place to another, where the destination folder doesn't exist yet but according to the copyfile() help page, should be created automatically.

The code (simplified for posting) is

for i = 1:length(listing)
for k=1:length(files)
source = [root listing(i).name '\' files(k).name ];
destination = [destinationroot listing(i).name '\' ];
[status,message,messageId] = copyfile(source,destination);
end
end

listing is a struct containing all the subfolders and files is a struct containing the files I'm trying to copy, per subfolder in listing. root and destinationroot both exist. The subfolder listing(i).name does not exist in destinationroot and this subfolder is what should be created by copyfile().

The copy doesn't work, giving me this output in the command window:
_____________________________________________________________
status =
logical
0

message =
The filename, directory name, or volume label syntax is incorrect.

messageId =
MATLAB:COPYFILE:OSError
_____________________________________________________________
I have tried adding 'f' in copyfile(_,_,'f') but that didn't help. The only thing that works is creating the subfolder manually so that copyfile() doesn't have to do it. I'd prefer to not have to do that, since there are hundreds of folders to do.

Does anyone have any ideas on what the problem is? The destinationroot directory doesn't seem locked to me or anything, it's just another folder under /Documents. Am I completely missing something basic?
Thanks.

dpb

unread,
May 17, 2017, 8:55:00 AM5/17/17
to
On 05/17/2017 4:56 AM, KJ wrote:
> Hi all, I'm trying to use copyfile() in R2016b to copy a large number of
> files from one place to another, where the destination folder doesn't
> exist yet but according to the copyfile() help page, should be created
> automatically.
>
> The code (simplified for posting) is
>
> for i = 1:length(listing)
> for k=1:length(files)
> source = [root listing(i).name '\' files(k).name ];
> destination = [destinationroot listing(i).name '\' ];
> [status,message,messageId] = copyfile(source,destination);
> end
> end
...


Well, that isn't _exactly_ what the doc says...what it actually says
regarding creating a new destination when source is other than a
subdirectory is

"When source is multiple files and destination does not exist,
copyfile creates destination."

It doesn't say anything about when source is a single file.

Can you use a wildcard matching pattern instead of explicit file names?

--

Saravanan Mani

unread,
May 17, 2017, 9:41:08 AM5/17/17
to
"KJ" wrote in message <ofh6nk$ebc$1...@newscl01ah.mathworks.com>...
Dear KJ,

1. Status - Logical value is zero means, no folder/path

can you please try following script. I was used R2010b. It is working from my side.

I write it new script because I don't have input here given m-script- listing, root, files variables.

I was looking into current directory "Array_Script.m"

clc;
clear;
files_list = dir('*.m');
dest_path = uigetdir(pwd,'Select destination folder (D:\....\Saravanan)');
% user can specify the directory.
% dest_path = pwd;
% dest_path = 'C:\....\foldername';
for n=1:length(files_list)
file_name = (files_list(n).name);
% Searching Array m-script from current directory
if (~isempty(regexp(file_name,'Array','once')))
dest_file_name = [dest_path '\' file_name];
[status,msg,msgid] = copyfile(file_name,dest_file_name,'f');
else
dest_file_name = [dest_path '\' file_name];
[status,msg,msgid] = copyfile(file_name,dest_file_name,'f');
end
end

Please let me know if you have any concern

Thanks & Regards,
Saravanan

KJ

unread,
May 18, 2017, 9:44:08 AM5/18/17
to
"dpb" wrote in message <ofhh0k$hr9$1...@dont-email.me>...
Ha... That's pretty pedantic to allow destination creation only for multiple files..!
Using a wildcard to catch all source files at once worked, and it created the destination as expected. Thank you!

dpb

unread,
May 18, 2017, 9:51:35 AM5/18/17
to
On 05/18/2017 8:44 AM, KJ wrote:
> "dpb" wrote in message <ofhh0k$hr9$1...@dont-email.me>...
...

>> Well, that isn't _exactly_ what the doc says...what it actually says
>> regarding creating a new destination when source is other than a
>> subdirectory is
>>
>> "When source is multiple files and destination does not exist,
>> copyfile creates destination."
>>
>> It doesn't say anything about when source is a single file.
>>
...

> Ha... That's pretty pedantic to allow destination creation only for
> multiple files..!
> Using a wildcard to catch all source files at once worked, and it
> created the destination as expected. Thank you!

It's a result of the way the underlying OS COPY command works, at least
on MS.

It can be ambiguous in meaning for only a single file in interpretation
so the easy way out is to toss all out instead of try to parse more
extensively.

--

KJ

unread,
May 18, 2017, 11:16:07 AM5/18/17
to
"dpb" wrote in message <ofk8mn$dbi$1...@dont-email.me>...
For future reference (for me and other potential readers) is there a way to use wildcard to get multiple file names, but also add an 'except' rule, like an anti-wildcard, to exclude all files with some specific string in the name?
e.g. I want to copy all of apples_1_ok.csv bananas_1_maybe.csv peaches_1_sometimes.csv (get all *_1_*.csv), but do not want to copy apples_1_done.csv bananas_1_done.csv and peaches_1_done.csv (exclude '_done' string). With this example, using *_1_*.csv would also unnecessarily copy over the latter 3 files.

dpb

unread,
May 18, 2017, 1:12:44 PM5/18/17
to
On 05/18/2017 10:16 AM, KJ wrote:
...

> For future reference (for me and other potential readers) is there a way
> to use wildcard to get multiple file names, but also add an 'except'
> rule, like an anti-wildcard, to exclude all files with some specific
> string in the name?
> e.g. I want to copy all of apples_1_ok.csv bananas_1_maybe.csv
> peaches_1_sometimes.csv (get all *_1_*.csv), but do not want to copy
> apples_1_done.csv bananas_1_done.csv and peaches_1_done.csv (exclude
> '_done' string). With this example, using *_1_*.csv would also
> unnecessarily copy over the latter 3 files.

Under Matlab COPYFILE(), no.

Under MS default shell, no.

There are other command shells one can use that have that facility (JP
Software products for one that do have such extended functionality.

--

dpb

unread,
May 18, 2017, 2:37:58 PM5/18/17
to
On 05/18/2017 12:12 PM, dpb wrote:
...

> Under Matlab COPYFILE(), no.
...

Of course, you can return a directory structure using the larger
wildcard match and use regexp() on it to pare it down.

But, that takes then traversing the remaining directory entries afterwards.

I forget is the MS shells accept input from file or not; the JPSoft
shell I've used for >20 yr does so could create a file and use it as the
input there--of course, it has EXCEPT and other file exclusion list
capabilities the MS shells don't as noted before.

I've always wondered why TMW left the Matlab toolset so neutered as it
is...made a number of enhancement requests in the dark ages that were
never acted upon so just gave it up as lost cause...

--

0 new messages