Bug with copyfile for matlab 2015b related to string length "The system cannot find the path specified." - Question still unresolved

I am trying to move files from one folder to another and I am getting the error "The system cannot find the path specified." Both paths exist and I am pretty sure there is a glitch related to string length. I am calling out the function like so:
% move files
[copystat, err] = copyfile([parameters.inputdir cont],destination);
where the error will be thrown if :
cont = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX_20151217_143010.raw
but not if:
cont = XXXXXXXXXXXXXXXXXXXXXXXXXXXX_20151217_143010.raw
There is only 1 character different in these two. Also all strings longer than this will throw the error too. The longest string that will work is 143 characters for [parameters.inputdir cont]. I am assuming it is an issue with string length because of this and was hoping someone out there would have an idea for how to allow longer strings.
Any help is greatly appreciated.
EDIT: I found out more information related the issue... I noticed that size(destination) is 210 char. So size([destination '\' cont]) is 261 char. I tried adding the long path to destination so my code is now:
[copystat, err] = copyfile(['\\?\' parameters.inputdir cont],['\\?\' destination]);
but this doesn't seem to work either and throws a different error: "The filename, directory name, or volume label syntax is incorrect."
but this is the same destination that used to work with shorter names (without the \\?\). Also it won't even copy shorter paths over (same error). Is the \\?\ not allowed on destination for some reason?
Note: I also tried
[copystat, err] = copyfile([parameters.inputdir cont],['\\?\' destination]);
and got the same error.

9 Comments

I would have expected a limit of 260
Does the limit change if you use long-pathnames? \\?\.... ?
Is it possible that when you fully-qualify the filenames using the current directory (since they are relative paths) that the name is coming out too long?
Yes that is more of the length I was expecting also, but no that doesn't help. I had already tried that earlier while I was trying to debug it.
I don't think that could be the problem either because parameters.inputdir is the full path of the directory.
I found out more information related the issue... I noticed that size(destination) is 210 char. So size([destination '\' cont]) is 261 char. I tried adding the long path to destination so my code is now:
[copystat, err] = copyfile(['\\?\' parameters.inputdir cont],['\\?\' destination]);
but this doesn't seem to work either and throws a different error: "The filename, directory name, or volume label syntax is incorrect."
but this is the same destination that used to work with shorter names (without the \\?\). Also it won't even copy shorter paths over (same error). Is the \\?\ not allowed on destination for some reason?
Note: I also tried
[copystat, err] = copyfile([parameters.inputdir cont],['\\?\' destination]);
and got the same error.
I assume you definitely have write access in the destination folder and that the file you are trying to write is not already in existence and open either in Matlab or some other software (I think the error message would be different though in the latter case, but I can't remember off-hand)
What does '\\?\' represent?
It isn't usual to have a ? in a directory name and you aren't using regexp or anything where it would be interpreted as a special character so far as I can see.
The Windows API has many functions that also have Unicode versions to permit an extended-length path for a maximum total path length of 32,767 characters. This type of path is composed of components separated by backslashes, each up to the value returned in the lpMaximumComponentLength parameter of the GetVolumeInformation function (this value is commonly 255 characters). To specify an extended-length path, use the "\\?\" prefix. For example, "\\?\D:\very long path".
Adam, yes I have permission and it is not open anywhere else. I can copy over the same file if I shorten the name and remove the long-path prefix, but when it has the prefix it fails.
Hi, I have exactly the same problem... Do you have resolved this issue? It seems to me it is not only the string's length that matters, as I kept having the same error with short string while it's copying the file for longer string.
@Jean: In the OP's case the string length was the problem. If this is not the problem in you case, you do not have exactly the same problem. Please post the name of the failing files. Do they contain unicode characters? Did you check, that the file names are valid? No stars, no colons, no question marks etc?

Sign in to comment.

Answers (2)

Perhaps Fex: GetFullPath is useful to insert the specifier for long file names automatically. But the error mesage
The filename, directory name, or volume label syntax is incorrect.
might point in another direction: Are the path names really valid file names?

1 Comment

Yes it finds it in both cases, but it only has the problem when trying to execute copystat. (I was running it in a loop to do multiple files and 4 was the failure because the name was longer. Same results outside of a loop though.)
Here is everything I was trying:
K>> exist(destination)
ans =
7
K>> exist(['\\?\' destination])
ans =
7
K>> clear copystat
for ii = 1:size(cont,1)
[copystat(ii), ~] = copyfile(['\\?\' parameters.inputdir cont{ii,1}],[destination])
end
copystat =
1 1 1 0 1 1
K>> clear copystat
for ii = 1:size(cont,1)
[copystat(ii), ~] = copyfile(['\\?\' parameters.inputdir cont{ii,1}],['\\?\' destination])
end
copystat =
0 0 0 0 0 0
In the first case I got the error "The system cannot find the path specified." on number 4.
In the next case I got the error "The filename, directory name, or volume label syntax is incorrect." on all them.

Sign in to comment.

Certain programs have their own limits, like Excel's 218 character limit for filenames. Who knows why? I do not know if MATLAB has a similar limit or what it might be if it has one.

3 Comments

I understand there is the limitation on pathname length, but the long pathname tool is not working. Also the "\\?\" should not effect the ability for Matlab to find the path in copyfile but it is. This is shown in my example in my comment to the other attempted answer.
Something to try: use forward slashes instead of backslashes. Believe it or not, Windows understands forward slashes in filenames perfectly fine.
I didn't know about this, but there is still the exact same result with forward slashes.

Sign in to comment.

Categories

Asked:

on 7 Mar 2016

Commented:

Jan
on 28 Sep 2016

Community Treasure Hunt

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

Start Hunting!