MATLAB: Copyfile with long path names

copyfilewindows file system

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

Best Answer

It is not a problem of Copy&Paste, but file names have this general limitation under Windows. E.g. the Windows Explorer cannot move files to the recycle bin, if their name is longer.
As workaround you can use the Windows API function and mask the name with the leading magic string '\\?\'. But a lot of important functions (like the already mentioned recycling) will not work. Even e.g. DIR fails for too long path and/or folder names (at least here the masking works, see also FEX: GetFullPath). Therefore I suggest three strategies:
  1. Write an enhancement request to Microsoft. This is such a dull and idiotic limitation, which blocks a serious using of the system since the first DOS implementations. Try to reach Bill Gates personally, because he is responsible.
  2. Use a serious operating system for serious tasks. Linux, Unix, Aix, BeOS, HP-UX, MacOS (7,8,9,X), Minix, BSD, Solaris, VMS, and even the Sinclair ZX Spectrum (1982) had no such limits in the file name length.
  3. Use shorter file names, when you insist on using Windows for considerable reasons.
Note that very long file names usually mean, that the user inserts important information into the name, instead of storing it in the contents. But it is not efficient to store important information in the filesystem's name tables. A slightly exaggerated example: You can even store pictures in the name tables by using spaces and X as black&white pixels and using a very small font size for the command window, where the LS command is called. But then you cannot blame the OS, if you cannot use duplicate pixel rows or if the output is dosplayed in alphabetical order.
PS. Sometimes I think of inserting smilies in my messages, but I still hope my English is distinct enough to let the readers understand the intention without such extra markers. Of course I know, that Bill will no answer, but I'm impressed every time TMWs technical support responses to such enhancement requests.