Copyfile with long path names
Mostrar comentarios más antiguos
There seems to exist a limitation for the length of the file path when using the copyfile-command (something like 260 characters), because I get an 'Unknown error' when I want to copy to a destination with a longer path.
This seems to be related to a problem with the Windows copy+paste command (because I cannot even manually copy the files to that location). I googled the problem and it seems that there exist some workarounds for manual copy+paste. Do you know of any workarounds for this problem in Matlab?
My system: Windows 7 64-bit Matlab R2013a
8 comentarios
K E
el 13 de Oct. de 2015
Can someone point to where "Long path tool" can be found? I don't see it in the file exchange.
Walter Roberson
el 13 de Oct. de 2015
The references to "Long path tool" were spam. :(
Jan
el 27 de Mzo. de 2017
I'm not sure if an automatic deleteing is necessarily a fix of the problems.
jack reacher
el 25 de Nov. de 2017
Enable long file name support in Windows 10
Walter Roberson
el 25 de Nov. de 2017
Long filename support still has a limit of something like 259 or 260, unless you use the \\?\ noted below.
Ana Fletcher
el 21 de Abr. de 2024
Has anyone used LongPath Tool?
John
el 23 de Abr. de 2024
Yes, I have. Just Google it and you will find everything. It is easy btw.
Martin
el 24 de Abr. de 2024
I just installed the tool. I will soon share the reviews.
Respuesta aceptada
Más respuestas (2)
Hi,
have you tried to prefix \\?\ for the file path like mentioned here:
AFAIK MATLAB calls into the WIndows API function when using copyfile. At least my MATLAB doesnt complain when I build up a file path like
a = '\\?\C:\myfolder\test.dat'
copfyile(a,'C:\demo.dat')
And it also works. Does this work for you?
5 comentarios
Jan
el 23 de Mayo de 2013
Even the Windows API is restricted to MAX_PATH characters, as I have written already. While the ANSI version of CopyFile accepts MAX_PATH (260) characters only, the Unicode version CopyfileW accepts up to 32,767 characters, when you provide the leading magic string also. And the same for CopyFileEx.
Moving a file with a long name or path to the recycle bin ShFileOperation will not work at all. Even the displaying in the Windows Explorer can fail. Therefore the Windows API is the actual problem and not a solution.
At least this \\?\ prefix works for me with a path longer than 260 characters:
>> file = fullfile(pwd,'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb','test.txt');
>> numel(file)
ans =
285
>> exist('D:\test1.txt','file')
ans =
0
>> copyfile(strcat('\\?\',file),'D:\test1.txt')
>> exist('D:\test1.txt','file')
ans =
2
When I don't add the \\?\ prefix I get an error:
>> copyfile(file,'D:\test1.txt')
Error using copyfile
No matching files were found.
The magic (another word for ugly here) string '\\?\' helps also, for scanning folders with long pathes by the DIR command. Unfortunately it is not the 260 characters limit, but some characters less. The length of the file names might matter also, but it cannot be known before getting the list. After some user requests, I've expanded GetFullPath, such that it can handle this securely:
List = dir(GetFullPath(Folder, 'fat'));
KAE
el 8 de En. de 2018
In case it helps someone else, here is an example for the move command if you need to move a file with a long name to a subdirectory resulting in an even longer name so that the final filename would exceed the 260 character limit. There are double quotes to deal with spaces in the file or directory name.
eval(['!move "\\?\' longFilename '" "' subdirectoryMakingLongerFilename '"']);
Iain
el 24 de Mayo de 2013
0 votos
I'm not sure how you can reasonably use this or if it still exists in Win7.
There is a "short" name for every file and folder, which is restricted to 8 characters.
C:\docume~1\ is the short path for C:\documents and settings\.
It gets extremely hard to use if you have huge numbers of similarly named files, because the "~1" part gets longer and more complicated.
1 comentario
Jan
el 24 de Mayo de 2013
Fortunately the Windows API offers method to get the short name automatically, such that a simple Mex-File can achieve the conversion. But even the short name must fail, when the path of a file is deeply nested and even the resulting short name exceeds MAX_PATH characters. In addition error messages containing the abbrev. filename will look such ugly.
Categorías
Más información sobre Filename Construction en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!